Skip to content

Use the PHPUnit Exporter util class #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions PhpUnit/ContainerBuilderHasServiceDefinitionConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use SebastianBergmann\Exporter\Exporter;
use PHPUnit\Util\Exporter;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class ContainerBuilderHasServiceDefinitionConstraint extends Constraint
{
private $serviceId;
private $expectedClass;
private $checkExpectedClass;
private $exporter;

public function __construct(
string $serviceId,
Expand All @@ -22,7 +21,6 @@ public function __construct(
$this->serviceId = $serviceId;
$this->expectedClass = $expectedClass;
$this->checkExpectedClass = $checkExpectedClass;
$this->exporter = new Exporter();
}

public function toString(): string
Expand Down Expand Up @@ -88,8 +86,8 @@ private function evaluateClass(ContainerBuilder $containerBuilder, bool $returnR
$this->fail($containerBuilder, sprintf(
'The class of the service definition of "%s" (%s) does not match the expected value (%s)',
$this->serviceId,
$this->exporter->export($actualClass),
$this->exporter->export($this->expectedClass)
Exporter::export($actualClass),
Exporter::export($this->expectedClass)
));
}

Expand Down
8 changes: 3 additions & 5 deletions PhpUnit/ContainerHasParameterConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\Constraint\IsIdentical;
use SebastianBergmann\Exporter\Exporter;
use PHPUnit\Util\Exporter;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class ContainerHasParameterConstraint extends Constraint
Expand All @@ -14,7 +14,6 @@ final class ContainerHasParameterConstraint extends Constraint
private $expectedParameterValue;
private $checkParameterValue;
private $strict;
private $exporter;

public function __construct(
string $parameterName,
Expand All @@ -26,7 +25,6 @@ public function __construct(
$this->expectedParameterValue = $expectedParameterValue;
$this->checkParameterValue = $checkParameterValue;
$this->strict = $strict;
$this->exporter = new Exporter();
}

public function toString(): string
Expand Down Expand Up @@ -86,8 +84,8 @@ private function evaluateParameterValue(ContainerInterface $container, bool $ret
$this->fail($container, sprintf(
'The value of parameter "%s" (%s) does not match the expected value (%s)',
$this->parameterName,
$this->exporter->export($actualValue),
$this->exporter->export($this->expectedParameterValue)
Exporter::export($actualValue),
Exporter::export($this->expectedParameterValue)
));
}

Expand Down
13 changes: 5 additions & 8 deletions PhpUnit/DefinitionArgumentEqualsServiceLocatorConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use SebastianBergmann\Exporter\Exporter;
use PHPUnit\Util\Exporter;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -20,7 +20,6 @@ final class DefinitionArgumentEqualsServiceLocatorConstraint extends Constraint
private $argumentIndex;
private $expectedValue;
private $serviceId;
private $exporter;

public function __construct(string $serviceId, $argumentIndex, array $expectedValue)
{
Expand Down Expand Up @@ -56,8 +55,6 @@ function ($serviceId) {
},
$expectedValue
);

$this->exporter = new Exporter();
}

public function toString(): string
Expand Down Expand Up @@ -130,7 +127,7 @@ private function evaluateArgumentValue(ContainerBuilder $container, bool $return
sprintf(
'The value of argument with index %s (%s) was expected to an instance of Symfony\Component\DependencyInjection\Reference or \Symfony\Component\DependencyInjection\Definition',
$this->argumentIndex,
$this->exporter->export($actualValue)
Exporter::export($actualValue)
)
);
}
Expand All @@ -145,7 +142,7 @@ private function evaluateArgumentValue(ContainerBuilder $container, bool $return
sprintf(
'The referenced service class of argument with index %s (%s) was expected to be an instance of Symfony\Component\DependencyInjection\ServiceLocator',
$this->argumentIndex,
$this->exporter->export($serviceLocatorDef->getClass())
Exporter::export($serviceLocatorDef->getClass())
)
);
}
Expand Down Expand Up @@ -176,8 +173,8 @@ private function evaluateServiceDefinition(
sprintf(
'The value of argument with index %s (%s) does not equal to the expected ServiceLocator service-map (%s)',
$this->argumentIndex,
$this->exporter->export($actualValue),
$this->exporter->export($this->expectedValue)
Exporter::export($actualValue),
Exporter::export($this->expectedValue)
)
);
}
Expand Down
12 changes: 5 additions & 7 deletions PhpUnit/DefinitionEqualsServiceLocatorConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use SebastianBergmann\Exporter\Exporter;
use PHPUnit\Util\Exporter;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -13,7 +13,6 @@
final class DefinitionEqualsServiceLocatorConstraint extends Constraint
{
private $expectedValue;
private $exporter;

public function __construct($expectedValue)
{
Expand All @@ -31,7 +30,6 @@ function ($serviceId) {
},
$expectedValue
);
$this->exporter = new Exporter();
}

public function toString(): string
Expand Down Expand Up @@ -73,8 +71,8 @@ private function evaluateServiceDefinitionClass(Definition $definition, bool $re
$definition,
sprintf(
'class %s was expected as service definition class, found %s instead',
$this->exporter->export(ServiceLocator::class),
$this->exporter->export($definition->getClass())
Exporter::export(ServiceLocator::class),
Exporter::export($definition->getClass())
)
);
}
Expand All @@ -93,8 +91,8 @@ private function evaluateArgumentIndex(Definition $definition, bool $returnResul
$definition,
sprintf(
'The service-map %s does not equal to the expected service-map (%s)',
$this->exporter->export($actualValue),
$this->exporter->export($this->expectedValue)
Exporter::export($actualValue),
Exporter::export($this->expectedValue)
)
);
}
Expand Down
16 changes: 7 additions & 9 deletions PhpUnit/DefinitionHasArgumentConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use SebastianBergmann\Exporter\Exporter;
use PHPUnit\Util\Exporter;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;

Expand All @@ -16,7 +16,6 @@ final class DefinitionHasArgumentConstraint extends Constraint
private string|int $argumentIndex;
private mixed $expectedValue;
private bool $checkExpectedValue;
private Exporter $exporter;

public function __construct($argumentIndex, $expectedValue, bool $checkExpectedValue = true)
{
Expand All @@ -39,7 +38,6 @@ public function __construct($argumentIndex, $expectedValue, bool $checkExpectedV
$this->argumentIndex = $argumentIndex;
$this->expectedValue = $expectedValue;
$this->checkExpectedValue = $checkExpectedValue;
$this->exporter = new Exporter();
}

public function toString(): string
Expand Down Expand Up @@ -107,8 +105,8 @@ private function evaluateArgumentValue(Definition $definition, bool $returnResul
sprintf(
'The value of argument named "%s" (%s) is not equal to the expected value (%s)',
$this->argumentIndex,
$this->exporter->export($actualValue),
$this->exporter->export($this->expectedValue)
Exporter::export($actualValue),
Exporter::export($this->expectedValue)
)
);
}
Expand All @@ -124,15 +122,15 @@ private function evaluateArgumentValue(Definition $definition, bool $returnResul
$message = sprintf(
'The value of argument named "%s" (%s) is not equal to the expected value (%s)',
$this->argumentIndex,
$this->exporter->export($actualValue),
$this->exporter->export($this->expectedValue)
Exporter::export($actualValue),
Exporter::export($this->expectedValue)
);
} else {
$message = sprintf(
'The value of argument with index %d (%s) is not equal to the expected value (%s)',
$this->argumentIndex,
$this->exporter->export($actualValue),
$this->exporter->export($this->expectedValue)
Exporter::export($actualValue),
Exporter::export($this->expectedValue)
);
}

Expand Down
6 changes: 2 additions & 4 deletions PhpUnit/DefinitionHasMethodCallConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use SebastianBergmann\Exporter\Exporter;
use PHPUnit\Util\Exporter;
use Symfony\Component\DependencyInjection\Definition;

final class DefinitionHasMethodCallConstraint extends Constraint
{
private $methodName;
private $arguments;
private $index;
private $exporter;

public function __construct(string $methodName, array $arguments = [], $index = null)
{
Expand All @@ -23,7 +22,6 @@ public function __construct(string $methodName, array $arguments = [], $index =
$this->methodName = $methodName;
$this->arguments = $arguments;
$this->index = $index;
$this->exporter = new Exporter();
}

public function evaluate($other, string $description = '', bool $returnResult = false): bool
Expand Down Expand Up @@ -58,7 +56,7 @@ public function evaluate($other, string $description = '', bool $returnResult =
sprintf(
'None of the method calls matched the expected method "%s" with arguments %s with %s invocation order index',
$this->methodName,
$this->exporter->export($this->arguments),
Exporter::export($this->arguments),
(null === $this->index) ? 'any' : sprintf('"%s"', $this->index)
)
);
Expand Down
6 changes: 2 additions & 4 deletions PhpUnit/DefinitionHasTagConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use SebastianBergmann\Exporter\Exporter;
use PHPUnit\Util\Exporter;
use Symfony\Component\DependencyInjection\Definition;

final class DefinitionHasTagConstraint extends Constraint
{
private $name;
private $attributes;
private $exporter;

public function __construct(string $name, array $attributes = [])
{
$this->name = $name;
$this->attributes = $attributes;
$this->exporter = new Exporter();
}

public function evaluate($other, string $description = '', bool $returnResult = false): bool
Expand Down Expand Up @@ -45,7 +43,7 @@ public function evaluate($other, string $description = '', bool $returnResult =
sprintf(
'None of the tags matched the expected name "%s" with attributes %s',
$this->name,
$this->exporter->export($this->attributes)
Exporter::export($this->attributes)
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ container:

| Version | Released | PHPUnit | Status |
|---------|--------------|---------------------|------------|
| 6.x | Aug 8, 2024 | 10.5, 11.x and 12.x | Latest |
| 6.x | Aug 8, 2024 | 10.5, 11.5 and 12.x | Latest |
| 5.x | Nov 22, 2023 | 9.6 and 10.x | Bugfixes |
| 4.x | Mar 28, 2019 | 8.x and 9.x | Bugfixes |
| 3.x | Mar 5, 2018 | 7.x | Bugfixes |
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"require": {
"php": "^8.1",
"matthiasnoback/symfony-config-test": "^5.0 || ^6.0",
"phpunit/phpunit": "^10.5.11 || ^11.0 || ^12.0",
"sebastian/exporter": " ^5.0 || ^6.0 || ^7.0",
"phpunit/phpunit": "^10.5.11 || ^11.5 || ^12.0",
"symfony/dependency-injection": "^5.4 || ^6.2 || ^7.0",
"symfony/config": "^5.4 || ^6.2 || ^7.0",
"symfony/yaml": "^5.4 || ^6.2 || ^7.0"
Expand Down