Passed
Push — master ( 012822...f43c0a )
by Alexander
56s
created

FlexibleTimestampBehaviorTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
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
     * Create new application instance if it doesn't exist.
19
     * @return void
20
     */
21
    public function setUp()
22
    {
23
        if (isset(\Yii::$app)) {
24
            return;
25
        }
26
        \Yii::$container = new Container();
27
        \Yii::createObject([
28
            'class' => Application::class,
29
            'id' => mt_rand(),
30
            'basePath' => __DIR__,
31
        ]);
32
    }
33
34
    /**
35
     * Clear created application.
36
     * @return void
37
     */
38
    public function tearDown()
39
    {
40
        \Yii::$app = null;
41
        \Yii::$container = new Container();
42
    }
43
44
    /**
45
     * @return void
46
     */
47
    public function testCorrectMapping()
48
    {
49
        $date1 = Carbon::now()->subDay();
50
        $date2 = Carbon::now()->subYear();
51
52
        $model = new TimestampTestMock([
53
            'timestamp' => $date1->timestamp,
54
            'date1' => $date2->format('d.m.Y'),
55
            'date2' => $date2->format('Y-m-d'),
56
        ]);
57
        $model->validate();
58
59
        $this->assertEquals($date1->format($model->format), $model->timestamp);
60
        $this->assertEquals($date2->format($model->format), $model->date1);
61
        $this->assertEquals($date2->format($model->format), $model->date2);
62
    }
63
}