From ee1dbf4766d59de267d691a8cae926b3adc9a4b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Tue, 12 Nov 2019 16:03:05 +0100
Subject: [PATCH] =?UTF-8?q?test:=20ajoute=20des=20tests=20pour=20les=20m?=
 =?UTF-8?q?=C3=A9thodes=20personnalis=C3=A9s=20avec=20DTO.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Fixtures/Controller/CustomController.php  | 51 +++++++++++++
 .../Entity/EntityWithCustomMethods.php        | 75 +++++++++++++++++++
 tests/Fixtures/ValueObject/InputDTO.php       | 31 ++++++++
 tests/Fixtures/ValueObject/OutputDTO.php      | 34 +++++++++
 tests/Functional/CustomMethodTest.php         | 66 ++++++++++++++++
 5 files changed, 257 insertions(+)
 create mode 100644 tests/Fixtures/Controller/CustomController.php
 create mode 100644 tests/Fixtures/Entity/EntityWithCustomMethods.php
 create mode 100644 tests/Fixtures/ValueObject/InputDTO.php
 create mode 100644 tests/Fixtures/ValueObject/OutputDTO.php
 create mode 100644 tests/Functional/CustomMethodTest.php

diff --git a/tests/Fixtures/Controller/CustomController.php b/tests/Fixtures/Controller/CustomController.php
new file mode 100644
index 0000000..40ac22f
--- /dev/null
+++ b/tests/Fixtures/Controller/CustomController.php
@@ -0,0 +1,51 @@
+<?php declare(strict_types=1);
+/*
+ * This file is part of "irstea/ng-model-generator-bundle".
+ *
+ * "irstea/ng-model-generator-bundle" generates Typescript interfaces for Angular using api-platform metadata.
+ * Copyright (C) 2018-2019 IRSTEA
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option) any
+ * later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License and the GNU
+ * Lesser General Public License along with this program. If not, see
+ * <https://www.gnu.org/licenses/>.
+ */
+
+namespace Irstea\NgModelGeneratorBundle\Tests\Fixtures\Controller;
+
+use Irstea\NgModelGeneratorBundle\Tests\Fixtures\ValueObject\InputDTO;
+use Irstea\NgModelGeneratorBundle\Tests\Fixtures\ValueObject\OutputDTO;
+
+/**
+ * Class CustomController.
+ */
+final class CustomController
+{
+    /**
+     * @param InputDTO $input
+     *
+     * @return OutputDTO
+     */
+    public function customItem(InputDTO $input): OutputDTO
+    {
+        return new OutputDTO();
+    }
+
+    /**
+     * @param InputDTO[] $inputs
+     *
+     * @return OutputDTO[]
+     */
+    public function customCollection(array $inputs): array
+    {
+        return [];
+    }
+}
diff --git a/tests/Fixtures/Entity/EntityWithCustomMethods.php b/tests/Fixtures/Entity/EntityWithCustomMethods.php
new file mode 100644
index 0000000..193cb23
--- /dev/null
+++ b/tests/Fixtures/Entity/EntityWithCustomMethods.php
@@ -0,0 +1,75 @@
+<?php declare(strict_types=1);
+/*
+ * This file is part of "irstea/ng-model-generator-bundle".
+ *
+ * "irstea/ng-model-generator-bundle" generates Typescript interfaces for Angular using api-platform metadata.
+ * Copyright (C) 2018-2019 IRSTEA
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option) any
+ * later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License and the GNU
+ * Lesser General Public License along with this program. If not, see
+ * <https://www.gnu.org/licenses/>.
+ */
+
+namespace Irstea\NgModelGeneratorBundle\Tests\Fixtures\Entity;
+
+use ApiPlatform\Core\Annotation as API;
+use Irstea\NgModelGeneratorBundle\Tests\Fixtures\ValueObject\InputDTO;
+use Irstea\NgModelGeneratorBundle\Tests\Fixtures\ValueObject\OutputDTO;
+use Ramsey\Uuid\Uuid;
+use Ramsey\Uuid\UuidInterface;
+
+/**
+ * Class EntityWithCustomMethods.
+ *
+ * @API\ApiResource(
+ *     itemOperations={
+ *         "get",
+ *         "custom": {
+ *             "method": "PUT",
+ *             "input": InputDTO::class,
+ *             "output": OutputDTO::class,
+ *             "controller": "Irstea\NgModelGeneratorBundle\Tests\Fixtures\Controller\CustomController::customItem",
+ *         }
+ *     },
+ *     collectionOperations={
+ *         "custom": {
+ *             "method": "POST",
+ *             "input": InputDTO::class,
+ *             "output": OutputDTO::class,
+ *             "controller": "Irstea\NgModelGeneratorBundle\Tests\Fixtures\Controller\CustomController::customCollection",
+ *         }
+ *     }
+ * )
+ */
+class EntityWithCustomMethods
+{
+    /**
+     * @var UuidInterface
+     * @API\ApiProperty(identifier=true)
+     */
+    private $id;
+
+    public function __construct()
+    {
+        $this->id = Uuid::uuid4();
+    }
+
+    /**
+     * Get id.
+     *
+     * @return UuidInterface
+     */
+    public function getId(): UuidInterface
+    {
+        return $this->id;
+    }
+}
diff --git a/tests/Fixtures/ValueObject/InputDTO.php b/tests/Fixtures/ValueObject/InputDTO.php
new file mode 100644
index 0000000..40f45a0
--- /dev/null
+++ b/tests/Fixtures/ValueObject/InputDTO.php
@@ -0,0 +1,31 @@
+<?php declare(strict_types=1);
+/*
+ * This file is part of "irstea/ng-model-generator-bundle".
+ *
+ * "irstea/ng-model-generator-bundle" generates Typescript interfaces for Angular using api-platform metadata.
+ * Copyright (C) 2018-2019 IRSTEA
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option) any
+ * later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License and the GNU
+ * Lesser General Public License along with this program. If not, see
+ * <https://www.gnu.org/licenses/>.
+ */
+
+namespace Irstea\NgModelGeneratorBundle\Tests\Fixtures\ValueObject;
+
+/**
+ * Class InputDTO.
+ */
+final class InputDTO
+{
+    /** @var int[] */
+    public $values;
+}
diff --git a/tests/Fixtures/ValueObject/OutputDTO.php b/tests/Fixtures/ValueObject/OutputDTO.php
new file mode 100644
index 0000000..c32f9ba
--- /dev/null
+++ b/tests/Fixtures/ValueObject/OutputDTO.php
@@ -0,0 +1,34 @@
+<?php declare(strict_types=1);
+/*
+ * This file is part of "irstea/ng-model-generator-bundle".
+ *
+ * "irstea/ng-model-generator-bundle" generates Typescript interfaces for Angular using api-platform metadata.
+ * Copyright (C) 2018-2019 IRSTEA
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option) any
+ * later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License and the GNU
+ * Lesser General Public License along with this program. If not, see
+ * <https://www.gnu.org/licenses/>.
+ */
+
+namespace Irstea\NgModelGeneratorBundle\Tests\Fixtures\ValueObject;
+
+/**
+ * Class OutputDTO.
+ */
+final class OutputDTO
+{
+    /** @var bool */
+    public $success;
+
+    /** @var string[] */
+    public $results;
+}
diff --git a/tests/Functional/CustomMethodTest.php b/tests/Functional/CustomMethodTest.php
new file mode 100644
index 0000000..b9cd669
--- /dev/null
+++ b/tests/Functional/CustomMethodTest.php
@@ -0,0 +1,66 @@
+<?php declare(strict_types=1);
+/*
+ * This file is part of "irstea/ng-model-generator-bundle".
+ *
+ * "irstea/ng-model-generator-bundle" generates Typescript interfaces for Angular using api-platform metadata.
+ * Copyright (C) 2018-2019 IRSTEA
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option) any
+ * later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License and the GNU
+ * Lesser General Public License along with this program. If not, see
+ * <https://www.gnu.org/licenses/>.
+ */
+
+namespace Irstea\NgModelGeneratorBundle\Tests\Functional;
+
+/**
+ * Class CustomMethodTest.
+ */
+final class CustomMethodTest extends GenerationTestCase
+{
+    /**
+     * @param string $snippet
+     * @param string $code
+     *
+     * @test
+     * @dataProvider listExpectedSnippets
+     */
+    public function checkCodeSnippet(string $snippet): void
+    {
+        self::assertCodeContains($snippet, self::generateTypescript());
+    }
+
+    /**
+     * @return array
+     */
+    public function listExpectedSnippets(): array
+    {
+        return [
+            'CustomInputDTO' => [
+                <<<'TYPESCRIPT'
+export interface CustomInputDTO {
+  values: Array<number>;
+}
+TYPESCRIPT
+            ],
+            'OutputDTO' => [
+                <<<'TYPESCRIPT'
+export interface OutputDTO {
+  results: Array<string>;
+  success: boolean;
+}
+TYPESCRIPT
+            ],
+            'CustomItemMethod'       => ['public custom(iri: IRI<EntityWithCustomMethods>, body: CustomInputDTO, options: RequestOptions = {}): Observable<OutputDTO> {'],
+            'CustomCollectionMethod' => ['public customs(body: Array<CustomInputDTO>, options: RequestOptions = {}): Observable<Collection<OutputDTO>> {'],
+        ];
+    }
+}
-- 
GitLab