LocalisedVersionsTest::testArchive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 35
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 65
rs 9.36

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace TractorCow\Fluent\Tests\Extension;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\Versioned\Versioned;
7
use TractorCow\Fluent\Extension\FluentVersionedExtension;
8
use TractorCow\Fluent\State\FluentState;
9
use TractorCow\Fluent\Tests\Extension\Stub\FluentDataObject;
10
11
/**
12
 * Class LocalisedVersionsTest
13
 *
14
 * The main trick to test localised version is to switch between locales when creating content (versions)
15
 * Correctly localised versions will not be affected by frequent locale switching
16
 *
17
 * @package TractorCow\Fluent\Tests\Extension
18
 */
19
class LocalisedVersionsTest extends SapphireTest
20
{
21
    /**
22
     * @var string
23
     */
24
    protected static $fixture_file = 'LocalisedVersionsTest.yml';
25
26
    /**
27
     * @var array
28
     */
29
    protected static $extra_dataobjects = [
30
        FluentDataObject::class,
31
    ];
32
33
    protected static $required_extensions = [
34
        FluentDataObject::class => [
35
            Versioned::class,
36
            FluentVersionedExtension::class,
37
        ],
38
    ];
39
40
    /**
41
     * @var int
42
     */
43
    private $objectId;
44
45
    protected function setUp(): void
46
    {
47
        parent::setUp();
48
49
        FluentState::singleton()->withState(function (FluentState $state): void {
50
            $state->setLocale('en_NZ');
51
52
            // version 1
53
            /** @var FluentDataObject|Versioned|FluentVersionedExtension $object */
54
            $object = FluentDataObject::create();
55
            $object->Title = 'EN Title';
0 ignored issues
show
Bug introduced by
The property Title does not seem to exist on SilverStripe\Versioned\Versioned.
Loading history...
Bug introduced by
The property Title does not seem to exist on TractorCow\Fluent\Extens...luentVersionedExtension.
Loading history...
56
            $object->write();
0 ignored issues
show
Bug introduced by
The method write() does not exist on TractorCow\Fluent\Extens...luentVersionedExtension. ( Ignorable by Annotation )

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

56
            $object->/** @scrutinizer ignore-call */ 
57
                     write();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method write() does not exist on SilverStripe\Versioned\Versioned. ( Ignorable by Annotation )

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

56
            $object->/** @scrutinizer ignore-call */ 
57
                     write();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
58
            // version 2
59
            $object->Description = 'EN Description';
0 ignored issues
show
Bug introduced by
The property Description does not seem to exist on TractorCow\Fluent\Extens...luentVersionedExtension.
Loading history...
Bug introduced by
The property Description does not seem to exist on SilverStripe\Versioned\Versioned.
Loading history...
60
            $object->write();
61
62
            $this->objectId = (int) $object->ID;
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist on SilverStripe\Versioned\Versioned.
Loading history...
Bug introduced by
The property ID does not seem to exist on TractorCow\Fluent\Extens...luentVersionedExtension.
Loading history...
63
        });
64
65
        FluentState::singleton()->withState(function (FluentState $state): void {
66
            $state->setLocale('en_US');
67
68
            // version 3
69
            /** @var FluentDataObject|Versioned|FluentVersionedExtension $object */
70
            $object = FluentDataObject::get()->byID($this->objectId);
71
            $object->Title = 'US Title';
72
            $object->write();
73
74
            // version 4
75
            $object->Description = 'US Description';
76
            $object->write();
77
        });
78
79
        FluentState::singleton()->withState(function (FluentState $state): void {
80
            $state->setLocale('jp_JP');
81
82
            // version 5
83
            /** @var FluentDataObject|Versioned|FluentVersionedExtension $object */
84
            $object = FluentDataObject::get()->byID($this->objectId);
85
            $object->Title = 'JP Title';
86
            $object->write();
87
88
            // version 6
89
            $object->Description = 'JP Description';
90
            $object->write();
91
        });
92
    }
93
94
    /**
95
     * @param string|null $locale
96
     * @param int $expected
97
     * @dataProvider latestVersionsProvider
98
     */
99
    public function testGetLatestVersion(?string $locale, int $expected): void
100
    {
101
        FluentState::singleton()->withState(function (FluentState $state) use ($locale, $expected): void {
102
            $state->setLocale($locale);
103
104
            /** @var FluentDataObject|Versioned|FluentVersionedExtension $object */
105
            $object = Versioned::get_latest_version(FluentDataObject::class, $this->objectId);
106
107
            $this->assertNotNull($object);
108
            $this->assertTrue($object->exists());
109
            $this->assertFalse($object->isArchived());
0 ignored issues
show
Bug introduced by
The method isArchived() does not exist on TractorCow\Fluent\Tests\...n\Stub\FluentDataObject. 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

109
            $this->assertFalse($object->/** @scrutinizer ignore-call */ isArchived());
Loading history...
110
            $this->assertEquals($expected, $object->Version);
0 ignored issues
show
Bug Best Practice introduced by
The property Version does not exist on TractorCow\Fluent\Tests\...n\Stub\FluentDataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
111
        });
112
    }
113
114
    /**
115
     * @param string|null $locale
116
     * @param int $expected
117
     * @dataProvider latestVersionsProvider
118
     */
119
    public function testGetVersionNumberByStage(?string $locale, int $expected): void
120
    {
121
        FluentState::singleton()->withState(function (FluentState $state) use ($locale, $expected): void {
122
            $state->setLocale($locale);
123
124
            $version = Versioned::get_versionnumber_by_stage(
125
                FluentDataObject::class,
126
                Versioned::DRAFT,
127
                $this->objectId
128
            );
129
130
            $this->assertEquals($expected, (int) $version);
131
        });
132
    }
133
134
    /**
135
     * @param string|null $locale
136
     * @param int $expected
137
     * @dataProvider listVersionsProvider
138
     */
139
    public function testGetAllVersions(?string $locale, array $expected): void
140
    {
141
        FluentState::singleton()->withState(function (FluentState $state) use ($locale, $expected): void {
142
            $state->setLocale($locale);
143
144
            $versions = Versioned::get_all_versions(FluentDataObject::class, $this->objectId)
145
                ->sort('Version', 'ASC')
146
                ->columnUnique('Version');
147
148
            $this->assertSame($expected, $versions);
149
        });
150
    }
151
152
    public function testArchive(): void
153
    {
154
        FluentState::singleton()->withState(function (FluentState $state): void {
155
            $state->setLocale('en_US');
156
157
            /** @var FluentDataObject|Versioned|FluentVersionedExtension $object */
158
            $object = FluentDataObject::get()->byID($this->objectId);
159
            $object->doArchive();
0 ignored issues
show
Bug introduced by
The method doArchive() does not exist on TractorCow\Fluent\Tests\...n\Stub\FluentDataObject. 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

159
            $object->/** @scrutinizer ignore-call */ 
160
                     doArchive();
Loading history...
160
        });
161
162
        FluentState::singleton()->withState(function (FluentState $state): void {
163
            $state->setLocale('jp_JP');
164
165
            /** @var FluentDataObject|Versioned|FluentVersionedExtension $object */
166
            $object = FluentDataObject::get()->byID($this->objectId);
167
            $object->doArchive();
168
        });
169
170
        FluentState::singleton()->withState(function (FluentState $state): void {
171
            $state->setLocale('en_US');
172
173
            /** @var FluentDataObject|Versioned|FluentVersionedExtension $object */
174
            $object = Versioned::get_latest_version(FluentDataObject::class, $this->objectId);
175
176
            $this->assertNotNull($object);
177
            $this->assertTrue($object->isArchived());
178
            $this->assertTrue($object->hasArchiveInLocale());
0 ignored issues
show
Bug introduced by
The method hasArchiveInLocale() does not exist on TractorCow\Fluent\Tests\...n\Stub\FluentDataObject. 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

178
            $this->assertTrue($object->/** @scrutinizer ignore-call */ hasArchiveInLocale());
Loading history...
179
180
            // Restore
181
            $object->writeToStage(Versioned::DRAFT);
0 ignored issues
show
Bug introduced by
The method writeToStage() does not exist on TractorCow\Fluent\Tests\...n\Stub\FluentDataObject. 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

181
            $object->/** @scrutinizer ignore-call */ 
182
                     writeToStage(Versioned::DRAFT);
Loading history...
182
183
            $object = FluentDataObject::get()->byID($this->objectId);
184
            $this->assertEquals('US Description', $object->Description);
185
        });
186
187
        FluentState::singleton()->withState(function (FluentState $state): void {
188
            $state->setLocale('jp_JP');
189
190
            /** @var FluentDataObject|Versioned|FluentVersionedExtension $object */
191
            $object = Versioned::get_latest_version(FluentDataObject::class, $this->objectId);
192
            $this->assertNotNull($object);
193
194
            $this->assertNotNull($object);
195
            $this->assertTrue($object->isArchived());
196
            $this->assertTrue($object->hasArchiveInLocale());
197
198
            // Restore
199
            $object->writeToStage(Versioned::DRAFT);
200
201
            $object = FluentDataObject::get()->byID($this->objectId);
202
            $this->assertEquals('JP Description', $object->Description);
203
        });
204
205
        FluentState::singleton()->withState(function (FluentState $state): void {
206
            $state->setLocale('en_CA');
207
208
            /** @var FluentDataObject|Versioned|FluentVersionedExtension $object */
209
            $object = Versioned::get_latest_version(FluentDataObject::class, $this->objectId);
210
            $this->assertNull($object);
211
212
            $object = FluentDataObject::get()->byID($this->objectId);
213
214
            $this->assertNotNull($object);
215
            $this->assertFalse($object->isArchived());
216
            $this->assertFalse($object->hasArchiveInLocale());
217
        });
218
    }
219
220
    public function latestVersionsProvider(): array
221
    {
222
        return [
223
            [null, 6],
224
            ['en_NZ', 2],
225
            ['en_US', 4],
226
            ['jp_JP', 6],
227
        ];
228
    }
229
230
    public function listVersionsProvider(): array
231
    {
232
        return [
233
            [
234
                null,
235
                [1, 2, 3, 4, 5, 6],
236
            ],
237
            [
238
                'en_NZ',
239
                [1, 2],
240
            ],
241
            [
242
                'en_US',
243
                [3, 4],
244
            ],
245
            [
246
                'jp_JP',
247
                [5, 6],
248
            ],
249
        ];
250
    }
251
}
252