Skip to content

Commit

Permalink
[FEATURE] Basic Test Improvements (#2)
Browse files Browse the repository at this point in the history
* Test improvements and GitHub Actions improvements
* Updated .gitattributes
* Conflict with known bad versions of `tightenco/collect`
  • Loading branch information
yesdevnull committed Mar 17, 2021
1 parent 400436f commit ec620b1
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 17 deletions.
21 changes: 15 additions & 6 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
28 changes: 23 additions & 5 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand All @@ -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:
Expand Down
75 changes: 72 additions & 3 deletions Tests/DependencyInjection/Iter8LdapRecordExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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],
]));
}
}
28 changes: 28 additions & 0 deletions Tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Iter8\Bundle\LdapRecordBundle\Tests;

use PHPUnit\Framework\TestCase as PHPUnitTestCase;

class TestCase extends PHPUnitTestCase
{
protected function getLdapConfig(): array
{
/** @var resource|null $h */
$h = @ldap_connect((string) getenv('LDAP_HOST'), (int) getenv('LDAP_PORT'));
@ldap_set_option($h, \LDAP_OPT_PROTOCOL_VERSION, 3);

if (!\is_resource($h) || !@ldap_bind($h)) {
self::markTestSkipped('No server is listening on LDAP_HOST:LDAP_PORT');
}

ldap_unbind($h);

return [
'host' => getenv('LDAP_HOST'),
'port' => getenv('LDAP_PORT'),
];
}
}
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"require": {
"php": "^7.2 || ^8.0",
"ext-ldap": "*",
"directorytree/ldaprecord": "^2.0",
"symfony/framework-bundle": "^4.4 || ^5.0"
},
Expand All @@ -35,6 +36,9 @@
"preferred-install": "dist",
"sort-packages": true
},
"conflict": {
"tightenco/collect": "<5.6"
},
"autoload": {
"psr-4": {
"Iter8\\Bundle\\LdapRecordBundle\\": ""
Expand All @@ -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"
}
}
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<exclude name="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator.RequiredNullCoalesceEqualOperator"/> <!-- PHP 7.4 required-->
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/> <!-- PHP 7.4 required-->
<exclude name="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName"/>
<exclude name="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName"/>

<!-- As per: https://symfony.com/doc/current/contributing/code/standards.html#structure -->
<exclude name="PSR12.Operators.OperatorSpacing.NoSpaceBefore"/>
Expand All @@ -39,6 +40,10 @@
<exclude-pattern>Tests</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.PHP.RequireExplicitAssertion.RequiredExplicitAssertion">
<exclude-pattern>Tests</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification">
<exclude-pattern>Tests</exclude-pattern>
</rule>
Expand Down
6 changes: 3 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ec620b1

Please sign in to comment.