Failed Conditions
Pull Request — master (#26)
by Bob Olde
07:33
created

tests/services/LocalesTest.php (3 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft\Craft;
6
use Craft\BaseTest;
7
use Craft\LocaleModel;
8
use Craft\LocalizationService;
9
use NerdsAndCompany\Schematic\Models\Result;
10
use PHPUnit_Framework_MockObject_MockObject as Mock;
11
12
/**
13
 * Class LocalesTest.
14
 *
15
 * @author    Nerds & Company
16
 * @copyright Copyright (c) 2015, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @link      http://www.nerds.company
20
 *
21
 * @coversDefaultClass NerdsAndCompany\Schematic\Services\Locales
22
 * @covers ::__construct
23
 * @covers ::<!public>
24
 */
25
class LocalesTest extends BaseTest
26
{
27
    /**
28
     * @var Locales
29
     */
30
    private $schematicLocalesService;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function setUp()
36
    {
37
        $this->schematicLocalesService = new Locales();
38
    }
39
40
    /**
41
     * @return LocalizationService|Mock
42
     *
43
     * @param array $getSiteLocalesResponse
44
     * @param bool  $addSiteLocaleResponse
45
     *
46
     * @return Mock
47
     */
48
    protected function getMockLocalizationService(
49
        $getSiteLocaleIdsResponse = [],
50
        $getSiteLocalesResponse = [],
51
        $addSiteLocaleResponse = true
52
    ) {
53
        $mock = $this->getMockBuilder(LocalizationService::class)->getMock();
54
        $mock->expects($this->any())->method('getSiteLocaleIds')->willReturn($getSiteLocaleIdsResponse);
55
        $mock->expects($this->any())->method('getSiteLocales')->willReturn($getSiteLocalesResponse);
56
        $mock->expects($this->any())->method('addSiteLocale')->willReturn($addSiteLocaleResponse);
57
        $mock->expects($this->any())->method('reorderSiteLocales')->willReturn(true);
58
59
        return $mock;
60
    }
61
62
    /**
63
     * Test default import functionality.
64
     *
65
     * @covers ::import
66
     */
67
    public function testImportWithInstalledLocales()
68
    {
69
        $data = $this->getLocaleData();
70
71
        $mockLocalizationService = $this->getMockLocalizationService($data);
72
        $this->setComponent(Craft::app(), 'i18n', $mockLocalizationService);
73
74
        $import = $this->schematicLocalesService->import($data);
75
76
        $this->assertTrue($import instanceof Result);
0 ignored issues
show
The class NerdsAndCompany\Schematic\Models\Result does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
77
        $this->assertFalse($import->hasErrors());
78
    }
79
80
    /**
81
     * Test default import functionality.
82
     *
83
     * @covers ::import
84
     */
85
    public function testImportWithNotInstalledLocales()
86
    {
87
        $mockLocalizationService = $this->getMockLocalizationService();
88
        $this->setComponent(Craft::app(), 'i18n', $mockLocalizationService);
89
90
        $data = $this->getLocaleData();
91
92
        $import = $this->schematicLocalesService->import($data);
93
94
        $this->assertTrue($import instanceof Result);
0 ignored issues
show
The class NerdsAndCompany\Schematic\Models\Result does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
95
        $this->assertFalse($import->hasErrors());
96
    }
97
98
    /**
99
     * Test default import functionality.
100
     *
101
     * @covers ::import
102
     */
103
    public function testImportWithFailedInstalledLocales()
104
    {
105
        $mockLocalizationService = $this->getMockLocalizationService([], [], false);
106
        $this->setComponent(Craft::app(), 'i18n', $mockLocalizationService);
107
108
        $data = $this->getLocaleData();
109
110
        $import = $this->schematicLocalesService->import($data);
111
112
        $this->assertTrue($import instanceof Result);
0 ignored issues
show
The class NerdsAndCompany\Schematic\Models\Result does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
113
        $this->assertTrue($import->hasErrors());
114
    }
115
116
    /**
117
     * Test export functionality.
118
     *
119
     * @covers ::export
120
     */
121
    public function testExport()
122
    {
123
        $data = $this->getLocaleData();
124
125
        $locales = [];
126
        foreach ($data as $id) {
127
            $locales[] = new LocaleModel($id);
128
        }
129
130
        $mockLocalizationService = $this->getMockLocalizationService($data, $locales);
131
132
        $this->setComponent(Craft::app(), 'i18n', $mockLocalizationService);
133
134
        $export = $this->schematicLocalesService->export();
135
        $this->assertEquals($data, $export);
136
    }
137
138
    /**
139
     * Returns locale data.
140
     *
141
     * @return array
142
     */
143
    public function getLocaleData()
144
    {
145
        return ['nl', 'en', 'de'];
146
    }
147
}
148