6
6
namespace Magento \Cron \Test \Unit \Model ;
7
7
8
8
use Magento \Cron \Model \Schedule ;
9
+ use Magento \Framework \Intl \DateTimeFactory ;
10
+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
11
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
9
12
10
13
/**
11
14
* Class \Magento\Cron\Test\Unit\Model\ObserverTest
@@ -18,11 +21,27 @@ class ScheduleTest extends \PHPUnit\Framework\TestCase
18
21
*/
19
22
protected $ helper ;
20
23
24
+ /**
25
+ * @var \Magento\Cron\Model\ResourceModel\Schedule
26
+ */
21
27
protected $ resourceJobMock ;
22
28
29
+ /**
30
+ * @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
31
+ */
32
+ private $ timezoneConverter ;
33
+
34
+ /**
35
+ * @var DateTimeFactory|\PHPUnit_Framework_MockObject_MockObject
36
+ */
37
+ private $ dateTimeFactory ;
38
+
39
+ /**
40
+ * @inheritdoc
41
+ */
23
42
protected function setUp ()
24
43
{
25
- $ this ->helper = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
44
+ $ this ->helper = new ObjectManager ($ this );
26
45
27
46
$ this ->resourceJobMock = $ this ->getMockBuilder (\Magento \Cron \Model \ResourceModel \Schedule::class)
28
47
->disableOriginalConstructor ()
@@ -32,18 +51,30 @@ protected function setUp()
32
51
$ this ->resourceJobMock ->expects ($ this ->any ())
33
52
->method ('getIdFieldName ' )
34
53
->will ($ this ->returnValue ('id ' ));
54
+
55
+ $ this ->timezoneConverter = $ this ->getMockBuilder (TimezoneInterface::class)
56
+ ->setMethods (['date ' ])
57
+ ->getMockForAbstractClass ();
58
+
59
+ $ this ->dateTimeFactory = $ this ->getMockBuilder (DateTimeFactory::class)
60
+ ->setMethods (['create ' ])
61
+ ->getMock ();
35
62
}
36
63
37
64
/**
65
+ * Test for SetCronExpr
66
+ *
38
67
* @param string $cronExpression
39
68
* @param array $expected
69
+ *
70
+ * @return void
40
71
* @dataProvider setCronExprDataProvider
41
72
*/
42
- public function testSetCronExpr ($ cronExpression , $ expected )
73
+ public function testSetCronExpr ($ cronExpression , $ expected ): void
43
74
{
44
75
// 1. Create mocks
45
- /** @var \Magento\Cron\Model\ Schedule $model */
46
- $ model = $ this ->helper ->getObject (\ Magento \ Cron \ Model \ Schedule::class);
76
+ /** @var Schedule $model */
77
+ $ model = $ this ->helper ->getObject (Schedule::class);
47
78
48
79
// 2. Run tested method
49
80
$ model ->setCronExpr ($ cronExpression );
@@ -61,7 +92,7 @@ public function testSetCronExpr($cronExpression, $expected)
61
92
*
62
93
* @return array
63
94
*/
64
- public function setCronExprDataProvider ()
95
+ public function setCronExprDataProvider (): array
65
96
{
66
97
return [
67
98
['1 2 3 4 5 ' , [1 , 2 , 3 , 4 , 5 ]],
@@ -121,27 +152,33 @@ public function setCronExprDataProvider()
121
152
}
122
153
123
154
/**
155
+ * Test for SetCronExprException
156
+ *
124
157
* @param string $cronExpression
158
+ *
159
+ * @return void
125
160
* @expectedException \Magento\Framework\Exception\CronException
126
161
* @dataProvider setCronExprExceptionDataProvider
127
162
*/
128
- public function testSetCronExprException ($ cronExpression )
163
+ public function testSetCronExprException ($ cronExpression ): void
129
164
{
130
165
// 1. Create mocks
131
- /** @var \Magento\Cron\Model\ Schedule $model */
132
- $ model = $ this ->helper ->getObject (\ Magento \ Cron \ Model \ Schedule::class);
166
+ /** @var Schedule $model */
167
+ $ model = $ this ->helper ->getObject (Schedule::class);
133
168
134
169
// 2. Run tested method
135
170
$ model ->setCronExpr ($ cronExpression );
136
171
}
137
172
138
173
/**
174
+ * Data provider
175
+ *
139
176
* Here is a list of allowed characters and values for Cron expression
140
177
* http://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm
141
178
*
142
179
* @return array
143
180
*/
144
- public function setCronExprExceptionDataProvider ()
181
+ public function setCronExprExceptionDataProvider (): array
145
182
{
146
183
return [
147
184
['' ],
@@ -153,17 +190,31 @@ public function setCronExprExceptionDataProvider()
153
190
}
154
191
155
192
/**
193
+ * Test for trySchedule
194
+ *
156
195
* @param int $scheduledAt
157
196
* @param array $cronExprArr
158
197
* @param $expected
198
+ *
199
+ * @return void
159
200
* @dataProvider tryScheduleDataProvider
160
201
*/
161
- public function testTrySchedule ($ scheduledAt , $ cronExprArr , $ expected )
202
+ public function testTrySchedule ($ scheduledAt , $ cronExprArr , $ expected ): void
162
203
{
163
204
// 1. Create mocks
205
+ $ this ->timezoneConverter ->method ('getConfigTimezone ' )
206
+ ->willReturn ('UTC ' );
207
+
208
+ $ this ->dateTimeFactory ->method ('create ' )
209
+ ->willReturn (new \DateTime (null , new \DateTimeZone ('UTC ' )));
210
+
164
211
/** @var \Magento\Cron\Model\Schedule $model */
165
212
$ model = $ this ->helper ->getObject (
166
- \Magento \Cron \Model \Schedule::class
213
+ \Magento \Cron \Model \Schedule::class,
214
+ [
215
+ 'timezoneConverter ' => $ this ->timezoneConverter ,
216
+ 'dateTimeFactory ' => $ this ->dateTimeFactory
217
+ ]
167
218
);
168
219
169
220
// 2. Set fixtures
@@ -177,22 +228,29 @@ public function testTrySchedule($scheduledAt, $cronExprArr, $expected)
177
228
$ this ->assertEquals ($ expected , $ result );
178
229
}
179
230
180
- public function testTryScheduleWithConversionToAdminStoreTime ()
231
+ /**
232
+ * Test for tryScheduleWithConversionToAdminStoreTime
233
+ *
234
+ * @return void
235
+ */
236
+ public function testTryScheduleWithConversionToAdminStoreTime (): void
181
237
{
182
238
$ scheduledAt = '2011-12-13 14:15:16 ' ;
183
239
$ cronExprArr = ['* ' , '* ' , '* ' , '* ' , '* ' ];
184
240
185
- // 1. Create mocks
186
- $ timezoneConverter = $ this ->createMock (\Magento \Framework \Stdlib \DateTime \TimezoneInterface::class);
187
- $ timezoneConverter ->expects ($ this ->once ())
188
- ->method ('date ' )
189
- ->with ($ scheduledAt )
190
- ->willReturn (new \DateTime ($ scheduledAt ));
241
+ $ this ->timezoneConverter ->method ('getConfigTimezone ' )
242
+ ->willReturn ('UTC ' );
243
+
244
+ $ this ->dateTimeFactory ->method ('create ' )
245
+ ->willReturn (new \DateTime (null , new \DateTimeZone ('UTC ' )));
191
246
192
247
/** @var \Magento\Cron\Model\Schedule $model */
193
248
$ model = $ this ->helper ->getObject (
194
249
\Magento \Cron \Model \Schedule::class,
195
- ['timezoneConverter ' => $ timezoneConverter ]
250
+ [
251
+ 'timezoneConverter ' => $ this ->timezoneConverter ,
252
+ 'dateTimeFactory ' => $ this ->dateTimeFactory
253
+ ]
196
254
);
197
255
198
256
// 2. Set fixtures
@@ -207,9 +265,11 @@ public function testTryScheduleWithConversionToAdminStoreTime()
207
265
}
208
266
209
267
/**
268
+ * Data provider
269
+ *
210
270
* @return array
211
271
*/
212
- public function tryScheduleDataProvider ()
272
+ public function tryScheduleDataProvider (): array
213
273
{
214
274
$ date = '2011-12-13 14:15:16 ' ;
215
275
return [
@@ -229,12 +289,16 @@ public function tryScheduleDataProvider()
229
289
}
230
290
231
291
/**
292
+ * Test for matchCronExpression
293
+ *
232
294
* @param string $cronExpressionPart
233
295
* @param int $dateTimePart
234
296
* @param bool $expectedResult
297
+ *
298
+ * @return void
235
299
* @dataProvider matchCronExpressionDataProvider
236
300
*/
237
- public function testMatchCronExpression ($ cronExpressionPart , $ dateTimePart , $ expectedResult )
301
+ public function testMatchCronExpression ($ cronExpressionPart , $ dateTimePart , $ expectedResult ): void
238
302
{
239
303
// 1. Create mocks
240
304
/** @var \Magento\Cron\Model\Schedule $model */
@@ -248,9 +312,11 @@ public function testMatchCronExpression($cronExpressionPart, $dateTimePart, $exp
248
312
}
249
313
250
314
/**
315
+ * Data provider
316
+ *
251
317
* @return array
252
318
*/
253
- public function matchCronExpressionDataProvider ()
319
+ public function matchCronExpressionDataProvider (): array
254
320
{
255
321
return [
256
322
['* ' , 0 , true ],
@@ -287,11 +353,15 @@ public function matchCronExpressionDataProvider()
287
353
}
288
354
289
355
/**
356
+ * Test for matchCronExpressionException
357
+ *
290
358
* @param string $cronExpressionPart
359
+ *
360
+ * @return void
291
361
* @expectedException \Magento\Framework\Exception\CronException
292
362
* @dataProvider matchCronExpressionExceptionDataProvider
293
363
*/
294
- public function testMatchCronExpressionException ($ cronExpressionPart )
364
+ public function testMatchCronExpressionException ($ cronExpressionPart ): void
295
365
{
296
366
$ dateTimePart = 10 ;
297
367
@@ -304,9 +374,11 @@ public function testMatchCronExpressionException($cronExpressionPart)
304
374
}
305
375
306
376
/**
377
+ * Data provider
378
+ *
307
379
* @return array
308
380
*/
309
- public function matchCronExpressionExceptionDataProvider ()
381
+ public function matchCronExpressionExceptionDataProvider (): array
310
382
{
311
383
return [
312
384
['1/2/3 ' ], //Invalid cron expression, expecting 'match/modulus': 1/2/3
@@ -317,11 +389,15 @@ public function matchCronExpressionExceptionDataProvider()
317
389
}
318
390
319
391
/**
392
+ * Test for GetNumeric
393
+ *
320
394
* @param mixed $param
321
395
* @param int $expectedResult
396
+ *
397
+ * @return void
322
398
* @dataProvider getNumericDataProvider
323
399
*/
324
- public function testGetNumeric ($ param , $ expectedResult )
400
+ public function testGetNumeric ($ param , $ expectedResult ): void
325
401
{
326
402
// 1. Create mocks
327
403
/** @var \Magento\Cron\Model\Schedule $model */
@@ -335,9 +411,11 @@ public function testGetNumeric($param, $expectedResult)
335
411
}
336
412
337
413
/**
414
+ * Data provider
415
+ *
338
416
* @return array
339
417
*/
340
- public function getNumericDataProvider ()
418
+ public function getNumericDataProvider (): array
341
419
{
342
420
return [
343
421
[null , false ],
@@ -362,7 +440,12 @@ public function getNumericDataProvider()
362
440
];
363
441
}
364
442
365
- public function testTryLockJobSuccess ()
443
+ /**
444
+ * Test for tryLockJobSuccess
445
+ *
446
+ * @return void
447
+ */
448
+ public function testTryLockJobSuccess (): void
366
449
{
367
450
$ scheduleId = 1 ;
368
451
@@ -386,7 +469,12 @@ public function testTryLockJobSuccess()
386
469
$ this ->assertEquals (Schedule::STATUS_RUNNING , $ model ->getStatus ());
387
470
}
388
471
389
- public function testTryLockJobFailure ()
472
+ /**
473
+ * Test for tryLockJobFailure
474
+ *
475
+ * @return void
476
+ */
477
+ public function testTryLockJobFailure (): void
390
478
{
391
479
$ scheduleId = 1 ;
392
480
0 commit comments