File: ContainerBuilderTest.php

package info (click to toggle)
php-faker 1.20.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,412 kB
  • sloc: php: 115,671; xml: 213; makefile: 49
file content (164 lines) | stat: -rw-r--r-- 4,162 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

declare(strict_types=1);

namespace Faker\Test\Extension;

use Faker\Container\ContainerBuilder;
use Faker\Container\ContainerInterface;
use Faker\Core\File;
use Faker\Extension\Extension;
use PHPUnit\Framework\TestCase;

/**
 * @covers \Faker\Container\ContainerBuilder
 */
final class ContainerBuilderTest extends TestCase
{
    /**
     * @dataProvider provideInvalidValue
     *
     * @param array|bool|float|int|resource|null $value
     */
    public function testAddRejectsInvalidValue($value): void
    {
        $containerBuilder = new ContainerBuilder();

        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage(sprintf(
            'First argument to "%s::add()" must be a string, callable or object.',
            ContainerBuilder::class
        ));

        $containerBuilder->add($value);
    }

    /**
     * @return \Generator<string, array{0: array|bool|float|int|null|resource}>
     */
    public function provideInvalidValue(): \Generator
    {
        $values = [
            'array' => [
                'foo',
                'bar',
                'baz',
            ],
            'bool-false' => false,
            'bool-true' => true,
            'float' => 3.14,
            'int' => 9001,
            'null' => true,
            'resource' => fopen(__FILE__, 'rb'),
        ];

        foreach ($values as $key => $value) {
            yield $key => [
                $value,
            ];
        }
    }

    public function testAddRejectsNameWhenValueIsCallableAndNameIsNull(): void
    {
        $value = [
            new class() {
                public static function create(): Extension
                {
                    return new class() implements Extension {
                    };
                }
            },
            'create',
        ];

        $containerBuilder = new ContainerBuilder();

        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage(sprintf(
            'Second argument to "%s::add()" is required not passing a string or object as first argument',
            ContainerBuilder::class
        ));

        $containerBuilder->add($value);
    }

    public function testAddAcceptsValueWhenItIsAnObjectAndNameIsNull(): void
    {
        $value = new class() implements Extension {};

        $name = get_class($value);

        $containerBuilder = new ContainerBuilder();

        $containerBuilder->add($value);

        $container = $containerBuilder->build();

        self::assertTrue($container->has($name));
        self::assertSame($value, $container->get($name));
    }

    public function testBuildEmpty(): void
    {
        $builder = new ContainerBuilder();

        $container = $builder->build();

        self::assertInstanceOf(ContainerInterface::class, $container);
    }

    public function testBuild(): void
    {
        $builder = new ContainerBuilder();

        $builder->add(File::class);

        $container = $builder->build();

        self::assertInstanceOf(ContainerInterface::class, $container);
    }

    public function testBuildWithDuplicates(): void
    {
        $builder = new ContainerBuilder();

        $builder->add(File::class);
        $builder->add(File::class);

        $container = $builder->build();

        self::assertInstanceOf(ContainerInterface::class, $container);
    }

    public function testBuildWithObject(): void
    {
        $builder = new ContainerBuilder();

        $builder->add(new File(), 'foo');

        $container = $builder->build();

        self::assertInstanceOf(ContainerInterface::class, $container);
    }

    public function testBuildWithCallable(): void
    {
        $builder = new ContainerBuilder();

        $builder->add(static function () {
            return new File();
        }, 'foo');

        $container = $builder->build();

        self::assertInstanceOf(ContainerInterface::class, $container);
    }

    public function testBuildDefault(): void
    {
        $container = ContainerBuilder::getDefault();

        self::assertInstanceOf(ContainerInterface::class, $container);
    }
}