Test Setup Failed
Push — master ( e2ccdd...da0315 )
by Gerrit
09:14
created

EntityArgumentFactoryTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 140
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (C) 2017  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\SymfonyGenerics\Tests\Unit\Arguments\ArgumentFactory;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory;
15
use InvalidArgumentException;
16
use Addiks\SymfonyGenerics\Arguments\ArgumentCall;
17
use Addiks\SymfonyGenerics\Arguments\Argument;
18
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\EntityArgumentFactory;
19
use Addiks\SymfonyGenerics\Arguments\EntityArgument;
20
use Doctrine\Persistence\ObjectManager;
21
22
final class EntityArgumentFactoryTest extends TestCase
23
{
24
25
    private EntityArgumentFactory $factory;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
26
27
    private ObjectManager $objectManager;
28
29
    private ArgumentFactory $argumentFactory;
30
31
    public function setUp()
32
    {
33
        $this->objectManager = $this->createMock(ObjectManager::class);
34
        $this->argumentFactory = $this->createMock(ArgumentFactory::class);
35
36
        $this->factory = new EntityArgumentFactory($this->objectManager, $this->argumentFactory);
37
    }
38
39
    /**
40
     * @test
41
     * @dataProvider dataProviderForShouldKnowIfUnderstandString
42
     */
43
    public function shouldKnowIfUnderstandString(bool $expectedResult, string $source)
44
    {
45
        /** @var bool $actualResult */
46
        $actualResult = $this->factory->understandsString($source);
47
48
        $this->assertEquals($expectedResult, $actualResult);
49
    }
50
51
    public function dataProviderForShouldKnowIfUnderstandString()
52
    {
53
        return array(
54
            [true, 'Foo#bar'],
55
            [false, 'Foo#'],
56
            [false, '#bar'],
57
            [false, '#'],
58
        );
59
    }
60
61
    /**
62
     * @test
63
     * @dataProvider dataProviderForShouldKnowIfUnderstandArray
64
     */
65
    public function shouldKnowIfUnderstandArray(bool $expectedResult, array $source)
66
    {
67
        /** @var bool $actualResult */
68
        $actualResult = $this->factory->understandsArray($source);
69
70
        $this->assertEquals($expectedResult, $actualResult);
71
    }
72
73
    public function dataProviderForShouldKnowIfUnderstandArray()
74
    {
75
        return array(
76
            [true,  ['entity-class' => 'foo', 'entity-id' => 'bar']],
77
            [false, ['entity-id' => 'bar']],
78
            [false, ['entity-class' => 'foo']],
79
        );
80
    }
81
82
    /**
83
     * @test
84
     * @dataProvider dataProviderForShouldCreateCallArgumentFromString
85
     */
86
    public function shouldCreateCallArgumentFromString(
87
        ?EntityArgument $expectedResult,
88
        string $source,
89
        bool $shouldRejectCreation
90
    ) {
91
        if ($shouldRejectCreation) {
92
            $this->expectException(InvalidArgumentException::class);
93
94
        } else {
95
            $this->argumentFactory->method('createArgumentFromString')->willReturn($this->createMock(Argument::class));
96
        }
97
98
        $actualResult = $this->factory->createArgumentFromString($source);
99
100
        $this->assertEquals($expectedResult, $actualResult);
101
    }
102
103
    public function dataProviderForShouldCreateCallArgumentFromString(): array
104
    {
105
        $this->setUp();
106
107
        return array(
108
            [new EntityArgument($this->objectManager, 'Foo', $this->createMock(Argument::class)), 'Foo#bar', false],
109
            [null, 'a#', true],
110
            [null, '#b', true],
111
            [null, '#', true],
112
        );
113
    }
114
115
    /**
116
     * @test
117
     * @dataProvider dataProviderForShouldCreateCallArgumentFromArray
118
     */
119
    public function shouldCreateCallArgumentFromArray(
120
        ?EntityArgument $expectedResult,
121
        array $source,
122
        bool $shouldRejectCreation
123
    ) {
124
        if ($shouldRejectCreation) {
125
            $this->expectException(InvalidArgumentException::class);
126
127
        } else {
128
            $this->argumentFactory->method('createArgumentFromString')->willReturn($this->createMock(Argument::class));
129
            $this->argumentFactory->method('createArgumentFromArray')->willReturn($this->createMock(Argument::class));
130
        }
131
132
        $actualResult = $this->factory->createArgumentFromArray($source);
133
134
        $this->assertEquals($expectedResult, $actualResult);
135
    }
136
137
    public function dataProviderForShouldCreateCallArgumentFromArray(): array
138
    {
139
        $this->setUp();
140
141
        return array(
142
            [null, [], true],
143
            [null, ['entity-id' => 'foo'], true],
144
            [null, ['entity-class' => 'bar'], true],
145
            [new EntityArgument($this->objectManager, 'Foo', $this->createMock(Argument::class)), [
146
                'entity-class' => 'Foo',
147
                'entity-id' => 'bar'
148
            ], false],
149
        );
150
    }
151
152
}
153