Passed
Pull Request — master (#277)
by Kirill
03:11
created

FactoryTest::testCascadeFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Core;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Core\Container;
16
use Spiral\Core\FactoryInterface;
17
use Spiral\Tests\Core\Fixtures\BadClass;
18
use Spiral\Tests\Core\Fixtures\Bucket;
19
use Spiral\Tests\Core\Fixtures\CorruptedClass;
0 ignored issues
show
Bug introduced by
The type Spiral\Tests\Core\Fixtures\CorruptedClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Spiral\Tests\Core\Fixtures\SampleClass;
21
22
class FactoryTest extends TestCase
23
{
24
    public function testAutoFactory(): void
25
    {
26
        $container = new Container();
27
        $this->assertInstanceOf(FactoryInterface::class, $container);
28
29
        $bucket = $container->make(Bucket::class, [
30
            'name' => 'abc',
31
            'data' => 'some data',
32
        ]);
33
34
        $this->assertInstanceOf(Bucket::class, $bucket);
35
        $this->assertSame('abc', $bucket->getName());
36
        $this->assertSame('some data', $bucket->getData());
37
    }
38
39
    public function testClosureFactory(): void
40
    {
41
        $container = new Container();
42
        $this->assertInstanceOf(FactoryInterface::class, $container);
43
44
        $container->bind(Bucket::class, function ($data) {
45
            return new Bucket('via-closure', $data);
46
        });
47
48
        $bucket = $container->make(Bucket::class, [
49
            'data' => 'some data',
50
        ]);
51
52
        $this->assertInstanceOf(Bucket::class, $bucket);
53
        $this->assertSame('via-closure', $bucket->getName());
54
        $this->assertSame('some data', $bucket->getData());
55
    }
56
57
    public function testMethodFactory(): void
58
    {
59
        $container = new Container();
60
        $this->assertInstanceOf(FactoryInterface::class, $container);
61
62
        $container->bind(Bucket::class, [self::class, 'makeBucket']);
63
64
        $bucket = $container->make(Bucket::class, [
65
            'data' => 'some data',
66
        ]);
67
68
        $this->assertInstanceOf(Bucket::class, $bucket);
69
        $this->assertSame('via-method', $bucket->getName());
70
        $this->assertSame('some data', $bucket->getData());
71
    }
72
73
    public function testCascadeFactory(): void
74
    {
75
        $container = new Container();
76
        $this->assertInstanceOf(FactoryInterface::class, $container);
77
78
        $sample = new SampleClass();
79
80
        $container->bind(Bucket::class, [self::class, 'makeBucketWithSample']);
81
        $container->bind(SampleClass::class, function () use ($sample) {
82
            return $sample;
83
        });
84
85
        $bucket = $container->make(Bucket::class);
86
87
        $this->assertInstanceOf(Bucket::class, $bucket);
88
        $this->assertSame('via-method-with-sample', $bucket->getName());
89
        $this->assertSame($sample, $bucket->getData());
90
    }
91
92
    public function testConstructAbstract(): void
93
    {
94
        $this->expectException(\Spiral\Core\Exception\Container\ContainerException::class);
95
        $container = new Container();
96
        $container->make(BadClass::class);
97
    }
98
99
    public function testConstructCorrupted(): void
100
    {
101
        $this->expectException(\ParseError::class);
102
        $container = new Container();
103
        $container->make(CorruptedClass::class);
104
    }
105
106
    /**
107
     * @param mixed $data
108
     *
109
     * @return Bucket
110
     */
111
    private function makeBucket($data)
112
    {
113
        return new Bucket('via-method', $data);
114
    }
115
116
    /**
117
     * @param SampleClass $sample
118
     *
119
     * @return Bucket
120
     */
121
    private function makeBucketWithSample(SampleClass $sample)
122
    {
123
        return new Bucket('via-method-with-sample', $sample);
124
    }
125
}
126