From ec620b1a67817184f488ebf66d681244a88c62bb Mon Sep 17 00:00:00 2001 From: Dan Barrett Date: Thu, 18 Mar 2021 00:30:10 +1100 Subject: [PATCH] [FEATURE] Basic Test Improvements (#2) * Test improvements and GitHub Actions improvements * Updated .gitattributes * Conflict with known bad versions of `tightenco/collect` --- .gitattributes | 21 ++++-- .github/workflows/continuous-integration.yml | 28 +++++-- .../Iter8LdapRecordExtensionTest.php | 75 ++++++++++++++++++- Tests/TestCase.php | 28 +++++++ composer.json | 19 +++++ docker-compose.yml | 14 ++++ phpcs.xml.dist | 5 ++ phpstan.neon.dist | 6 +- 8 files changed, 179 insertions(+), 17 deletions(-) create mode 100644 Tests/TestCase.php create mode 100644 docker-compose.yml diff --git a/.gitattributes b/.gitattributes index bfba612..d83e50c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,6 +1,15 @@ -.gitattributes export-ignore -.gitignore export-ignore -.php_cs.dist export-ignore -phpunit.xml.dist export-ignore -/Resources/doc export-ignore -/Tests export-ignore +* text=auto + +/.github export-ignore +.editorconfig export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.php_cs.dist export-ignore +.whitesource export-ignore +/Resources/doc export-ignore +/Tests export-ignore +docker-compose.yml export-ignore +phpcs.xml.dist export-ignore +phpstan.neon.dist export-ignore +phpunit.xml.dist export-ignore +psalm.xml.dist export-ignore diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 3333ea7..f422cee 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -5,7 +5,12 @@ name: "Continuous Integration" -on: [push, pull_request] +on: + push: + pull_request: + # Run the CI tasks weekly to catch any potential dependency regressions. + schedule: + - cron: '0 0 * * 1' jobs: phpunit: @@ -43,11 +48,22 @@ jobs: composer-flags: "--ignore-platform-reqs" symfony-require: "5.2.*" + services: + ldap: + image: bitnami/openldap + ports: + - 3389:3389 + env: + LDAP_ADMIN_USERNAME: admin + LDAP_ADMIN_PASSWORD: a_great_password + LDAP_ROOT: dc=local,dc=com + LDAP_PORT_NUMBER: 3389 + LDAP_USERS: a + LDAP_PASSWORDS: a + steps: - name: "Checkout" uses: "actions/checkout@v2" - with: - fetch-depth: 2 - name: "Install PHP with PCOV" uses: "shivammathur/setup-php@v2" @@ -64,8 +80,10 @@ jobs: uses: "actions/cache@v2" with: path: "~/.composer/cache" - key: "php-${{ matrix.php-version }}-symfony-${{ matrix.symfony-require }}-composer-locked-${{ hashFiles('composer.lock') }}" - restore-keys: "php-${{ matrix.php-version }}-symfony-${{ matrix.symfony-require }}-composer-locked-" + key: php-${{ matrix.php-version }}-symfony-${{ matrix.symfony-require }}-composer-locked-${{ hashFiles('composer.lock') }} + restore-keys: | + php-${{ matrix.php-version }}-symfony-${{ matrix.symfony-require }}-composer-locked- + php-${{ matrix.php-version }}- - name: "Install dependencies with composer" env: diff --git a/Tests/DependencyInjection/Iter8LdapRecordExtensionTest.php b/Tests/DependencyInjection/Iter8LdapRecordExtensionTest.php index b0c229f..551def2 100644 --- a/Tests/DependencyInjection/Iter8LdapRecordExtensionTest.php +++ b/Tests/DependencyInjection/Iter8LdapRecordExtensionTest.php @@ -6,8 +6,11 @@ use InvalidArgumentException; use Iter8\Bundle\LdapRecordBundle\DependencyInjection\Iter8LdapRecordExtension; -use PHPUnit\Framework\TestCase; +use Iter8\Bundle\LdapRecordBundle\Tests\TestCase; +use LdapRecord\Connection; +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; class Iter8LdapRecordExtensionTest extends TestCase { @@ -22,14 +25,80 @@ public function test_cannot_configure_tls_and_ssl_for_connection(): void $extension->load([array_merge($this->baseConfig(), ['use_ssl' => true, 'use_tls' => true])], $container); } + public function test_load_empty_configuration(): void + { + $this->expectException(InvalidConfigurationException::class); + + $container = $this->createContainer(); + $container->registerExtension(new Iter8LdapRecordExtension()); + $container->loadFromExtension('iter8_ldap_record'); + $container->compile(); + } + + public function test_load_valid_configuration(): void + { + $container = $this->createContainer(); + $container->registerExtension(new Iter8LdapRecordExtension()); + $container->loadFromExtension('iter8_ldap_record', $this->baseConfig()); + $container->compile(); + + self::assertTrue($container->getDefinition('iter8_ldap_record.connection')->isPublic()); + } + + public function test_is_connected_with_auto_connect_disabled(): void + { + $this->getLdapConfig(); + + $container = $this->createContainer(); + $container->registerExtension(new Iter8LdapRecordExtension()); + $container->loadFromExtension('iter8_ldap_record', $this->baseConfig()); + $container->compile(); + + /** @var Connection $connection */ + $connection = $container->get('iter8_ldap_record.connection'); + + self::assertFalse($connection->isConnected()); + } + + public function test_is_connected_with_auto_connect_enabled(): void + { + $this->getLdapConfig(); + + $config = array_merge( + $this->baseConfig(), + ['auto_connect' => true] + ); + + $container = $this->createContainer(); + $container->registerExtension(new Iter8LdapRecordExtension()); + $container->loadFromExtension('iter8_ldap_record', $config); + $container->compile(); + + /** @var Connection $connection */ + $connection = $container->get('iter8_ldap_record.connection'); + + self::assertTrue($connection->isConnected()); + } + private function baseConfig(): array { return [ - 'hosts' => ['example_host.local'], + 'hosts' => ['localhost'], 'base_dn' => 'dc=local,dc=com', 'username' => 'cn=admin,dc=local,dc=com', 'password' => 'a_great_password', - 'auto_connect' => true, + 'port' => 3389, ]; } + + private function createContainer(): ContainerBuilder + { + return new ContainerBuilder(new ParameterBag([ + 'kernel.cache_dir' => __DIR__, + 'kernel.project_dir' => __DIR__, + 'kernel.charset' => 'UTF-8', + 'kernel.debug' => false, + 'kernel.bundles' => ['Iter8LdapRecordBundle' => true], + ])); + } } diff --git a/Tests/TestCase.php b/Tests/TestCase.php new file mode 100644 index 0000000..c667bc0 --- /dev/null +++ b/Tests/TestCase.php @@ -0,0 +1,28 @@ + getenv('LDAP_HOST'), + 'port' => getenv('LDAP_PORT'), + ]; + } +} diff --git a/composer.json b/composer.json index 3117b0a..9d3f13b 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ }, "require": { "php": "^7.2 || ^8.0", + "ext-ldap": "*", "directorytree/ldaprecord": "^2.0", "symfony/framework-bundle": "^4.4 || ^5.0" }, @@ -35,6 +36,9 @@ "preferred-install": "dist", "sort-packages": true }, + "conflict": { + "tightenco/collect": "<5.6" + }, "autoload": { "psr-4": { "Iter8\\Bundle\\LdapRecordBundle\\": "" @@ -47,5 +51,20 @@ "psr-4": { "": "Tests/DependencyInjection" } + }, + "scripts": { + "ci": [ + "@phpcs", + "@phpunit", + "@phpstan", + "@psalm" + ], + "csf": "php-cs-fixer fix", + "csf-dry": "@csf --dry-run", + "phpcs": "phpcs", + "phpstan": "phpstan analyze", + "phpstan-max": "@phpstan --level=max", + "phpunit": "phpunit", + "psalm": "psalm --show-info=true" } } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..568296a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '2' + +services: + ldap: + image: bitnami/openldap + ports: + - 3389:3389 + environment: + - LDAP_ADMIN_USERNAME=admin + - LDAP_ADMIN_PASSWORD=a_great_password + - LDAP_USERS=a + - LDAP_PASSWORDS=a + - LDAP_ROOT=dc=local,dc=com + - LDAP_PORT_NUMBER=3389 diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 54b7f21..ec27d43 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -24,6 +24,7 @@ + @@ -39,6 +40,10 @@ Tests + + Tests + + Tests diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 2c907b7..541ed4e 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -5,12 +5,12 @@ parameters: checkMissingIterableValueType: false checkGenericClassInNonGenericObjectType: false paths: - - ./DependencyInjection - - ./Tests + - %currentWorkingDirectory%/DependencyInjection + - %currentWorkingDirectory%/Tests ignoreErrors: - message: '#.*NodeDefinition::children.*#' - path: ./DependencyInjection + path: %currentWorkingDirectory%/DependencyInjection includes: - vendor/phpstan/phpstan-strict-rules/rules.neon - vendor/phpstan/phpstan-phpunit/extension.neon