diff --git a/.travis.yml b/.travis.yml index fa423c39..2ca1fe8d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,6 +52,7 @@ script: - composer validate --strict - bin/phpspec run + - bin/phpunit - bin/behat --strict -vvv --no-interaction || bin/behat --strict -vvv --no-interaction --rerun --tags="~@todo" after_failure: diff --git a/composer.json b/composer.json index 497e7c24..e7177fd7 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "lakion/mink-debug-extension": "^1.2.3", "se/selenium-server-standalone": "^2.52", "symplify/easy-coding-standard": "^2.4", - "sylius-labs/coding-standard": "^1.0" + "sylius-labs/coding-standard": "^1.0", + "matthiasnoback/symfony-config-test": "^2.1.0" }, "prefer-stable": true, "minimum-stability": "alpha", @@ -40,4 +41,4 @@ "config": { "bin-dir": "bin" } -} \ No newline at end of file +} diff --git a/src/Fixture/BlockFixture.php b/src/Fixture/BlockFixture.php index eebfbd02..bea0aad4 100644 --- a/src/Fixture/BlockFixture.php +++ b/src/Fixture/BlockFixture.php @@ -130,7 +130,7 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void ->prototype('array') ->children() ->scalarNode('type')->isRequired()->cannotBeEmpty()->end() - ->scalarNode('enabled')->defaultTrue()->end() + ->booleanNode('enabled')->defaultTrue()->end() ->arrayNode('translations') ->prototype('array') ->children() diff --git a/src/Fixture/FrequentlyAskedQuestionFixture.php b/src/Fixture/FrequentlyAskedQuestionFixture.php index c04f9c01..4fe44434 100644 --- a/src/Fixture/FrequentlyAskedQuestionFixture.php +++ b/src/Fixture/FrequentlyAskedQuestionFixture.php @@ -107,8 +107,8 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void ->arrayNode('frequently_asked_questions') ->prototype('array') ->children() - ->scalarNode('enabled')->defaultTrue()->end() - ->scalarNode('position')->defaultNull()->end() + ->booleanNode('enabled')->defaultTrue()->end() + ->integerNode('position')->defaultNull()->end() ->arrayNode('translations') ->prototype('array') ->children() diff --git a/src/Fixture/PageFixture.php b/src/Fixture/PageFixture.php index e5863eab..3620fb4e 100644 --- a/src/Fixture/PageFixture.php +++ b/src/Fixture/PageFixture.php @@ -109,7 +109,7 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void ->arrayNode('pages') ->prototype('array') ->children() - ->scalarNode('enabled')->defaultTrue()->end() + ->booleanNode('enabled')->defaultTrue()->end() ->arrayNode('translations') ->prototype('array') ->children() diff --git a/src/Fixture/SectionFixture.php b/src/Fixture/SectionFixture.php new file mode 100644 index 00000000..9482af81 --- /dev/null +++ b/src/Fixture/SectionFixture.php @@ -0,0 +1,118 @@ + + */ +final class SectionFixture extends AbstractFixture implements FixtureInterface +{ + /** + * @var FactoryInterface + */ + private $sectionFactory; + + /** + * @var FactoryInterface + */ + private $sectionTranslationFactory; + + /** + * @var SectionRepositoryInterface + */ + private $sectionRepository; + + /** + * @param FactoryInterface $sectionFactory + * @param FactoryInterface $sectionTranslationFactory + * @param SectionRepositoryInterface $sectionRepository + */ + public function __construct( + FactoryInterface $sectionFactory, + FactoryInterface $sectionTranslationFactory, + SectionRepositoryInterface $sectionRepository + ) + { + $this->sectionFactory = $sectionFactory; + $this->sectionTranslationFactory = $sectionTranslationFactory; + $this->sectionRepository = $sectionRepository; + } + + /** + * {@inheritDoc} + */ + public function load(array $options): void + { + foreach ($options['sections'] as $code => $fields) { + + if (null !== $this->sectionRepository->findOneBy(['code' => $code])) { + continue; + } + + /** @var SectionInterface $section */ + $section = $this->sectionFactory->createNew(); + + $section->setCode($code); + + foreach ($fields['translations'] as $localeCode => $translation) { + /** @var SectionTranslationInterface $sectionTranslation */ + $sectionTranslation = $this->sectionTranslationFactory->createNew(); + + $sectionTranslation->setName($translation['name']); + + $section->addTranslation($sectionTranslation); + } + + $this->sectionRepository->add($section); + } + } + + /** + * {@inheritDoc} + */ + public function getName(): string + { + return 'bitbag_cms_section'; + } + + /** + * {@inheritDoc} + */ + protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void + { + $optionsNode + ->children() + ->arrayNode('sections') + ->prototype('array') + ->children() + ->arrayNode('translations') + ->prototype('array') + ->children() + ->scalarNode('name')->defaultNull()->end() + ->end() + ->end() + ->end() + ->end() + ->end() + ->end() + ->end(); + } +} diff --git a/src/Resources/config/fixture.yml b/src/Resources/config/fixture.yml deleted file mode 100644 index 3c84cb6c..00000000 --- a/src/Resources/config/fixture.yml +++ /dev/null @@ -1,2 +0,0 @@ -imports: - - { resource: "@BitBagCmsPlugin/Resources/config/fixture/block.yml" } \ No newline at end of file diff --git a/src/Resources/config/services/fixture.yml b/src/Resources/config/services/fixture.yml index 3d1093b4..fa25963f 100644 --- a/src/Resources/config/services/fixture.yml +++ b/src/Resources/config/services/fixture.yml @@ -24,5 +24,14 @@ services: - "@bitbag.factory.frequently_asked_question" - "@bitbag.factory.frequently_asked_question_translation" - "@bitbag.repository.frequently_asked_question" + tags: + - { name: sylius_fixtures.fixture } + + bitbag.fixture.section: + class: BitBag\CmsPlugin\Fixture\SectionFixture + arguments: + - "@bitbag.factory.section" + - "@bitbag.factory.section_translation" + - "@bitbag.repository.section" tags: - { name: sylius_fixtures.fixture } \ No newline at end of file diff --git a/tests/Fixture/BlockFixtureTest.php b/tests/Fixture/BlockFixtureTest.php new file mode 100644 index 00000000..28bdf4eb --- /dev/null +++ b/tests/Fixture/BlockFixtureTest.php @@ -0,0 +1,289 @@ + + */ +final class BlockFixtureTest extends \PHPUnit_Framework_TestCase +{ + use ConfigurationTestCaseTrait; + + /** + * @test + */ + public function blocks_are_optional(): void + { + $this->assertConfigurationIsValid([[]], 'blocks'); + } + + /** + * @test + */ + public function blocks_enabled_is_optional_but_must_be_boolean(): void + { + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'enabled' => true + ] + ] + ] + ], 'blocks.*.enabled'); + + $this->assertPartialConfigurationIsInvalid([ + [ + 'blocks' => [ + 'blocks_1' => [ + 'enabled' => 'boolean' + ] + ] + ] + ], 'blocks.*.enabled'); + } + + /** + * @test + */ + public function blocks_type_is_required_and_cannot_be_empty(): void + { + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'type' => true + ] + ] + ] + ], 'blocks.*.type'); + + $this->assertPartialConfigurationIsInvalid([ + [ + 'blocks' => [ + 'blocks_1' => [ + 'type' => '' + ] + ] + ] + ], 'blocks.*.type'); + + $this->assertPartialConfigurationIsInvalid([ + [ + 'blocks' => [ + 'blocks_1' => [] + ] + ] + ], 'blocks.*.type'); + } + + + /** + * @test + */ + public function blocks_translations_is_optional_but_must_be_array(): void + { + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'translations' => [] + ] + ] + ] + ], 'blocks.*.translations'); + + $this->assertPartialConfigurationIsInvalid([ + [ + 'blocks' => [ + 'blocks_1' => [ + 'translations' => '' + ] + ] + ] + ], 'blocks.*.translations'); + + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'blocks_1' => [] + ] + ] + ], 'blocks.*.translations'); + } + + /** + * @test + */ + public function blocks_may_contain_name(): void + { + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'translations' => [ + 'en_US' => [ + 'name' => 'block' + ] + ] + ] + ] + ] + ], 'blocks.*.translations.*.name'); + + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'translations' => [ + 'en_US' => [ + 'name' => '' + ] + ] + ] + ] + ] + ], 'blocks.*.translations.*.name'); + } + + /** + * @test + */ + public function blocks_may_contain_content(): void + { + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'translations' => [ + 'en_US' => [ + 'content' => 'block' + ] + ] + ] + ] + ] + ], 'blocks.*.translations.*.content'); + + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'translations' => [ + 'en_US' => [ + 'content' => '' + ] + ] + ] + ] + ] + ], 'blocks.*.translations.*.content'); + } + + /** + * @test + */ + public function blocks_may_contain_link(): void + { + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'translations' => [ + 'en_US' => [ + 'link' => 'block' + ] + ] + ] + ] + ] + ], 'blocks.*.translations.*.link'); + + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'translations' => [ + 'en_US' => [ + 'link' => '' + ] + ] + ] + ] + ] + ], 'blocks.*.translations.*.link'); + } + + /** + * @test + */ + public function blocks_may_contain_image_path(): void + { + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'translations' => [ + 'en_US' => [ + 'image_path' => '/path/to/img' + ] + ] + ] + ] + ] + ], 'blocks.*.translations.*.image_path'); + + $this->assertConfigurationIsValid([ + [ + 'blocks' => [ + 'block_1' => [ + 'translations' => [ + 'en_US' => [ + 'image_path' => '' + ] + ] + ] + ] + ] + ], 'blocks.*.translations.*.image_path'); + } + + /** + * {@inheritdoc} + */ + protected function getConfiguration(): BlockFixture + { + /** @var BlockFactoryInterface $blockFactory */ + $blockFactory = $this->getMockBuilder(BlockFactoryInterface::class)->getMock(); + /** @var FactoryInterface $blockTranslationFactory */ + $blockTranslationFactory = $this->getMockBuilder(FactoryInterface::class)->getMock(); + /** @var BlockRepositoryInterface $blockRepository */ + $blockRepository = $this->getMockBuilder(BlockRepositoryInterface::class)->getMock(); + /** @var ImageUploaderInterface $imageUploader */ + $imageUploader = $this->getMockBuilder(ImageUploaderInterface::class)->getMock(); + + return new BlockFixture( + $blockFactory, + $blockTranslationFactory, + $blockRepository, + $imageUploader + ); + } +} diff --git a/tests/Fixture/FrequentlyAskedQuestionFixtureTest.php b/tests/Fixture/FrequentlyAskedQuestionFixtureTest.php new file mode 100644 index 00000000..3363b0cc --- /dev/null +++ b/tests/Fixture/FrequentlyAskedQuestionFixtureTest.php @@ -0,0 +1,199 @@ + + */ +final class FrequentlyAskedQuestionFixtureTest extends \PHPUnit_Framework_TestCase +{ + use ConfigurationTestCaseTrait; + + /** + * @test + */ + public function frequently_asked_questions_are_optional(): void + { + $this->assertConfigurationIsValid([[]], 'frequently_asked_questions'); + } + + /** + * @test + */ + public function frequently_asked_questions_enabled_is_optional_but_must_be_boolean(): void + { + $this->assertConfigurationIsValid([ + [ + 'frequently_asked_questions' => [ + 'faq_1' => [ + 'enabled' => true + ] + ] + ] + ], 'frequently_asked_questions.*.enabled'); + + $this->assertPartialConfigurationIsInvalid([ + [ + 'frequently_asked_questions' => [ + 'faq_1' => [ + 'enabled' => 'boolean' + ] + ] + ] + ], 'frequently_asked_questions.*.enabled'); + } + + /** + * @test + */ + public function frequently_asked_questions_position_is_optional_but_must_be_integer(): void + { + $this->assertConfigurationIsValid([ + [ + 'frequently_asked_questions' => [ + 'faq_1' => [ + 'position' => 1 + ] + ] + ] + ], 'frequently_asked_questions.*.position'); + + $this->assertPartialConfigurationIsInvalid([ + [ + 'frequently_asked_questions' => [ + 'faq_1' => [ + 'position' => '1' + ] + ] + ] + ], 'frequently_asked_questions.*.position'); + } + + /** + * @test + */ + public function frequently_asked_questions_translations_is_optional_but_must_be_array(): void + { + $this->assertConfigurationIsValid([ + [ + 'frequently_asked_questions' => [ + 'faq_1' => [ + 'translations' => [] + ] + ] + ] + ], 'frequently_asked_questions.*.translations'); + + $this->assertPartialConfigurationIsInvalid([ + [ + 'frequently_asked_questions' => [ + 'faq_1' => [ + 'translations' => 'array' + ] + ] + ] + ], 'frequently_asked_questions.*.translations'); + } + + /** + * @test + */ + public function frequently_asked_questions_may_contain_question(): void + { + $this->assertConfigurationIsValid([ + [ + 'frequently_asked_questions' => [ + 'faq_1' => [ + 'translations' => [ + 'en_US' => [ + 'question' => 'Example question ?' + ] + ] + ] + ] + ] + ], 'frequently_asked_questions.*.translations.*.question'); + + $this->assertConfigurationIsValid([ + [ + 'frequently_asked_questions' => [ + 'faq_1' => [ + 'translations' => [ + 'en_US' => [ + 'question' => '' + ] + ] + ] + ] + ] + ], 'frequently_asked_questions.*.translations.*.question'); + } + + /** + * @test + */ + public function frequently_asked_questions_may_contain_answer(): void + { + $this->assertConfigurationIsValid([ + [ + 'frequently_asked_questions' => [ + 'faq_1' => [ + 'translations' => [ + 'en_US' => [ + 'answer' => 'Example answer' + ] + ] + ] + ] + ] + ], 'frequently_asked_questions.*.translations.*.answer'); + + $this->assertConfigurationIsValid([ + [ + 'frequently_asked_questions' => [ + 'faq_1' => [ + 'translations' => [ + 'en_US' => [ + 'answer' => '' + ] + ] + ] + ] + ] + ], 'frequently_asked_questions.*.translations.*.answer'); + } + + /** + * {@inheritdoc} + */ + protected function getConfiguration(): FrequentlyAskedQuestionFixture + { + /** @var FactoryInterface $frequentlyAskedQuestionFactory */ + $frequentlyAskedQuestionFactory = $this->getMockBuilder(FactoryInterface::class)->getMock(); + /** @var FactoryInterface $frequentlyAskedQuestionFactoryTranslation */ + $frequentlyAskedQuestionFactoryTranslation = $this->getMockBuilder(FactoryInterface::class)->getMock(); + /** @var FrequentlyAskedQuestionRepositoryInterface $frequentlyAskedQuestionRepository */ + $frequentlyAskedQuestionRepository = $this->getMockBuilder(FrequentlyAskedQuestionRepositoryInterface::class)->getMock(); + + return new FrequentlyAskedQuestionFixture( + $frequentlyAskedQuestionFactory, + $frequentlyAskedQuestionFactoryTranslation, + $frequentlyAskedQuestionRepository + ); + } +} diff --git a/tests/Fixture/PageFixtureTest.php b/tests/Fixture/PageFixtureTest.php new file mode 100644 index 00000000..91a17276 --- /dev/null +++ b/tests/Fixture/PageFixtureTest.php @@ -0,0 +1,275 @@ + + */ +final class PageFixtureTest extends \PHPUnit_Framework_TestCase +{ + use ConfigurationTestCaseTrait; + + /** + * @test + */ + public function pages_are_optional(): void + { + $this->assertConfigurationIsValid([[]], 'pages'); + } + + /** + * @test + */ + public function pages_enabled_is_optional_but_must_be_boolean(): void + { + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'enabled' => true + ] + ] + ] + ], 'pages.*.enabled'); + + $this->assertPartialConfigurationIsInvalid([ + [ + 'pages' => [ + 'page_1' => [ + 'enabled' => 'boolean' + ] + ] + ] + ], 'pages.*.enabled'); + } + + /** + * @test + */ + public function pages_translations_is_optional_but_must_be_array(): void + { + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [] + ] + ] + ] + ], 'pages.*.translations'); + + $this->assertPartialConfigurationIsInvalid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => 'array' + ] + ] + ] + ], 'pages.*.translations'); + } + + /** + * @test + */ + public function pages_may_contain_slug(): void + { + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [ + 'en_US' => [ + 'slug' => 'my-page' + ] + ] + ] + ] + ] + ], 'pages.*.translations.*.slug'); + + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [ + 'en_US' => [ + 'slug' => '' + ] + ] + ] + ] + ] + ], 'pages.*.translations.*.slug'); + } + + /** + * @test + */ + public function pages_may_contain_name(): void + { + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [ + 'en_US' => [ + 'name' => 'My page' + ] + ] + ] + ] + ] + ], 'pages.*.translations.*.name'); + + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [ + 'en_US' => [ + 'name' => '' + ] + ] + ] + ] + ] + ], 'pages.*.translations.*.name'); + } + + /** + * @test + */ + public function pages_may_contain_meta_keywords(): void + { + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [ + 'en_US' => [ + 'meta_keywords' => 'page' + ] + ] + ] + ] + ] + ], 'pages.*.translations.*.meta_keywords'); + + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [ + 'en_US' => [ + 'meta_keywords' => '' + ] + ] + ] + ] + ] + ], 'pages.*.translations.*.meta_keywords'); + } + + /** + * @test + */ + public function pages_may_contain_meta_description(): void + { + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [ + 'en_US' => [ + 'meta_description' => 'My page' + ] + ] + ] + ] + ] + ], 'pages.*.translations.*.meta_description'); + + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [ + 'en_US' => [ + 'meta_description' => 'My page' + ] + ] + ] + ] + ] + ], 'pages.*.translations.*.meta_description'); + } + + /** + * @test + */ + public function pages_may_contain_content(): void + { + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [ + 'en_US' => [ + 'content' => 'My page' + ] + ] + ] + ] + ] + ], 'pages.*.translations.*.content'); + + $this->assertConfigurationIsValid([ + [ + 'pages' => [ + 'page_1' => [ + 'translations' => [ + 'en_US' => [ + 'content' => 'My page' + ] + ] + ] + ] + ] + ], 'pages.*.translations.*.content'); + } + + /** + * {@inheritdoc} + */ + protected function getConfiguration(): PageFixture + { + /** @var FactoryInterface $pageFactory */ + $pageFactory = $this->getMockBuilder(FactoryInterface::class)->getMock(); + /** @var FactoryInterface $pageTranslationFactory */ + $pageTranslationFactory = $this->getMockBuilder(FactoryInterface::class)->getMock(); + /** @var PageRepositoryInterface $pageRepository */ + $pageRepository = $this->getMockBuilder(PageRepositoryInterface::class)->getMock(); + + return new PageFixture( + $pageFactory, + $pageTranslationFactory, + $pageRepository + ); + } +} diff --git a/tests/Fixture/SectionFixtureTest.php b/tests/Fixture/SectionFixtureTest.php new file mode 100644 index 00000000..722a1bf2 --- /dev/null +++ b/tests/Fixture/SectionFixtureTest.php @@ -0,0 +1,113 @@ + + */ +final class SectionFixtureTest extends \PHPUnit_Framework_TestCase +{ + use ConfigurationTestCaseTrait; + + /** + * @test + */ + public function sections_are_optional(): void + { + $this->assertConfigurationIsValid([[]], 'sections'); + } + + /** + * @test + */ + public function sections_translations_is_optional_but_must_be_array(): void + { + $this->assertConfigurationIsValid([ + [ + 'sections' => [ + 'blog' => [ + 'translations' => [] + ] + ] + ] + ], 'sections.*.translations'); + + $this->assertPartialConfigurationIsInvalid([ + [ + 'sections' => [ + 'blog' => [ + 'translations' => 'array' + ] + ] + ] + ], 'sections.*.translations'); + } + + /** + * @test + */ + public function sections_may_contain_name(): void + { + $this->assertConfigurationIsValid([ + [ + 'sections' => [ + 'blog' => [ + 'translations' => [ + 'en_US' => [ + 'name' => 'Blog' + ] + ] + ] + ] + ] + ], 'sections.*.translations.*.name'); + + $this->assertConfigurationIsValid([ + [ + 'sections' => [ + 'blog' => [ + 'translations' => [ + 'en_US' => [ + 'name' => '' + ] + ] + ] + ] + ] + ], 'sections.*.translations.*.name'); + } + + /** + * {@inheritdoc} + */ + protected function getConfiguration(): SectionFixture + { + /** @var FactoryInterface $sectionFactory */ + $sectionFactory = $this->getMockBuilder(FactoryInterface::class)->getMock(); + /** @var FactoryInterface $sectionTranslationFactory */ + $sectionTranslationFactory = $this->getMockBuilder(FactoryInterface::class)->getMock(); + /** @var SectionRepositoryInterface $sectionRepository */ + $sectionRepository = $this->getMockBuilder(SectionRepositoryInterface::class)->getMock(); + + return new SectionFixture( + $sectionFactory, + $sectionTranslationFactory, + $sectionRepository + ); + } +}