From 338dab1552e96cdf0bc50318675602b54018694e Mon Sep 17 00:00:00 2001 From: robertfausk Date: Sun, 29 Mar 2020 14:16:32 +0200 Subject: [PATCH] Add tests and init configuration support Refs #3 Closes #1 * add docker-compose support * req --dev phpunit/phpunit matthiasnoback/symfony-config-test so that they support at least php 7.1 * add unit tests and one behat test with chrome * add PHP 7.4 to travis and adopt script section * init CHANGELOG.md --- .travis.yml | 14 ++-- CHANGELOG.md | 39 +++++++++ Dockerfile | 39 +++++++++ README.md | 47 ++++++++++- composer.json | 7 ++ docker-compose.yml | 38 +++++++++ phpunit.xml.dist | 23 ++++++ .../Driver/PantherFactory.php | 14 +--- .../ServiceContainer/PantherConfiguration.php | 47 +++++++++++ tests/Behat/behat.yml | 29 +++++++ tests/Behat/features/simple.feature | 6 ++ tests/Behat/public/index.php | 6 ++ tests/Unit/Driver/PantherFactoryTest.php | 75 ++++++++++++++++++ tests/Unit/PantherConfigurationTest.php | 79 +++++++++++++++++++ 14 files changed, 446 insertions(+), 17 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 Dockerfile create mode 100755 docker-compose.yml create mode 100644 phpunit.xml.dist create mode 100644 src/Robertfausk/Behat/PantherExtension/ServiceContainer/PantherConfiguration.php create mode 100644 tests/Behat/behat.yml create mode 100644 tests/Behat/features/simple.feature create mode 100644 tests/Behat/public/index.php create mode 100644 tests/Unit/Driver/PantherFactoryTest.php create mode 100644 tests/Unit/PantherConfigurationTest.php diff --git a/.travis.yml b/.travis.yml index 3411523..7d0eb09 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,16 +5,16 @@ addons: sudo: false -php: [7.1, 7.2, 7.3] +php: [7.1, 7.2, 7.3, 7.4] matrix: allow_failures: - env: DEPENDENCIES='dev' - - php: 7.3 + - php: 7.4 fast_finish: true include: # Test against dev versions of dependencies - - php: 7.3 + - php: 7.4 env: DEPENDENCIES='dev' cache: directories: @@ -27,5 +27,9 @@ install: - composer install --no-progress script: - - vendor/bin/phpspec run -f pretty - - vendor/bin/behat -fprogress --strict + - vendor/bin/phpunit -v --coverage-clover=coverage.clover + - vendor/bin/behat + +after_script: + - wget https://scrutinizer-ci.com/ocular.phar + - php ocular.phar code-coverage:upload --format=php-clover coverage.clover diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..09f1c99 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,39 @@ +1.0.1 / 2020- +================== + +Features: +* Enabled configuration per driver instance; usage examples with all sessions using mink-panther-driver: +```YAML +# in behat.yml + extensions: + Robertfausk\Behat\PantherExtension: ~ # no configuration here + Behat\MinkExtension: + javascript_session: javascript_chrome + sessions: + default: + panther: ~ + javascript: + panther: + options: ~ + javascript_chrome: + panther: + options: + browser: 'chrome' + webServerDir: '%paths.base%/public' + javascript_firefox: + panther: + options: + browser: 'firefox' +``` + +Testsuite: + +* Enabled travis at all with phpunit and behat for PHP 7.1-7.4 +* Added PHP 7.4 in the CI +* Added Unit Tests +* Added one simple scenario test with behat + +1.0.0 / 2019-08-16 +================== + +Initial Release :tada: diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b40000f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +ARG PHP_VERSION=7.2 + +FROM composer:latest as composer +FROM php:${PHP_VERSION}-cli + +# replace shell with bash so we can source files +RUN rm /bin/sh && ln -s /bin/bash /bin/sh + +RUN apt-get update && apt-get install -y \ + git-core + + +COPY --from=composer /usr/bin/composer /usr/bin/composer + +# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser +ENV COMPOSER_ALLOW_SUPERUSER 1 +ENV COMPOSER_HOME /home/docker/.composer +# contains dev-mode packages +RUN composer global require "hirak/prestissimo:^0.3" "sllh/composer-versions-check:^2.0" "pyrech/composer-changelogs:^1.7" --prefer-dist --no-progress --no-suggest --classmap-authoritative + +############################################################## +# add symfony/panther +############################################################## +RUN apt-get update && apt-get install -y libzip-dev zlib1g-dev unzip chromium && docker-php-ext-install zip +ENV PANTHER_NO_SANDBOX 1 + +############################################################## +# add gd +############################################################## + +RUN apt-get update && apt-get install -y \ + libjpeg-dev \ + libpng-dev + +RUN docker-php-ext-configure gd --with-jpeg-dir=/usr/include/ && \ + docker-php-ext-install gd + +WORKDIR /var/www/html +COPY . /var/www/html diff --git a/README.md b/README.md index a16cfe1..bf755fa 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,53 @@ Symfony Panther extension for Behat composer require --dev robertfausk/behat-panther-extension ``` +## Usage example + +* Add ```Robertfausk\Behat\PantherExtension: ~``` to your behat.yml. +* Use ```panther``` session in ```Behat\MinkExtension```. +* The extension will use options of ```symfony/panther``` by default. +Have a look at ```PantherTestCaseTrait::$defaultOptions``` for this. +* Following are some examples with all sessions using mink-panther-driver: + ```YAML + # in behat.yml + extensions: + Robertfausk\Behat\PantherExtension: ~ # no configuration here + Behat\MinkExtension: + javascript_session: javascript_chrome + sessions: + default: + panther: ~ + javascript: + panther: + options: ~ + javascript_chrome: + panther: + options: + browser: 'chrome' + webServerDir: '%paths.base%/public' # your custom public dir + javascript_firefox: + panther: + options: + browser: 'firefox' + ``` + +### How to upgrade? + + Have a look at [CHANGELOG](CHANGELOG.md) for detailed information. + +## How to contribute? + +Start docker-compose with php version of your choice. Currently there are php7.2, php7.3 and php7.4. + + docker-compose up php7.2 + +Run phpunit tests + + docker-compose run php7.2 vendor/bin/phpunit + ## Credits Created by Robert Freigang [robertfausk](https://github.com/robertfausk). -MinkPantherDriver is built on top of [Panther](https://github.com/symfony/panther) and for usage with [Behat and Mink](http://behat.org/en/latest/cookbooks/integrating_symfony2_with_behat.html#initialising-behat). +BehatPantherExtension is built on top of [symfony/panther](https://github.com/symfony/panther) and [robertfausk/mink-panther-driver]([Panther](https://github.com/robertfausk/mink-panther-driver)). +It is for usage with [Behat and Mink](http://behat.org/en/latest/cookbooks/integrating_symfony2_with_behat.html#initialising-behat). diff --git a/composer.json b/composer.json index 90cc59e..478d0ab 100644 --- a/composer.json +++ b/composer.json @@ -26,12 +26,19 @@ "symfony/config": "^3.4|^4.0" }, "require-dev": { + "phpunit/phpunit": "~7.5", + "matthiasnoback/symfony-config-test": "^4.1" }, "autoload": { "psr-0": { "Robertfausk\\Behat\\PantherExtension": "src/" } }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, "extra": { "branch-alias": { "dev-master": "1.0.x-dev" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 0000000..3bba118 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,38 @@ +version: "3" +services: + + php7.1: + tty: true + build: + context: . + args: + - PHP_VERSION=7.1 + volumes: + - .:/var/www/html + + php7.2: + tty: true + build: + context: . + args: + - PHP_VERSION=7.2 + volumes: + - .:/var/www/html + + php7.3: + tty: true + build: + context: . + args: + - PHP_VERSION=7.3 + volumes: + - .:/var/www/html + + php7.4: + tty: true + build: + context: . + args: + - PHP_VERSION=7.4 + volumes: + - .:/var/www/html diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..3e081bc --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + ./tests + + + + + + ./src + + + diff --git a/src/Robertfausk/Behat/PantherExtension/ServiceContainer/Driver/PantherFactory.php b/src/Robertfausk/Behat/PantherExtension/ServiceContainer/Driver/PantherFactory.php index 9d43072..b7355c3 100644 --- a/src/Robertfausk/Behat/PantherExtension/ServiceContainer/Driver/PantherFactory.php +++ b/src/Robertfausk/Behat/PantherExtension/ServiceContainer/Driver/PantherFactory.php @@ -4,6 +4,7 @@ namespace Robertfausk\Behat\PantherExtension\ServiceContainer\Driver; use Behat\MinkExtension\ServiceContainer\Driver\DriverFactory; +use Robertfausk\Behat\PantherExtension\ServiceContainer\PantherConfiguration; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\DependencyInjection\Definition; @@ -33,17 +34,8 @@ public function supportsJavascript() */ public function configure(ArrayNodeDefinition $builder) { - $builder - ->children() - ->arrayNode('options') - ->useAttributeAsKey('key') - ->prototype('variable')->end() - ->info( - "These are the options passed as first argument to PantherTestCaseTrait::createPantherClient client constructor." - ) - ->end() - ->end() - ; + $configuration = new PantherConfiguration(); + $builder->append($configuration->addOptionsNode()); } /** diff --git a/src/Robertfausk/Behat/PantherExtension/ServiceContainer/PantherConfiguration.php b/src/Robertfausk/Behat/PantherExtension/ServiceContainer/PantherConfiguration.php new file mode 100644 index 0000000..75c89bc --- /dev/null +++ b/src/Robertfausk/Behat/PantherExtension/ServiceContainer/PantherConfiguration.php @@ -0,0 +1,47 @@ + + */ +class PantherConfiguration implements ConfigurationInterface +{ + /** + * @inheritDoc + */ + public function getConfigTreeBuilder() + { + $treeBuilder = new TreeBuilder('panther'); + if (\method_exists($treeBuilder, 'getRootNode')) { + $root = $treeBuilder->getRootNode(); + } else { + $root = $treeBuilder->root('panther'); + } + + $root->append($this->addOptionsNode()); + + return $treeBuilder; + } + + public function addOptionsNode(): ArrayNodeDefinition + { + $treeBuilder = new TreeBuilder('options'); + + $node = $treeBuilder->getRootNode() + ->info( + "These are the options passed as first argument to PantherTestCaseTrait::createPantherClient client constructor." + ) + ->ignoreExtraKeys() + ->scalarPrototype() + ->end() + ; + + return $node; + } +} diff --git a/tests/Behat/behat.yml b/tests/Behat/behat.yml new file mode 100644 index 0000000..a84972a --- /dev/null +++ b/tests/Behat/behat.yml @@ -0,0 +1,29 @@ +default: + suites: + web: + paths: + - '%paths.base%/features' + contexts: + - Behat\MinkExtension\Context\MinkContext + extensions: + Robertfausk\Behat\PantherExtension: ~ # no configuration here + Behat\MinkExtension: + browser_name: chrome + javascript_session: javascript_chrome + sessions: + default: + panther: + options: + webServerDir: '%paths.base%/public' + javascript: + panther: + options: ~ + javascript_chrome: + panther: + options: + browser: 'chrome' + webServerDir: '%paths.base%/public' + javascript_firefox: + panther: + options: + browser: 'firefox' diff --git a/tests/Behat/features/simple.feature b/tests/Behat/features/simple.feature new file mode 100644 index 0000000..60c0095 --- /dev/null +++ b/tests/Behat/features/simple.feature @@ -0,0 +1,6 @@ +Feature: Just a simple call to a php script + + @javascript_chrome + Scenario: I am on a php and see the echo response + And I am on "index.php" + Then I should see "Huhuuu!" diff --git a/tests/Behat/public/index.php b/tests/Behat/public/index.php new file mode 100644 index 0000000..27ce14c --- /dev/null +++ b/tests/Behat/public/index.php @@ -0,0 +1,6 @@ +'; + +echo ''; diff --git a/tests/Unit/Driver/PantherFactoryTest.php b/tests/Unit/Driver/PantherFactoryTest.php new file mode 100644 index 0000000..e4fdea6 --- /dev/null +++ b/tests/Unit/Driver/PantherFactoryTest.php @@ -0,0 +1,75 @@ + + */ +class PantherFactoryTest extends TestCase +{ + public function test_support_javascript_is_true(): void + { + $pantherFactory = new PantherFactory(); + $this->assertTrue($pantherFactory->supportsJavascript()); + } + + public function test_driver_name_is_panther(): void + { + $pantherFactory = new PantherFactory(); + $this->assertSame('panther', $pantherFactory->getDriverName()); + } + + public function test_build_driver_with_default_options(): void + { + /** @see \Symfony\Component\Panther\PantherTestCaseTrait::$defaultOptions **/ + $options = [ + 'webServerDir' => __DIR__.'/../../../../public', + 'hostname' => '127.0.0.1', + 'port' => 9080, + 'router' => '', + 'external_base_uri' => null, + 'readinessPath' => '', + 'browser' => 'chrome', + ]; + + $config = [ + 'options' => $options, + ]; + $pantherFactory = new PantherFactory(); + $definition = $pantherFactory->buildDriver($config); + $arguments = $definition->getArguments(); + + $this->assertArrayHasKey(0, $arguments, 'Arguments of definition should not be empty.'); + $this->assertSame($options, $arguments[0]); + } + + public function test_build_driver_with_empty_options(): void + { + $options = []; + + $config = [ + 'options' => $options, + ]; + $pantherFactory = new PantherFactory(); + $definition = $pantherFactory->buildDriver($config); + $arguments = $definition->getArguments(); + + $this->assertArrayHasKey(0, $arguments, 'Arguments of definition should not be empty.'); + $this->assertSame($options, $arguments[0]); + } + + public function test_build_driver_without_options(): void + { + $config = []; + $pantherFactory = new PantherFactory(); + $definition = $pantherFactory->buildDriver($config); + $arguments = $definition->getArguments(); + + $this->assertArrayHasKey(0, $arguments, 'Arguments of definition should not be empty.'); + $this->assertSame(null, $arguments[0]); + } +} diff --git a/tests/Unit/PantherConfigurationTest.php b/tests/Unit/PantherConfigurationTest.php new file mode 100644 index 0000000..c5abd90 --- /dev/null +++ b/tests/Unit/PantherConfigurationTest.php @@ -0,0 +1,79 @@ + + */ +class PantherConfigurationTest extends TestCase +{ + use ConfigurationTestCaseTrait; + + public function validConfigurationProvider(): array + { + return [ + 'empty options' => [['options' => []]], + ]; + } + + public function invalidConfigurationProvider(): array + { + return [ + 'misspelled options' => [['opions' => []], 'Unrecognized option "opions"'], + ]; + } + + /** + * @dataProvider invalidConfigurationProvider + * + * @param array $configurationOptions + * @param string $message + */ + public function test_invalid_configuration(array $configurationOptions, string $message): void + { + $this->assertConfigurationIsInvalid( + [ + $configurationOptions, + ], + $message + ); + } + + /** + * @dataProvider validConfigurationProvider + * + * @param array $configurationOptions + */ + public function test_valid_configuration(array $configurationOptions): void + { + $this->assertConfigurationIsValid( + [ + $configurationOptions, + ] + ); + } + + public function test_processed_configuration(): void + { + $this->assertProcessedConfigurationEquals( + [ + ['options' => ['hostname' => '127.0.0.1']], + ], + [ + 'options' => [ + 'hostname' => '127.0.0.1', + ], + ] + ); + } + + protected function getConfiguration(): PantherConfiguration + { + return new PantherConfiguration(); + } +}