Passed
Pull Request — master (#6)
by Alexander
04:02
created

FlexibleTimestampBehaviorTest::mockApplication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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
     * @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;
0 ignored issues
show
Bug Compatibility introduced by
The expression \Yii::$app; of type yii\console\Application|yii\web\Application adds the type yii\console\Application to the return on line 44 which is incompatible with the return type documented by Horat1us\Yii\Tests\Behav...orTest::mockApplication of type yii\web\Application.
Loading history...
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
}