From 342a913c26c3673b91c0a825cd8945bd08e41a9d Mon Sep 17 00:00:00 2001 From: patryk Date: Mon, 22 Aug 2022 14:57:43 +0200 Subject: [PATCH] add php unit test --- .github/workflows/build.yml | 4 ++ composer.json | 6 +- phpunit.xml | 24 +++++++ tests/Application/config/bundles.php | 2 + .../test_product_attribute_repository.yaml | 9 +++ ...st_product_attribute_value_repository.yaml | 63 +++++++++++++++++++ .../Integration/IntegrationTestCase.php | 33 ++++++++++ .../ProductAttributeRepositoryTest.php | 39 ++++++++++++ .../ProductAttributeValueRepositoryTest.php | 44 +++++++++++++ 9 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/PHPUnit/Integration/DataFixtures/ORM/Repository/ProductAttributeValueRepositoryTest/test_product_attribute_repository.yaml create mode 100644 tests/PHPUnit/Integration/DataFixtures/ORM/Repository/ProductAttributeValueRepositoryTest/test_product_attribute_value_repository.yaml create mode 100644 tests/PHPUnit/Integration/IntegrationTestCase.php create mode 100644 tests/PHPUnit/Integration/Repository/ProductAttributeRepositoryTest.php create mode 100644 tests/PHPUnit/Integration/Repository/ProductAttributeValueRepositoryTest.php diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3f3183e..575a4ec 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -198,3 +198,7 @@ jobs: name: Behat logs path: etc/build/ if-no-files-found: ignore + + - + name: Run PHPUnit + run: vendor/bin/phpunit --colors=always diff --git a/composer.json b/composer.json index 094bcdc..688569b 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,8 @@ "symfony/web-profiler-bundle": "^4.4 || ^5.2", "vimeo/psalm": "4.7.1", "bitbag/coding-standard": "^1.0.1", - "polishsymfonycommunity/symfony-mocker-container": "^1.0" + "polishsymfonycommunity/symfony-mocker-container": "^1.0", + "lchrusciel/api-test-case": "^5.2" }, "conflict": { "symfony/form": "4.4.11 || 4.4.12" @@ -50,7 +51,8 @@ "autoload": { "psr-4": { "BitBag\\SyliusElasticsearchPlugin\\": "src/", - "Tests\\BitBag\\SyliusElasticsearchPlugin\\Application\\": "tests/Application" + "Tests\\BitBag\\SyliusElasticsearchPlugin\\Application\\": "tests/Application", + "Tests\\BitBag\\SyliusElasticsearchPlugin\\Integration\\": "tests/PHPUnit/Integration" } }, "autoload-dev": { diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..ea86c14 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,24 @@ + + + + + + tests/PHPUnit + + + + + + + + + + + + + + + diff --git a/tests/Application/config/bundles.php b/tests/Application/config/bundles.php index 519328b..cc48df1 100644 --- a/tests/Application/config/bundles.php +++ b/tests/Application/config/bundles.php @@ -58,4 +58,6 @@ return [ Sylius\Bundle\ApiBundle\SyliusApiBundle::class => ['all' => true], SyliusLabs\DoctrineMigrationsExtraBundle\SyliusLabsDoctrineMigrationsExtraBundle::class => ['all' => true], Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true], + Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['test' => true], + Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['test' => true], ]; diff --git a/tests/PHPUnit/Integration/DataFixtures/ORM/Repository/ProductAttributeValueRepositoryTest/test_product_attribute_repository.yaml b/tests/PHPUnit/Integration/DataFixtures/ORM/Repository/ProductAttributeValueRepositoryTest/test_product_attribute_repository.yaml new file mode 100644 index 0000000..bc6bb1c --- /dev/null +++ b/tests/PHPUnit/Integration/DataFixtures/ORM/Repository/ProductAttributeValueRepositoryTest/test_product_attribute_repository.yaml @@ -0,0 +1,9 @@ +Sylius\Component\Product\Model\ProductAttribute: + product_attribute_brand: + code: 't_shirt_brand' + type: 'text' + storage_type: 'text' + position: 1 + translatable: 0 + + diff --git a/tests/PHPUnit/Integration/DataFixtures/ORM/Repository/ProductAttributeValueRepositoryTest/test_product_attribute_value_repository.yaml b/tests/PHPUnit/Integration/DataFixtures/ORM/Repository/ProductAttributeValueRepositoryTest/test_product_attribute_value_repository.yaml new file mode 100644 index 0000000..774ea2a --- /dev/null +++ b/tests/PHPUnit/Integration/DataFixtures/ORM/Repository/ProductAttributeValueRepositoryTest/test_product_attribute_value_repository.yaml @@ -0,0 +1,63 @@ +Sylius\Component\Core\Model\Channel: + channel_web: + code: 'WEB' + name: 'Web Channel' + hostname: 'localhost' + description: 'Lorem ipsum' + baseCurrency: '@currency_usd' + defaultLocale: '@locale_en' + locales: ['@locale_en'] + color: 'black' + enabled: true + taxCalculationStrategy: 'order_items_based' + +Sylius\Component\Currency\Model\Currency: + currency_usd: + code: 'USD' + +Sylius\Component\Locale\Model\Locale: + locale_en: + code: 'en_US' + +Sylius\Component\Core\Model\Product: + product_mug: + code: 'MUG' + channels: ['@channel_web'] + currentLocale: 'en_US' + translations: + en_US: '@product_translation_mug' + + +Sylius\Component\Core\Model\ProductTranslation: + product_translation_mug: + slug: 'mug' + locale: 'en_US' + name: 'Mug' + description: '' + translatable: '@product_mug' + +Sylius\Component\Product\Model\ProductAttribute: + product_attribute_brand: + code: 't_shirt_brand' + type: 'text' + storage_type: 'text' + position: 1 + translatable: 0 + +Sylius\Component\Product\Model\ProductAttributeValue: + product_attribute_value_brand: + product: '@product_mug' + attribute: '@product_attribute_brand' + locale_code: '@locale_en' + value: 'You are breathtaking' + +Sylius\Component\Core\Model\Taxon: + mugs: + code: "mugs" + +Sylius\Component\Core\Model\ProductTaxon: + productTaxon1: + product: "@product_mug" + taxon: "@mugs" + position: 0 + diff --git a/tests/PHPUnit/Integration/IntegrationTestCase.php b/tests/PHPUnit/Integration/IntegrationTestCase.php new file mode 100644 index 0000000..312b380 --- /dev/null +++ b/tests/PHPUnit/Integration/IntegrationTestCase.php @@ -0,0 +1,33 @@ +dataFixturesPath = __DIR__ . '/DataFixtures/ORM'; + } + + protected function setUp(): void + { + + } + + protected function tearDown(): void + { + + } +} diff --git a/tests/PHPUnit/Integration/Repository/ProductAttributeRepositoryTest.php b/tests/PHPUnit/Integration/Repository/ProductAttributeRepositoryTest.php new file mode 100644 index 0000000..c387a0f --- /dev/null +++ b/tests/PHPUnit/Integration/Repository/ProductAttributeRepositoryTest.php @@ -0,0 +1,39 @@ +attributeRepository = self::$container->get('bitbag.sylius_elasticsearch_plugin.repository.product_attribute_repository'); + } + + public function test_get_attribute_type_by_name(): void + { + $this->loadFixturesFromFiles(['Repository/ProductAttributeValueRepositoryTest/test_product_attribute_repository.yaml']); + + $result = $this->attributeRepository->getAttributeTypeByName('t_shirt_brand'); + + $this->assertNotEmpty($result); + $this->assertIsString($result); + } +} diff --git a/tests/PHPUnit/Integration/Repository/ProductAttributeValueRepositoryTest.php b/tests/PHPUnit/Integration/Repository/ProductAttributeValueRepositoryTest.php new file mode 100644 index 0000000..8d32b37 --- /dev/null +++ b/tests/PHPUnit/Integration/Repository/ProductAttributeValueRepositoryTest.php @@ -0,0 +1,44 @@ +productAttributeValueRepository = self::$container->get('bitbag.sylius_elasticsearch_plugin.repository.product_attribute_value_repository'); + $this->attributeRepository = self::$container->get('sylius.repository.product_attribute'); + $this->taxonRepository = self::$container->get('sylius.repository.taxon'); + } + public function test_get_unique_attribute_values(): void + { + $this->loadFixturesFromFiles(['Repository/ProductAttributeValueRepositoryTest/test_product_attribute_value_repository.yaml']); + + $attribute = $this->attributeRepository->findAll()[0]; + $taxon = $this->taxonRepository->findAll()[0]; + + $result = $this->productAttributeValueRepository->getUniqueAttributeValues($attribute, $taxon); + + $this->assertNotEmpty($result); + $this->assertEquals(1, \count($result)); + } +}