Completed
Pull Request — master (#10)
by Mariusz
03:23
created

DivergingAssociationPathBuilderTest::testCase2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Xsolve\AssociateTests\Functional\DoctrineOrm\Association\Path\Builder;
4
5
use PHPUnit\Framework\TestCase;
6
use Xsolve\Associate\DoctrineOrm\Association\Path\Builder\DivergingAssociationPathBuilder;
7
use Xsolve\Associate\DoctrineOrm\Association\Path\DivergingAssociationPath;
8
9
class DivergingAssociationPathBuilderTest extends TestCase
10
{
11
    // TODO Assert alias.
12
    // TODO Assert load mode.
13
14
    public function testCase1()
15
    {
16
        $divergingAssociationPathBuilder = new DivergingAssociationPathBuilder();
17
18
        $divergingAssociationPath = $divergingAssociationPathBuilder
19
            ->associate('bar')
20
                ->aliasAs('bars')
21
                ->loadFull()
22
            ->associate('baz')
23
            ->associate('qux')
24
                ->aliasAs('qux')
25
                ->loadProxy()
26
            ->create();
27
28
        $this->assertInstanceOf(DivergingAssociationPath::class, $divergingAssociationPath);
29
        $this->assertSame(
30
            [
31
                '.bar.baz.qux',
32
            ],
33
            $this->convertDivergingAssociationPathToStrings($divergingAssociationPath)
34
        );
35
    }
36
37
    public function testCase2()
38
    {
39
        $divergingAssociationPathBuilder = new DivergingAssociationPathBuilder();
40
41
        $divergingAssociationPath = $divergingAssociationPathBuilder
42
            ->associate('bar')
43
            ->associate('baz')
44
            ->associate('qux')
45
                ->loadFull()
46
            ->create();
47
48
        $this->assertInstanceOf(DivergingAssociationPath::class, $divergingAssociationPath);
49
        $this->assertSame(
50
            [
51
                '.bar.baz.qux',
52
            ],
53
            $this->convertDivergingAssociationPathToStrings($divergingAssociationPath)
54
        );
55
    }
56
57
    public function testCase3()
58
    {
59
        $divergingAssociationPathBuilder = new DivergingAssociationPathBuilder();
60
61
        $divergingAssociationPath = $divergingAssociationPathBuilder
62
            ->diverge()
63
                ->associate('foo')
64
                ->diverge()
65
                    ->associate('bar')
66
                ->endDiverge()
67
                ->diverge()
68
                    ->associate('baz')
69
                        ->aliasAs('.foo.baz')
70
                        ->loadProxy()
71
                    ->associate('qux')
72
                ->endDiverge()
73
            ->endDiverge()
74
            ->diverge()
75
                ->associate('qux')
76
            ->endDiverge()
77
            ->create();
78
79
        $this->assertInstanceOf(DivergingAssociationPath::class, $divergingAssociationPath);
80
        $this->assertSame(
81
            [
82
                '.foo.bar',
83
                '.foo.baz.qux',
84
                '.qux',
85
            ],
86
            $this->convertDivergingAssociationPathToStrings($divergingAssociationPath)
87
        );
88
    }
89
90
    public function testCase4()
91
    {
92
        $divergingAssociationPathBuilder = new DivergingAssociationPathBuilder();
93
94
        $divergingAssociationPath = $divergingAssociationPathBuilder
95
            ->associate('foo')
96
            ->associate('bar')
97
            ->diverge()
98
                ->associate('baz')
99
                ->associate('qux')
100
            ->endDiverge()
101
            ->diverge()
102
                ->associate('qux')
103
            ->endDiverge()
104
            ->create();
105
106
        $this->assertInstanceOf(DivergingAssociationPath::class, $divergingAssociationPath);
107
        $this->assertSame(
108
            [
109
                '.foo.bar.baz.qux',
110
                '.foo.bar.qux',
111
            ],
112
            $this->convertDivergingAssociationPathToStrings($divergingAssociationPath)
113
        );
114
    }
115
116
    public function testCase5()
117
    {
118
        $divergingAssociationPathBuilder = new DivergingAssociationPathBuilder();
119
120
        $divergingAssociationPath = $divergingAssociationPathBuilder
121
            ->diverge()
122
                ->associate('foo')
123
            ->endDiverge()
124
            ->diverge()
125
                ->associate('bar')
126
            ->endDiverge()
127
            ->diverge()
128
                ->associate('baz')
129
                ->associate('qux')
130
            ->endDiverge()
131
            ->diverge()
132
                ->associate('qux')
133
            ->endDiverge()
134
            ->create();
135
136
        $this->assertInstanceOf(DivergingAssociationPath::class, $divergingAssociationPath);
137
        $this->assertSame(
138
            [
139
                '.foo',
140
                '.bar',
141
                '.baz.qux',
142
                '.qux',
143
            ],
144
            $this->convertDivergingAssociationPathToStrings($divergingAssociationPath)
145
        );
146
    }
147
148
    public function testCase6()
149
    {
150
        $divergingAssociationPathBuilder = new DivergingAssociationPathBuilder();
151
152
        $divergingAssociationPath = $divergingAssociationPathBuilder
153
            ->diverge()
154
                ->associate('foo')
155
                ->diverge()
156
                    ->associate('bar')
157
                    ->diverge()
158
                        ->associate('baz')
159
                        ->associate('qux')
160
                        ->diverge()
161
                            ->associate('qux')
162
            ->create();
163
164
        $this->assertInstanceOf(DivergingAssociationPath::class, $divergingAssociationPath);
165
        $this->assertSame(
166
            [
167
                '.foo.bar.baz.qux.qux',
168
            ],
169
            $this->convertDivergingAssociationPathToStrings($divergingAssociationPath)
170
        );
171
    }
172
173
    /**
174
     * @param DivergingAssociationPath $divergingAssociationPath
175
     *
176
     * @return string[]
177
     */
178
    protected function convertDivergingAssociationPathToStrings(DivergingAssociationPath $divergingAssociationPath): array
179
    {
180
        $path = '';
181
        foreach ($divergingAssociationPath->getAssociations() as $association) {
182
            $path .= sprintf('.%s', $association->getRelationshipName());
183
        }
184
185
        if (!$divergingAssociationPath->hasChildDivergingAssociationPath()) {
186
            return [$path];
187
        }
188
189
        $childPaths = [];
190
        foreach ($divergingAssociationPath->getChildDivergingAssociationPaths() as $childDivergingAssociationPath) {
191
            $childPaths = array_merge(
192
                $childPaths,
193
                $this->convertDivergingAssociationPathToStrings($childDivergingAssociationPath)
194
            );
195
        }
196
        foreach (array_keys($childPaths) as $childPathIndex) {
197
            $childPaths[$childPathIndex] = $path . $childPaths[$childPathIndex];
198
        }
199
200
        return $childPaths;
201
    }
202
}
203