Skip to content

Commit 4824124

Browse files
authored
Fix: Add missing tests for ContainerBuilder (#228)
1 parent 2b5a3c9 commit 4824124

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

test/Faker/Extension/ContainerBuilderTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Faker\Core\File;
88
use Faker\Extension\ContainerBuilder;
9+
use Faker\Extension\Extension;
910
use PHPUnit\Framework\TestCase;
1011
use Psr\Container\ContainerInterface;
1112

@@ -14,6 +15,90 @@
1415
*/
1516
final class ContainerBuilderTest extends TestCase
1617
{
18+
/**
19+
* @dataProvider provideInvalidValue
20+
*
21+
* @param array|bool|float|int|resource|null $value
22+
*/
23+
public function testAddRejectsInvalidValue($value): void
24+
{
25+
$containerBuilder = new ContainerBuilder();
26+
27+
$this->expectException(\InvalidArgumentException::class);
28+
$this->expectExceptionMessage(sprintf(
29+
'First argument to "%s::add()" must be a string, callable or object.',
30+
ContainerBuilder::class
31+
));
32+
33+
$containerBuilder->add($value);
34+
}
35+
36+
/**
37+
* @return \Generator<string, array{0: array|bool|float|int|null|resource}>
38+
*/
39+
public function provideInvalidValue(): \Generator
40+
{
41+
$values = [
42+
'array' => [
43+
'foo',
44+
'bar',
45+
'baz',
46+
],
47+
'bool-false' => false,
48+
'bool-true' => true,
49+
'float' => 3.14,
50+
'int' => 9001,
51+
'null' => true,
52+
'resource' => fopen(__FILE__, 'rb'),
53+
];
54+
55+
foreach ($values as $key => $value) {
56+
yield $key => [
57+
$value,
58+
];
59+
}
60+
}
61+
62+
public function testAddRejectsNameWhenValueIsCallableAndNameIsNull(): void
63+
{
64+
$value = [
65+
new class() {
66+
public static function create(): Extension
67+
{
68+
return new class() implements Extension {
69+
};
70+
}
71+
},
72+
'create',
73+
];
74+
75+
$containerBuilder = new ContainerBuilder();
76+
77+
$this->expectException(\InvalidArgumentException::class);
78+
$this->expectExceptionMessage(sprintf(
79+
'Second argument to "%s::add()" is required not passing a string or object as first argument',
80+
ContainerBuilder::class
81+
));
82+
83+
$containerBuilder->add($value);
84+
}
85+
86+
public function testAddAcceptsValueWhenItIsAnObjectAndNameIsNull(): void
87+
{
88+
$value = new class() implements Extension {};
89+
90+
$name = get_class($value);
91+
92+
$containerBuilder = new ContainerBuilder();
93+
94+
$containerBuilder->add($value);
95+
96+
$container = $containerBuilder->build();
97+
98+
self::assertTrue($container->has($name));
99+
self::assertSame($value, $container->get($name));
100+
}
101+
17102
public function testBuildEmpty(): void
18103
{
19104
$builder = new ContainerBuilder();

0 commit comments

Comments
 (0)