GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

EmailsTest::getMockDbCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\BaseTest;
6
use Craft\Commerce_EmailModel;
7
use Craft\Commerce_EmailsService;
8
use Craft\Craft;
9
use Craft\DbCommand;
10
use Craft\DbConnection;
11
use NerdsAndCompany\Schematic\Models\Result;
12
use PHPUnit_Framework_MockObject_MockObject as Mock;
13
14
/**
15
 * Class EmailsTest.
16
 *
17
 * @author    Nerds & Company
18
 * @copyright Copyright (c) 2015-2017, Nerds & Company
19
 * @license   MIT
20
 *
21
 * @see      http://www.nerds.company
22
 *
23
 * @coversDefaultClass \NerdsAndCompany\Schematic\Commerce\Services\Emails
24
 * @covers ::__construct
25
 * @covers ::<!public>
26
 */
27
class EmailsTest extends BaseTest
28
{
29
    //==============================================================================================================
30
    //=================================================  TESTS  ====================================================
31
    //==============================================================================================================
32
33
    /**
34
     * @covers ::export
35
     * @dataProvider provideValidEmails
36
     *
37
     * @param EmailModel[] $emails
38
     * @param array        $expectedResult
39
     */
40
    public function testSuccessfulExport(array $emails, array $expectedResult = [])
41
    {
42
        $schematicEmailsService = new Emails();
43
44
        $actualResult = $schematicEmailsService->export($emails);
45
46
        $this->assertSame($expectedResult, $actualResult);
47
    }
48
49
    /**
50
     * @covers ::import
51
     * @dataProvider provideValidEmailDefinitions
52
     *
53
     * @param array $emailDefinitions
54
     */
55
    public function testSuccessfulImport(array $emailDefinitions)
56
    {
57
        $this->setMockEmailsService();
58
        $this->setMockDbConnection();
59
60
        $schematicEmailsService = new Emails();
61
62
        $import = $schematicEmailsService->import($emailDefinitions);
63
64
        $this->assertInstanceOf(Result::class, $import);
65
        $this->assertFalse($import->hasErrors());
66
    }
67
68
    /**
69
     * @covers ::import
70
     * @dataProvider provideValidEmailDefinitions
71
     *
72
     * @param array $emailDefinitions
73
     */
74
    public function testImportWithForceOption(array $emailDefinitions)
75
    {
76
        $this->setMockEmailsService();
77
        $this->setMockDbConnection();
78
79
        $schematicEmailsService = new Emails();
80
81
        $import = $schematicEmailsService->import($emailDefinitions, true);
82
83
        $this->assertInstanceOf(Result::class, $import);
84
        $this->assertFalse($import->hasErrors());
85
    }
86
87
    //==============================================================================================================
88
    //==============================================  PROVIDERS  ===================================================
89
    //==============================================================================================================
90
91
    /**
92
     * @return array
93
     */
94
    public function provideValidEmails()
95
    {
96
        return [
97
            'single email' => [
98
                'Emails' => [
99
                    'email1' => $this->getMockEmail(1),
100
                ],
101
                'expectedResult' => [
102
                    'emailName1' => [
103
                        'name' => 'emailName1',
104
                        'subject' => null,
105
                        'recipientType' => null,
106
                        'to' => null,
107
                        'bcc' => null,
108
                        'enabled' => null,
109
                        'templatePath' => null,
110
                    ],
111
                ],
112
            ],
113
            'multiple emails' => [
114
                'Emails' => [
115
                    'email1' => $this->getMockEmail(1),
116
                    'email2' => $this->getMockEmail(2),
117
                ],
118
                'expectedResult' => [
119
                    'emailName1' => [
120
                        'name' => 'emailName1',
121
                        'subject' => null,
122
                        'recipientType' => null,
123
                        'to' => null,
124
                        'bcc' => null,
125
                        'enabled' => null,
126
                        'templatePath' => null,
127
                    ],
128
                    'emailName2' => [
129
                        'name' => 'emailName2',
130
                        'subject' => null,
131
                        'recipientType' => null,
132
                        'to' => null,
133
                        'bcc' => null,
134
                        'enabled' => null,
135
                        'templatePath' => null,
136
                    ],
137
                ],
138
            ],
139
        ];
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function provideValidEmailDefinitions()
146
    {
147
        return [
148
            'emptyArray' => [
149
                'emailDefinitions' => [],
150
            ],
151
            'single email' => [
152
                'emailDefinitions' => [
153
                    'emailName1' => [
154
                        'name' => 'emailName1',
155
                        'subject' => null,
156
                        'recipientType' => null,
157
                        'to' => null,
158
                        'bcc' => null,
159
                        'enabled' => null,
160
                        'templatePath' => null,
161
                    ],
162
                ],
163
            ],
164
        ];
165
    }
166
167
    //==============================================================================================================
168
    //=================================================  MOCKS  ====================================================
169
    //==============================================================================================================
170
171
    /**
172
     * @param string $emailId
173
     *
174
     * @return Mock|Commerce_EmailModel
175
     */
176
    private function getMockEmail($emailId)
177
    {
178
        $mockEmail = $this->getMockBuilder(Commerce_EmailModel::class)
179
            ->disableOriginalConstructor()
180
            ->getMock();
181
182
        $mockEmail->expects($this->any())
183
            ->method('__get')
184
            ->willReturnMap([
185
                ['id', $emailId],
186
                ['name', 'emailName'.$emailId],
187
            ]);
188
189
        $mockEmail->expects($this->any())
190
            ->method('getAllErrors')
191
            ->willReturn([
192
                'ohnoes' => 'horrible error',
193
            ]);
194
195
        return $mockEmail;
196
    }
197
198
    /**
199
     * @return Mock|Commerce_EmailsService
200
     */
201
    private function setMockEmailsService()
202
    {
203
        $mockEmailsService = $this->getMockBuilder(Commerce_EmailsService::class)
204
            ->disableOriginalConstructor()
205
            ->setMethods(['getAllEmails', 'saveEmail', 'deleteEmailById'])
206
            ->getMock();
207
208
        $mockEmailsService->expects($this->any())
209
            ->method('getAllEmails')
210
            ->willReturn([]);
211
212
        $this->setComponent(Craft::app(), 'commerce_emails', $mockEmailsService);
213
214
        return $mockEmailsService;
215
    }
216
217
    /**
218
     * @return Mock|DbConnection
219
     */
220
    private function setMockDbConnection()
221
    {
222
        $mockDbConnection = $this->getMockBuilder(DbConnection::class)
223
            ->disableOriginalConstructor()
224
            ->setMethods(['createCommand'])
225
            ->getMock();
226
        $mockDbConnection->autoConnect = false; // Do not auto connect
227
228
        $mockDbCommand = $this->getMockDbCommand();
229
        $mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand);
230
231
        Craft::app()->setComponent('db', $mockDbConnection);
232
233
        return $mockDbConnection;
234
    }
235
236
    /**
237
     * @return Mock|DbCommand
238
     */
239
    private function getMockDbCommand()
240
    {
241
        $mockDbCommand = $this->getMockBuilder(DbCommand::class)
242
            ->disableOriginalConstructor()
243
            ->setMethods(['insertOrUpdate'])
244
            ->getMock();
245
246
        return $mockDbCommand;
247
    }
248
}
249