Skip to content

Commit

Permalink
Check identity of the Container parameter (#128)
Browse files Browse the repository at this point in the history
* Check identity of the Container parameter

---------

Co-authored-by: Joseph Bielawski <stloyd@users.noreply.github.com>
  • Loading branch information
Zales0123 and stloyd authored Dec 13, 2023
1 parent 23e0c46 commit 2bd8962
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
17 changes: 17 additions & 0 deletions PhpUnit/AbstractContainerBuilderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ final protected function assertContainerBuilderHasParameter(
);
}

/**
* Assert that the ContainerBuilder for this test has a parameter and that its value and type is identical to expected.
*
* @param mixed $expectedParameterValue
*/
final protected function assertContainerBuilderHasExactParameter(
string $parameterName,
$expectedParameterValue = null
): void {
$checkParameterValue = (func_num_args() > 1);

self::assertThat(
$this->container,
new ContainerHasParameterConstraint($parameterName, $expectedParameterValue, $checkParameterValue, true)
);
}

/**
* Assert that the ContainerBuilder for this test has a service definition with the given id, which has an argument
* at the given index, and its value is the given value.
Expand Down
8 changes: 6 additions & 2 deletions PhpUnit/ContainerHasParameterConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\Constraint\IsIdentical;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class ContainerHasParameterConstraint extends Constraint
{
private $parameterName;
private $expectedParameterValue;
private $checkParameterValue;
private $strict;

public function __construct(
string $parameterName,
$expectedParameterValue = null,
bool $checkParameterValue = false
bool $checkParameterValue = false,
bool $strict = false
) {
$this->parameterName = $parameterName;
$this->expectedParameterValue = $expectedParameterValue;
$this->checkParameterValue = $checkParameterValue;
$this->strict = $strict;
}

public function toString(): string
Expand Down Expand Up @@ -69,7 +73,7 @@ private function evaluateParameterValue(ContainerInterface $container, bool $ret
{
$actualValue = $container->getParameter($this->parameterName);

$constraint = new IsEqual($this->expectedParameterValue);
$constraint = $this->strict ? new IsIdentical($this->expectedParameterValue) : new IsEqual($this->expectedParameterValue);

if (!$constraint->evaluate($actualValue, '', true)) {
if ($returnResult) {
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ These are the available semantic assertions for each of the test cases shown abo
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter.</dd>
<dt><code>assertContainerBuilderHasParameter($parameterName, $expectedParameterValue)</code></dt>
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter and that its value is the given value.</dd>
<dt><code>assertContainerBuilderHasExactParameter($parameterName)</code></dt>
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter.</dd>
<dt><code>assertContainerBuilderHasExactParameter($parameterName, $expectedParameterValue)</code></dt>
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter and that its value is the given value, as well as its type matches given value type.</dd>
<dt><code>assertContainerBuilderHasServiceDefinitionWithArgument($serviceId, $argumentIndex)</code></dt>
<dd>Assert that the <code>ContainerBuilder</code> for this test has a service definition with the given id, which has an argument at
the given index.</dd>
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function load(array $config, ContainerBuilder $container): void

// set a parameter manually
$container->setParameter('manual_parameter', 'parameter value');
$container->setParameter('manual_number_parameter', 123123);
$container->setParameter('manual_array_parameter', ['key1' => 'value1', 'key2' => 'value2']);

// manually add a service definition
$definition = new Definition('stdClass');
Expand Down
36 changes: 36 additions & 0 deletions Tests/PhpUnit/AbstractExtensionTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public function if_load_is_successful_it_does_not_fail(): void
// Just check parameter exists, value will not be checked.
$this->assertContainerBuilderHasParameter('manual_parameter');

// manually defined number parameter
$this->assertContainerBuilderHasExactParameter('manual_number_parameter', 123123);
// Just check parameter exists, value will not be checked.
$this->assertContainerBuilderHasExactParameter('manual_number_parameter');

// manually defined array parameter
$this->assertContainerBuilderHasExactParameter('manual_array_parameter', ['key1' => 'value1', 'key2' => 'value2']);
// Just check parameter exists, value will not be checked.
$this->assertContainerBuilderHasExactParameter('manual_array_parameter');

// manually defined service
$this->assertContainerBuilderHasService('manual_service_id', 'stdClass');
// Just check service exists, class will not be checked.
Expand Down Expand Up @@ -153,6 +163,32 @@ public function if_parameter_exists_but_has_wrong_value_it_fails(): void
$this->assertContainerBuilderHasParameter('manual_parameter', 'wrong');
}

/**
* @test
*/
public function if_parameter_exists_and_has_good_value_but_has_wrong_type_it_fails(): void
{
$this->load();

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('parameter value');

$this->assertContainerBuilderHasExactParameter('manual_number_parameter', '123123');
}

/**
* @test
*/
public function if_parameter_exists_but_has_wrong_order_it_fails(): void
{
$this->load();

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('parameter value');

$this->assertContainerBuilderHasExactParameter('manual_array_parameter', ['key2' => 'value2', 'key1' => 'value1']);
}

/**
* @test
*/
Expand Down
34 changes: 33 additions & 1 deletion Tests/PhpUnit/ContainerHasParameterConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,25 @@ public function match(
$this->assertSame($expectedToMatch, $constraint->evaluate($container, '', true));
}

public static function containerBuilderProvider()
/**
* @test
*
* @dataProvider typeAwareContainerBuilderProvider
*/
public function matchWithType(
array $containerParameters,
$parameterName,
$parameterValue,
$checkParameterValue,
$expectedToMatch
): void {
$container = $this->createMockContainerWithParameters($containerParameters);
$constraint = new ContainerHasParameterConstraint($parameterName, $parameterValue, $checkParameterValue, true);

$this->assertSame($expectedToMatch, $constraint->evaluate($container, '', true));
}

public static function containerBuilderProvider(): array
{
$parameterName = 'parameter_name';
$parameterValue = 'some value';
Expand All @@ -44,6 +62,20 @@ public static function containerBuilderProvider()
];
}

public static function typeAwareContainerBuilderProvider(): array
{
$parameterName = 'parameter_name';
$parameterValue = '123123';
$wrongParameterValue = 123123;

return [
// the container has the parameter but the type don't match
[[$parameterName => $parameterValue], $parameterName, $wrongParameterValue, true, false],
// the container has the parameter and the value matches
[[$parameterName => $parameterValue], $parameterName, $parameterValue, true, true],
];
}

private function createMockContainerWithParameters(array $parameters)
{
$container = $this->createMock(ContainerInterface::class);
Expand Down

0 comments on commit 2bd8962

Please sign in to comment.