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

LanguageBase::testNewClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Integration\LanguageBase 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\Language;
13
use eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException as PropertyNotFound;
14
use eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException;
15
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
16
use eZ\Publish\API\Repository\Tests\BaseTest as APIBaseTest;
17
18
/**
19
 * Test case for Language Service.
20
 */
21
abstract class LanguageBase extends BaseServiceTest
22
{
23
    /**
24
     * Test a new class and default values on properties.
25
     *
26
     * @covers \eZ\Publish\API\Repository\Values\Content\Language::__construct
27
     */
28
    public function testNewClass()
29
    {
30
        $language = new Language();
31
32
        $this->assertPropertiesCorrect(
33
            array(
34
                'id' => null,
35
                'languageCode' => null,
36
                'name' => null,
37
                'enabled' => null,
38
            ),
39
            $language
40
        );
41
    }
42
43
    /**
44
     * Test retrieving missing property.
45
     *
46
     * @covers \eZ\Publish\API\Repository\Values\Content\Language::__get
47
     */
48
    public function testMissingProperty()
49
    {
50
        try {
51
            $language = new Language();
52
            $value = $language->notDefined;
0 ignored issues
show
Documentation introduced by
The property notDefined does not exist on object<eZ\Publish\API\Re...alues\Content\Language>. 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...
53
            self::fail('Succeeded getting non existing property');
54
        } catch (PropertyNotFound $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
55
        }
56
    }
57
58
    /**
59
     * Test setting read only property.
60
     *
61
     * @covers \eZ\Publish\API\Repository\Values\Content\Language::__set
62
     */
63
    public function testReadOnlyProperty()
64
    {
65
        try {
66
            $language = new Language();
67
            $language->id = 42;
68
            self::fail('Succeeded setting read only property');
69
        } catch (PropertyReadOnlyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
70
        }
71
    }
72
73
    /**
74
     * Test if property exists.
75
     *
76
     * @covers \eZ\Publish\API\Repository\Values\Content\Language::__isset
77
     */
78
    public function testIsPropertySet()
79
    {
80
        $language = new Language();
81
        $value = isset($language->notDefined);
82
        self::assertEquals(false, $value);
83
84
        $value = isset($language->id);
85
        self::assertEquals(true, $value);
86
    }
87
88
    /**
89
     * Test unsetting a property.
90
     *
91
     * @covers \eZ\Publish\API\Repository\Values\Content\Language::__unset
92
     */
93
    public function testUnsetProperty()
94
    {
95
        $language = new Language(array('id' => 2));
96
        try {
97
            unset($language->id);
98
            self::fail('Unsetting read-only property succeeded');
99
        } catch (PropertyReadOnlyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
100
        }
101
    }
102
103
    /**
104
     * Test service method for creating language.
105
     *
106
     * @covers \eZ\Publish\API\Repository\LanguageService::createLanguage
107
     */
108
    public function testCreateLanguage()
109
    {
110
        $service = $this->repository->getContentLanguageService();
111
112
        $languageCreateStruct = $service->newLanguageCreateStruct();
113
        $languageCreateStruct->languageCode = 'test-TEST';
114
        $languageCreateStruct->name = 'test';
115
116
        $newLanguage = $service->createLanguage($languageCreateStruct);
117
118
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Language', $newLanguage);
119
120
        self::assertGreaterThan(0, $newLanguage->id);
121
122
        $this->assertPropertiesCorrect(
123
            array(
124
                'languageCode' => $languageCreateStruct->languageCode,
125
                'name' => $languageCreateStruct->name,
126
                'enabled' => $languageCreateStruct->enabled,
127
            ),
128
            $newLanguage
129
        );
130
    }
131
132
    /**
133
     * Test service method for creating language throwing InvalidArgumentException.
134
     *
135
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
136
     * @covers \eZ\Publish\API\Repository\LanguageService::createLanguage
137
     */
138
    public function testCreateLanguageThrowsInvalidArgumentException()
139
    {
140
        $service = $this->repository->getContentLanguageService();
141
142
        $languageCreateStruct = $service->newLanguageCreateStruct();
143
        $languageCreateStruct->languageCode = 'eng-GB';
144
        $languageCreateStruct->name = 'English';
145
146
        $service->createLanguage($languageCreateStruct);
147
    }
148
149
    /**
150
     * Test service method for updating language name.
151
     *
152
     * @covers \eZ\Publish\API\Repository\LanguageService::updateLanguageName
153
     */
154
    public function testUpdateLanguageName()
155
    {
156
        $languageService = $this->repository->getContentLanguageService();
157
158
        $language = $languageService->loadLanguage('eng-GB');
159
        self::assertEquals('English (United Kingdom)', $language->name);
160
161
        $updatedLanguage = $languageService->updateLanguageName($language, 'English');
162
163
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Language', $updatedLanguage);
164
165
        $this->assertPropertiesCorrect(
166
            array(
167
                'id' => $language->id,
168
                'languageCode' => $language->languageCode,
169
                'name' => 'English',
170
                'enabled' => $language->enabled,
171
            ),
172
            $updatedLanguage
173
        );
174
    }
175
176
    /**
177
     * Test service method for updating language name throwing InvalidArgumentException.
178
     *
179
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
180
     * @covers \eZ\Publish\API\Repository\LanguageService::updateLanguageName
181
     */
182
    public function testUpdateLanguageNameThrowsInvalidArgumentException()
183
    {
184
        $languageService = $this->repository->getContentLanguageService();
185
186
        $language = $languageService->loadLanguage('eng-GB');
187
        $languageService->updateLanguageName($language, 1);
188
    }
189
190
    /**
191
     * Test service method for enabling language.
192
     *
193
     * @covers \eZ\Publish\API\Repository\LanguageService::enableLanguage
194
     */
195
    public function testEnableLanguage()
196
    {
197
        $languageService = $this->repository->getContentLanguageService();
198
199
        $language = $languageService->loadLanguage('eng-GB');
200
        self::assertEquals(true, $language->enabled);
201
202
        $updatedLanguage = $languageService->disableLanguage($language);
203
204
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Language', $updatedLanguage);
205
206
        $this->assertPropertiesCorrect(
207
            array(
208
                'id' => $language->id,
209
                'languageCode' => $language->languageCode,
210
                'name' => $language->name,
211
                'enabled' => false,
212
            ),
213
            $updatedLanguage
214
        );
215
216
        $finalLanguage = $languageService->enableLanguage($updatedLanguage);
217
218
        $this->assertPropertiesCorrect(
219
            array(
220
                'id' => $updatedLanguage->id,
221
                'languageCode' => $updatedLanguage->languageCode,
222
                'name' => $updatedLanguage->name,
223
                'enabled' => true,
224
            ),
225
            $finalLanguage
226
        );
227
    }
228
229
    /**
230
     * Test service method for disabling language.
231
     *
232
     * @covers \eZ\Publish\API\Repository\LanguageService::disableLanguage
233
     */
234
    public function testDisableLanguage()
235
    {
236
        $languageService = $this->repository->getContentLanguageService();
237
238
        $language = $languageService->loadLanguage('eng-GB');
239
        self::assertEquals(true, $language->enabled);
240
241
        $updatedLanguage = $languageService->disableLanguage($language);
242
243
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Language', $updatedLanguage);
244
245
        $this->assertPropertiesCorrect(
246
            array(
247
                'id' => $language->id,
248
                'languageCode' => $language->languageCode,
249
                'name' => $language->name,
250
                'enabled' => false,
251
            ),
252
            $updatedLanguage
253
        );
254
    }
255
256
    /**
257
     * Test service method for loading language.
258
     *
259
     * @covers \eZ\Publish\API\Repository\LanguageService::loadLanguage
260
     */
261 View Code Duplication
    public function testLoadLanguage()
262
    {
263
        $language = $this->repository->getContentLanguageService()->loadLanguage('eng-GB');
264
265
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Language', $language);
266
        self::assertGreaterThan(0, $language->id);
267
268
        $this->assertPropertiesCorrect(
269
            array(
270
                'languageCode' => 'eng-GB',
271
                'name' => 'English (United Kingdom)',
272
                'enabled' => true,
273
            ),
274
            $language
275
        );
276
    }
277
278
    /**
279
     * Test service method for loading language throwing InvalidArgumentException.
280
     *
281
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
282
     * @covers \eZ\Publish\API\Repository\LanguageService::loadLanguage
283
     */
284
    public function testLoadLanguageThrowsInvalidArgumentException()
285
    {
286
        $this->repository->getContentLanguageService()->loadLanguage(PHP_INT_MAX);
287
    }
288
289
    /**
290
     * Test service function for loading language throwing NotFoundException.
291
     *
292
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
293
     * @covers \eZ\Publish\API\Repository\LanguageService::loadLanguage
294
     */
295
    public function testLoadLanguageThrowsNotFoundException()
296
    {
297
        $this->repository->getContentLanguageService()->loadLanguage('ita-FR');
298
    }
299
300
    /**
301
     * Test service method for loading all languages.
302
     *
303
     * @covers \eZ\Publish\API\Repository\LanguageService::loadLanguages
304
     */
305
    public function testLoadLanguages()
306
    {
307
        $languageService = $this->repository->getContentLanguageService();
308
309
        $languages = $languageService->loadLanguages();
310
311
        self::assertInternalType('array', $languages);
312
        self::assertNotEmpty($languages);
313
314
        foreach ($languages as $language) {
315
            self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Language', $language);
316
        }
317
    }
318
319
    /**
320
     * Test service method for loading language by ID.
321
     *
322
     * @covers \eZ\Publish\API\Repository\LanguageService::loadLanguageById
323
     */
324 View Code Duplication
    public function testLoadLanguageById()
325
    {
326
        $language = $this->repository->getContentLanguageService()->loadLanguageById(2);
327
328
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Language', $language);
329
330
        $this->assertPropertiesCorrect(
331
            array(
332
                'id' => 2,
333
                'languageCode' => 'eng-US',
334
                'name' => 'English (American)',
335
                'enabled' => true,
336
            ),
337
            $language
338
        );
339
    }
340
341
    /**
342
     * Test service method for loading language by ID throwing NotFoundException.
343
     *
344
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
345
     * @covers \eZ\Publish\API\Repository\LanguageService::loadLanguageById
346
     */
347
    public function testLoadLanguageByIdThrowsNotFoundException()
348
    {
349
        $this->repository->getContentLanguageService()->loadLanguageById(APIBaseTest::DB_INT_MAX);
350
    }
351
352
    /**
353
     * Test service method for deleting language.
354
     *
355
     * @covers \eZ\Publish\API\Repository\LanguageService::deleteLanguage
356
     */
357 View Code Duplication
    public function testDeleteLanguage()
358
    {
359
        $languageService = $this->repository->getContentLanguageService();
360
361
        $languageCreateStruct = $languageService->newLanguageCreateStruct();
362
        $languageCreateStruct->name = 'test';
363
        $languageCreateStruct->languageCode = 'test-TEST';
364
365
        $newLanguage = $languageService->createLanguage($languageCreateStruct);
366
        $languageService->deleteLanguage($newLanguage);
367
368
        try {
369
            $languageService->loadLanguage($languageCreateStruct->languageCode);
370
            self::fail('Language is still returned after being deleted');
371
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
372
        }
373
    }
374
375
    /**
376
     * Test service method for deleting language throwing InvalidArgumentException.
377
     *
378
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
379
     * @covers \eZ\Publish\API\Repository\LanguageService::deleteLanguage
380
     */
381
    public function testDeleteLanguageThrowsInvalidArgumentException()
382
    {
383
        $languageService = $this->repository->getContentLanguageService();
384
385
        $language = $languageService->loadLanguage('eng-GB');
386
        $languageService->deleteLanguage($language);
387
    }
388
389
    /**
390
     * Test service method for fetching the default language code.
391
     *
392
     * @covers \eZ\Publish\API\Repository\LanguageService::getDefaultLanguageCode
393
     */
394
    public function testGetDefaultLanguageCode()
395
    {
396
        $defaultLanguageCode = $this->repository->getContentLanguageService()->getDefaultLanguageCode();
397
398
        self::assertEquals('eng-GB', $defaultLanguageCode);
399
    }
400
401
    /**
402
     * Test service method for creating a new language create struct object.
403
     *
404
     * @covers \eZ\Publish\API\Repository\LanguageService::newLanguageCreateStruct
405
     */
406
    public function testNewLanguageCreateStruct()
407
    {
408
        $languageCreateStruct = $this->repository->getContentLanguageService()->newLanguageCreateStruct();
409
410
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LanguageCreateStruct', $languageCreateStruct);
411
412
        $this->assertPropertiesCorrect(
413
            array(
414
                'languageCode' => null,
415
                'name' => null,
416
                'enabled' => true,
417
            ),
418
            $languageCreateStruct
419
        );
420
    }
421
}
422