Completed
Push — master ( 69a4a3...45ff21 )
by Damian
16s queued 13s
created

DuplicationTest::testCreateWithInheritedRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 25
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 35
rs 9.52
1
<?php
2
3
namespace TractorCow\Fluent\Tests\php\Extension\LocalisedCopyTest;
4
5
use SilverStripe\Dev\SapphireTest;
6
use TractorCow\Fluent\Extension\FluentExtension;
7
use TractorCow\Fluent\State\FluentState;
8
9
class DuplicationTest extends SapphireTest
10
{
11
    /**
12
     * @var string
13
     */
14
    protected static $fixture_file = 'DuplicationTest.yml';
15
16
    /**
17
     * @var array
18
     */
19
    protected static $extra_dataobjects = [
20
        Animal::class,
21
        Horse::class,
22
        Steed::class,
23
        Tail::class,
24
        Saddle::class,
25
    ];
26
27
    /**
28
     * @var array
29
     */
30
    protected static $required_extensions = [
31
        Animal::class => [
32
            FluentExtension::class,
33
        ],
34
    ];
35
36
    protected function setUp(): void
37
    {
38
        FluentState::singleton()->withState(function (FluentState $state): void {
39
            $state->setLocale('en_NZ');
40
41
            parent::setUp();
42
        });
43
    }
44
45
    /**
46
     * case: new object with defined relation is created
47
     * desired outcome: no additional changes
48
     *
49
     * @param bool $active
50
     * @dataProvider copyStateProvider
51
     */
52
    public function testCreateWithDefinedRelation(bool $active): void
53
    {
54
        FluentState::singleton()->withState(function (FluentState $state) use ($active): void {
55
            $state->setLocale('en_NZ');
56
57
            $tail = Tail::create();
58
            $tail->Title = 'New Tail';
59
            $tail->write();
60
61
            $tails = Tail::get()->sort('ID', 'DESC');
62
            $tailsCount = (int) $tails->count();
63
64
            $horse = Horse::create();
65
            $horse->Title = 'New Horse';
66
            $horse->TailID = $tail->ID;
67
68
            $horse->withLocalisedCopyState(function () use ($horse, $active) {
69
                $horse->setLocalisedCopyActive($active);
70
                $horse->write();
71
            });
72
73
            $this->assertCount($tailsCount, $tails);
74
            $this->assertGreaterThan(0, $horse->TailID);
75
            $this->assertEquals((int) $tail->ID, (int) $horse->TailID);
76
        });
77
    }
78
79
    /**
80
     * case: existing object with defined relation is localised into a new / existing locale
81
     * desired outcome:
82
     *
83
     * if localised copy is active
84
     * a new duplicted related object is created for the target locale
85
     *
86
     * if localised copy is inactive
87
     * desired outcome: no additional changes
88
     *
89
     * @param string $locale
90
     * @param bool $duplicated
91
     * @param bool $active
92
     * @dataProvider localesProvider
93
     */
94
    public function testEditWithDefinedRelation(string $locale, bool $duplicated, bool $active): void
95
    {
96
        FluentState::singleton()->withState(function (FluentState $state) use ($locale, $duplicated, $active): void {
97
            $state->setLocale($locale);
98
99
            $tails = Tail::get()->sort('ID', 'DESC');
100
            $tailsCount = (int) $tails->count();
101
102
            /** @var Horse|FluentExtension $horse */
103
            $horse = $this->objFromFixture(Horse::class, 'horse1');
104
            $horse->Title = 'Edited Title';
105
106
            $horse->withLocalisedCopyState(function () use ($horse, $active) {
0 ignored issues
show
Bug introduced by
The method withLocalisedCopyState() does not exist on TractorCow\Fluent\Tests\...LocalisedCopyTest\Horse. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            $horse->/** @scrutinizer ignore-call */ 
107
                    withLocalisedCopyState(function () use ($horse, $active) {
Loading history...
107
                $horse->setLocalisedCopyActive($active);
0 ignored issues
show
Bug introduced by
The method setLocalisedCopyActive() does not exist on TractorCow\Fluent\Tests\...LocalisedCopyTest\Horse. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
                $horse->/** @scrutinizer ignore-call */ 
108
                        setLocalisedCopyActive($active);
Loading history...
108
                $horse->write();
109
            });
110
111
            /** @var Tail $oldTail */
112
            $oldTail = $this->objFromFixture(Tail::class, 'tail1');
113
114
            $this->assertGreaterThan(0, $horse->TailID);
115
116
            if ($duplicated) {
117
                $this->assertCount($tailsCount + 1, $tails);
118
                $newTail = $tails->first();
119
                $this->assertNotEquals((int) $oldTail->ID, (int) $horse->TailID);
120
                $this->assertEquals((int) $newTail->ID, (int) $horse->TailID);
121
122
                return;
123
            }
124
125
            $this->assertCount($tailsCount, $tails);
126
            $this->assertEquals((int) $oldTail->ID, (int) $horse->TailID);
127
        });
128
    }
129
130
    /**
131
     * case: new object with inherited relation is created
132
     * desired outcome: no additional changes
133
     *
134
     * @param bool $active
135
     * @dataProvider copyStateProvider
136
     */
137
    public function testCreateWithInheritedRelation(bool $active): void
138
    {
139
        FluentState::singleton()->withState(function (FluentState $state) use ($active): void {
140
            $state->setLocale('en_NZ');
141
142
            $tail = Tail::create();
143
            $tail->Title = 'New Tail';
144
            $tail->write();
145
146
            $saddle = Saddle::create();
147
            $saddle->Title = 'New Saddle';
148
            $saddle->write();
149
150
            $tails = Tail::get()->sort('ID', 'DESC');
151
            $tailsCount = (int) $tails->count();
152
153
            $saddles = Saddle::get()->sort('ID', 'DESC');
154
            $saddlesCount = (int) $saddles->count();
155
156
            $steed = Steed::create();
157
            $steed->Title = 'New Steed';
158
            $steed->TailID = $tail->ID;
159
            $steed->SaddleID = $saddle->ID;
160
161
            $steed->withLocalisedCopyState(function () use ($steed, $active) {
162
                $steed->setLocalisedCopyActive($active);
163
                $steed->write();
164
            });
165
166
            $this->assertCount($tailsCount, $tails);
167
            $this->assertCount($saddlesCount, $saddles);
168
            $this->assertGreaterThan(0, $steed->TailID);
169
            $this->assertGreaterThan(0, $steed->SaddleID);
170
            $this->assertEquals((int) $tail->ID, (int) $steed->TailID);
171
            $this->assertEquals((int) $saddle->ID, (int) $steed->SaddleID);
172
        });
173
    }
174
175
    /**
176
     * case: existing object with inherited relation is localised into a new / existing locale
177
     * desired outcome:
178
     *
179
     * if localised copy is active
180
     * a new duplicted related object is created for the target locale
181
     *
182
     * if localised copy is inactive
183
     * desired outcome: no additional changes
184
     *
185
     * @param string $locale
186
     * @param bool $duplicated
187
     * @param bool $active
188
     * @dataProvider localesProvider
189
     */
190
    public function testEditWithInheritedRelation(string $locale, bool $duplicated, bool $active): void
191
    {
192
        FluentState::singleton()->withState(function (FluentState $state) use ($locale, $duplicated, $active): void {
193
            $state->setLocale($locale);
194
195
            $tails = Tail::get()->sort('ID', 'DESC');
196
            $tailsCount = (int) $tails->count();
197
198
            $saddles = Saddle::get()->sort('ID', 'DESC');
199
            $saddlesCount = (int) $saddles->count();
200
201
            /** @var Steed|FluentExtension $steed */
202
            $steed = $this->objFromFixture(Steed::class, 'steed1');
203
            $steed->Title = 'Edited Title';
204
205
            $steed->withLocalisedCopyState(function () use ($steed, $active) {
0 ignored issues
show
Bug introduced by
The method withLocalisedCopyState() does not exist on TractorCow\Fluent\Tests\...LocalisedCopyTest\Steed. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
            $steed->/** @scrutinizer ignore-call */ 
206
                    withLocalisedCopyState(function () use ($steed, $active) {
Loading history...
206
                $steed->setLocalisedCopyActive($active);
0 ignored issues
show
Bug introduced by
The method setLocalisedCopyActive() does not exist on TractorCow\Fluent\Tests\...LocalisedCopyTest\Steed. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
                $steed->/** @scrutinizer ignore-call */ 
207
                        setLocalisedCopyActive($active);
Loading history...
207
                $steed->write();
208
            });
209
210
            /** @var Tail $tail */
211
            $oldTail = $this->objFromFixture(Tail::class, 'tail2');
212
213
            /** @var Saddle $tail */
214
            $oldSaddle = $this->objFromFixture(Saddle::class, 'saddle1');
215
216
            $this->assertGreaterThan(0, $steed->TailID);
217
            $this->assertGreaterThan(0, $steed->SaddleID);
218
219
            if ($duplicated) {
220
                $this->assertCount($tailsCount + 1, $tails);
221
                $this->assertCount($saddlesCount + 1, $saddles);
222
223
                $newTail = $tails->first();
224
                $newSaddle = $saddles->first();
225
226
                $this->assertNotEquals((int) $oldTail->ID, (int) $steed->TailID);
227
                $this->assertNotEquals((int) $oldSaddle->ID, (int) $steed->SaddleID);
228
                $this->assertEquals((int) $newTail->ID, (int) $steed->TailID);
229
                $this->assertEquals((int) $newSaddle->ID, (int) $steed->SaddleID);
230
231
                return;
232
            }
233
234
            $this->assertCount($tailsCount, $tails);
235
            $this->assertCount($saddlesCount, $saddles);
236
            $this->assertEquals((int) $oldTail->ID, (int) $steed->TailID);
237
            $this->assertEquals((int) $oldSaddle->ID, (int) $steed->SaddleID);
238
        });
239
    }
240
241
    public function localesProvider(): array
242
    {
243
        return [
244
            ['en_NZ', false, true],
245
            ['en_NZ', false, false],
246
            ['ja_JP', false, false],
247
            ['ja_JP', true, true],
248
        ];
249
    }
250
251
    public function copyStateProvider(): array
252
    {
253
        return [
254
            [false],
255
            [true],
256
        ];
257
    }
258
}
259