Completed
Push — fix_ezp29385 ( 36b56f...415bc3 )
by
unknown
56:19 queued 22:25
created

SectionBase::testNewClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Integration\SectionTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Tests\Service\Integration;
10
11
use eZ\Publish\Core\Repository\Tests\Service\Integration\Base as BaseServiceTest;
12
use eZ\Publish\API\Repository\Values\Content\Section;
13
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
14
use eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException;
15
use eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException as PropertyNotFound;
16
use eZ\Publish\API\Repository\Tests\BaseTest as APIBaseTest;
17
18
/**
19
 * Test case for Section Service using InMemory storage class.
20
 */
21
abstract class SectionBase extends BaseServiceTest
22
{
23
    /**
24
     * Test a new class and default values on properties.
25
     *
26
     * @covers \eZ\Publish\API\Repository\Values\Content\Section::__construct
27
     */
28
    public function testNewClass()
29
    {
30
        $section = new Section();
31
32
        $this->assertPropertiesCorrect(
33
            array(
34
                'id' => null,
35
                'identifier' => null,
36
                'name' => null,
37
            ),
38
            $section
39
        );
40
    }
41
42
    /**
43
     * Test retrieving missing property.
44
     *
45
     * @covers \eZ\Publish\API\Repository\Values\Content\Section::__get
46
     */
47
    public function testMissingProperty()
48
    {
49
        try {
50
            $section = new Section();
51
            $value = $section->notDefined;
0 ignored issues
show
Documentation introduced by
The property notDefined does not exist on object<eZ\Publish\API\Re...Values\Content\Section>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
            self::fail('Succeeded getting non existing property');
53
        } catch (PropertyNotFound $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
54
        }
55
    }
56
57
    /**
58
     * Test setting read only property.
59
     *
60
     * @covers \eZ\Publish\API\Repository\Values\Content\Section::__set
61
     */
62
    public function testReadOnlyProperty()
63
    {
64
        try {
65
            $section = new Section();
66
            $section->id = 22;
67
            self::fail('Succeeded setting read only property');
68
        } catch (PropertyReadOnlyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
69
        }
70
    }
71
72
    /**
73
     * Test if property exists.
74
     *
75
     * @covers \eZ\Publish\API\Repository\Values\Content\Section::__isset
76
     */
77
    public function testIsPropertySet()
78
    {
79
        $section = new Section();
80
        $value = isset($section->notDefined);
81
        self::assertEquals(false, $value);
82
83
        $value = isset($section->id);
84
        self::assertEquals(true, $value);
85
    }
86
87
    /**
88
     * Test unsetting a property.
89
     *
90
     * @covers \eZ\Publish\API\Repository\Values\Content\Section::__unset
91
     */
92
    public function testUnsetProperty()
93
    {
94
        $section = new Section(array('id' => 1));
95
        try {
96
            unset($section->id);
97
            self::fail('Unsetting read-only property succeeded');
98
        } catch (PropertyReadOnlyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
99
        }
100
    }
101
102
    /**
103
     * Test service function for creating sections.
104
     *
105
     * @covers \eZ\Publish\Core\Repository\SectionService::createSection
106
     */
107 View Code Duplication
    public function testCreateSection()
108
    {
109
        $sectionService = $this->repository->getSectionService();
110
111
        $sectionCreateStruct = $sectionService->newSectionCreateStruct();
112
        $sectionCreateStruct->identifier = 'test';
113
        $sectionCreateStruct->name = 'Test';
114
115
        $createdSection = $sectionService->createSection($sectionCreateStruct);
116
117
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Section', $createdSection);
118
        self::assertGreaterThan(0, $createdSection->id);
119
120
        $this->assertStructPropertiesCorrect(
121
            $sectionCreateStruct,
122
            $createdSection
123
        );
124
    }
125
126
    /**
127
     * Test service function for creating sections throwing InvalidArgumentException.
128
     *
129
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
130
     * @covers \eZ\Publish\Core\Repository\SectionService::createSection
131
     */
132
    public function testCreateSectionThrowsInvalidArgumentException()
133
    {
134
        $sectionService = $this->repository->getSectionService();
135
136
        $sectionCreateStruct = $sectionService->newSectionCreateStruct();
137
        $sectionCreateStruct->identifier = 'standard';
138
        $sectionCreateStruct->name = 'Standard';
139
140
        $sectionService->createSection($sectionCreateStruct);
141
    }
142
143
    /**
144
     * Test service function for updating sections.
145
     *
146
     * @covers \eZ\Publish\Core\Repository\SectionService::updateSection
147
     */
148 View Code Duplication
    public function testUpdateSection()
149
    {
150
        $sectionService = $this->repository->getSectionService();
151
152
        $loadedSection = $sectionService->loadSection(1);
153
154
        $sectionUpdateStruct = $sectionService->newSectionUpdateStruct();
155
        $sectionUpdateStruct->identifier = 'test';
156
        $sectionUpdateStruct->name = 'Test';
157
158
        $updatedSection = $sectionService->updateSection($loadedSection, $sectionUpdateStruct);
159
160
        self::assertEquals($loadedSection->id, $updatedSection->id);
161
162
        $this->assertStructPropertiesCorrect(
163
            $sectionUpdateStruct,
164
            $updatedSection
165
        );
166
    }
167
168
    /**
169
     * Test service function for updating sections.
170
     *
171
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
172
     * @covers \eZ\Publish\Core\Repository\SectionService::updateSection
173
     */
174
    public function testUpdateSectionThrowsInvalidArgumentException()
175
    {
176
        $sectionService = $this->repository->getSectionService();
177
178
        $loadedSection = $sectionService->loadSectionByIdentifier('standard');
179
180
        $sectionUpdateStruct = $sectionService->newSectionUpdateStruct();
181
        $sectionUpdateStruct->identifier = 'media';
182
        $sectionUpdateStruct->name = 'Media';
183
184
        $sectionService->updateSection($loadedSection, $sectionUpdateStruct);
185
    }
186
187
    /**
188
     * Test service function for loading sections.
189
     *
190
     * @covers \eZ\Publish\Core\Repository\SectionService::loadSection
191
     */
192 View Code Duplication
    public function testLoadSection()
193
    {
194
        $sectionService = $this->repository->getSectionService();
195
196
        $section = $sectionService->loadSection(1);
197
198
        $this->assertPropertiesCorrect(
199
            array(
200
                'id' => 1,
201
                'name' => 'Standard',
202
                'identifier' => 'standard',
203
            ),
204
            $section
205
        );
206
    }
207
208
    /**
209
     * Test service function for loading sections throwing NotFoundException.
210
     *
211
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
212
     * @covers \eZ\Publish\Core\Repository\SectionService::loadSection
213
     */
214
    public function testLoadSectionThrowsNotFoundException()
215
    {
216
        $sectionService = $this->repository->getSectionService();
217
218
        $sectionService->loadSection(APIBaseTest::DB_INT_MAX);
219
    }
220
221
    /**
222
     * Test service function for loading all sections.
223
     *
224
     * @covers \eZ\Publish\Core\Repository\SectionService::loadSections
225
     */
226
    public function testLoadSections()
227
    {
228
        $sections = $this->repository->getSectionService()->loadSections();
229
230
        self::assertInternalType('array', $sections);
231
        self::assertGreaterThan(0, count($sections));
232
233
        foreach ($sections as $section) {
234
            self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Section', $section);
235
        }
236
    }
237
238
    /**
239
     * Test service function for loading section by identifier.
240
     *
241
     * @covers \eZ\Publish\Core\Repository\SectionService::loadSectionByIdentifier
242
     */
243 View Code Duplication
    public function testLoadSectionByIdentifier()
244
    {
245
        $sectionService = $this->repository->getSectionService();
246
247
        $section = $sectionService->loadSectionByIdentifier('standard');
248
249
        $this->assertPropertiesCorrect(
250
            array(
251
                'id' => 1,
252
                'name' => 'Standard',
253
                'identifier' => 'standard',
254
            ),
255
            $section
256
        );
257
    }
258
259
    /**
260
     * Test service function for loading section by identifier throwing NotFoundException.
261
     *
262
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
263
     * @covers \eZ\Publish\Core\Repository\SectionService::loadSectionByIdentifier
264
     */
265
    public function testLoadSectionByIdentifierThrowsNotFoundException()
266
    {
267
        $sectionService = $this->repository->getSectionService();
268
269
        $sectionService->loadSectionByIdentifier('non-existing');
270
    }
271
272
    /**
273
     * Test service function for counting content assigned to section.
274
     *
275
     * @covers \eZ\Publish\Core\Repository\SectionService::countAssignedContents
276
     */
277
    public function testCountAssignedContents()
278
    {
279
        $sectionService = $this->repository->getSectionService();
280
281
        $section = $sectionService->loadSection(1);
282
        $contentCount = $sectionService->countAssignedContents($section);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
283
284
        self::assertGreaterThan(0, $contentCount);
285
286
        $sectionCreateStruct = $sectionService->newSectionCreateStruct();
287
        $sectionCreateStruct->identifier = 'test';
288
        $sectionCreateStruct->name = 'Test';
289
290
        $newSection = $sectionService->createSection($sectionCreateStruct);
291
        $contentCount = $sectionService->countAssignedContents($newSection);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
292
293
        self::assertEquals(0, $contentCount);
294
    }
295
296
    /**
297
     * Test service function for assigning section to content.
298
     *
299
     * @covers \eZ\Publish\Core\Repository\SectionService::assignSection
300
     */
301 View Code Duplication
    public function testAssignSection()
302
    {
303
        $sectionService = $this->repository->getSectionService();
304
        $contentService = $this->repository->getContentService();
305
306
        $section = $sectionService->loadSection(1);
307
        $contentInfo = $contentService->loadContentInfo(4);
308
309
        self::assertEquals(2, $contentInfo->sectionId);
310
311
        $sectionService->assignSection($contentInfo, $section);
312
313
        $contentInfo = $contentService->loadContentInfo(4);
314
315
        self::assertEquals($section->id, $contentInfo->sectionId);
316
    }
317
318
    /**
319
     * Test service function for deleting sections.
320
     *
321
     * @covers \eZ\Publish\Core\Repository\SectionService::deleteSection
322
     */
323 View Code Duplication
    public function testDeleteSection()
324
    {
325
        $sectionService = $this->repository->getSectionService();
326
327
        $sectionCreateStruct = $sectionService->newSectionCreateStruct();
328
        $sectionCreateStruct->identifier = 'test';
329
        $sectionCreateStruct->name = 'Test';
330
331
        $newSection = $sectionService->createSection($sectionCreateStruct);
332
        $sectionService->deleteSection($newSection);
333
334
        try {
335
            $sectionService->loadSection($newSection->id);
336
            self::fail('Section is still returned after being deleted');
337
        } catch (NotFoundException $e) {
338
            // Do nothing
339
        }
340
    }
341
342
    /**
343
     * Test service function for deleting sections throwing NotFoundException.
344
     *
345
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
346
     * @covers \eZ\Publish\Core\Repository\SectionService::deleteSection
347
     */
348
    public function testDeleteSectionThrowsNotFoundException()
349
    {
350
        $sectionService = $this->repository->getSectionService();
351
352
        $section = new Section(array('id' => APIBaseTest::DB_INT_MAX));
353
354
        $sectionService->deleteSection($section);
355
    }
356
357
    /**
358
     * Test service function for deleting sections throwing BadStateException.
359
     *
360
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
361
     * @covers \eZ\Publish\Core\Repository\SectionService::deleteSection
362
     */
363
    public function testDeleteSectionThrowsBadStateException()
364
    {
365
        $sectionService = $this->repository->getSectionService();
366
367
        $section = $sectionService->loadSection(1);
368
369
        $sectionService->deleteSection($section);
370
    }
371
372
    /**
373
     * Test service function for creating new SectionCreateStruct.
374
     *
375
     * @covers \eZ\Publish\Core\Repository\SectionService::newSectionCreateStruct
376
     */
377 View Code Duplication
    public function testNewSectionCreateStruct()
378
    {
379
        $sectionService = $this->repository->getSectionService();
380
381
        $sectionCreateStruct = $sectionService->newSectionCreateStruct();
382
383
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\SectionCreateStruct', $sectionCreateStruct);
384
385
        $this->assertPropertiesCorrect(
386
            array(
387
                'identifier' => null,
388
                'name' => null,
389
            ),
390
            $sectionCreateStruct
391
        );
392
    }
393
394
    /**
395
     * Test service function for creating new SectionUpdateStruct.
396
     *
397
     * @covers \eZ\Publish\Core\Repository\SectionService::newSectionUpdateStruct
398
     */
399 View Code Duplication
    public function testNewSectionUpdateStruct()
400
    {
401
        $sectionService = $this->repository->getSectionService();
402
403
        $sectionUpdateStruct = $sectionService->newSectionUpdateStruct();
404
405
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\SectionUpdateStruct', $sectionUpdateStruct);
406
407
        $this->assertPropertiesCorrect(
408
            array(
409
                'identifier' => null,
410
                'name' => null,
411
            ),
412
            $sectionUpdateStruct
413
        );
414
    }
415
}
416