|
6 | 6 |
|
7 | 7 | use Faker\Core\File; |
8 | 8 | use Faker\Extension\ContainerBuilder; |
| 9 | +use Faker\Extension\Extension; |
9 | 10 | use PHPUnit\Framework\TestCase; |
10 | 11 | use Psr\Container\ContainerInterface; |
11 | 12 |
|
|
14 | 15 | */ |
15 | 16 | final class ContainerBuilderTest extends TestCase |
16 | 17 | { |
| 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 | + |
17 | 102 | public function testBuildEmpty(): void |
18 | 103 | { |
19 | 104 | $builder = new ContainerBuilder(); |
|
0 commit comments