1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Horat1us\Yii\Tests\Behaviors; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Horat1us\Yii\Tests\Mocks\TimestampTestMock; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use yii\di\Container; |
9
|
|
|
use yii\web\Application; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class FlexibleTimestampBehaviorTest |
13
|
|
|
* @package Horat1us\Yii\Tests\Behaviors |
14
|
|
|
*/ |
15
|
|
|
class FlexibleTimestampBehaviorTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function testCorrectMapping() |
21
|
|
|
{ |
22
|
|
|
$this->mockApplication(); |
23
|
|
|
$date1 = Carbon::now()->subDay(); |
24
|
|
|
$date2 = Carbon::now()->subYear(); |
25
|
|
|
|
26
|
|
|
$model = new TimestampTestMock([ |
27
|
|
|
'timestamp' => $date1->timestamp, |
28
|
|
|
'date' => $date2->toDateString(), |
29
|
|
|
]); |
30
|
|
|
$model->validate(); |
31
|
|
|
|
32
|
|
|
$this->assertEquals($date1->toDateString(), $model->timestamp); |
33
|
|
|
$this->assertEquals($date2->toDateString(), $model->date); |
34
|
|
|
|
35
|
|
|
$this->destroyApplication(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return Application |
40
|
|
|
*/ |
41
|
|
|
protected function mockApplication() |
42
|
|
|
{ |
43
|
|
|
if (isset(\Yii::$app)) { |
44
|
|
|
return \Yii::$app; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
\Yii::$container = new Container(); |
47
|
|
|
return \Yii::createObject([ |
48
|
|
|
'class' => Application::class, |
49
|
|
|
'id' => mt_rand(), |
50
|
|
|
'basePath' => __DIR__, |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
*/ |
57
|
|
|
protected function destroyApplication() |
58
|
|
|
{ |
59
|
|
|
if (\Yii::$app) { |
60
|
|
|
if (\Yii::$app->has('session', true)) { |
61
|
|
|
\Yii::$app->session->close(); |
62
|
|
|
} |
63
|
|
|
if (\Yii::$app->has('db', true)) { |
64
|
|
|
\Yii::$app->db->close(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
\Yii::$app = null; |
68
|
|
|
\Yii::$container = new Container(); |
69
|
|
|
} |
70
|
|
|
} |