Passed
Pull Request — master (#4)
by Alexander
03:54
created

TokenTest::testDefaultTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Wearesho\Yii\Tests\Models;
4
5
use Carbon\Carbon;
6
7
use paulzi\jsonBehavior\JsonField;
8
9
use Wearesho\Yii\Models\Token;
10
use Wearesho\Yii\Queries\TokenQuery;
11
use Wearesho\Yii\Tests\AbstractTestCase;
12
13
use Wearesho\Yii\Tests\Mocks\TokenRecordMock;
14
use yii\db\ActiveRecord;
15
16
/**
17
 * Class TokenTest
18
 * @package Wearesho\Yii\Tests\Models
19
 *
20
 * @internal
21
 */
22
class TokenTest extends AbstractTestCase
23
{
24
    /** @var  Token */
25
    protected $model;
26
27
    protected function setUp()
28
    {
29
        parent::setUp();
30
        $this->model = new TokenRecordMock();
31
    }
32
33
    public function testDefaultTimestamp()
34
    {
35
        Carbon::setTestNow($now = Carbon::now());
36
        $this->model->trigger(ActiveRecord::EVENT_BEFORE_INSERT);
37
        $this->assertEquals(
38
            $this->model->created_at,
39
            $now->toDateTimeString(),
40
            "created_at should be filled from now on insert"
41
        );
42
        Carbon::setTestNow();
43
    }
44
45
    public function testDefaultJson()
46
    {
47
        $this->assertInstanceOf(
48
            JsonField::class,
49
            $this->model->data,
50
            "Data should be init as " . JsonField::class
51
        );
52
    }
53
54
    public function testRecipient()
55
    {
56
        $recipient = mt_rand();
57
        $this->model->setRecipient($recipient);
58
        $this->assertEquals(
59
            $recipient,
60
            $this->model->getRecipient()
61
        );
62
    }
63
64
    public function testToken()
65
    {
66
        $token = mt_rand();
67
        $this->model->setToken($token);
68
        $this->assertEquals(
69
            $token,
70
            $this->model->getToken()
71
        );
72
    }
73
74
    public function testData()
75
    {
76
        $data = [
77
            mt_rand() => mt_rand(),
78
        ];
79
        $this->model->setData($data);
80
        $this->assertEquals(
81
            $data,
82
            $this->model->getData()
83
        );
84
    }
85
86
    public function testDeliveryCount()
87
    {
88
        $this->assertEquals(
89
            0,
90
            $this->model->getDeliveryCount(),
91
            "Delivery count should be 0 by default"
92
        );
93
        $this->model->increaseDeliveryCount();
94
        $this->assertEquals(
95
            1,
96
            $this->model->getDeliveryCount()
97
        );
98
    }
99
100
    public function testVerifyCount()
101
    {
102
        $this->assertEquals(
103
            0,
104
            $this->model->getVerifyCount(),
105
            "Verify count should be 0 by default"
106
        );
107
        $this->model->increaseVerifyCount();
108
        $this->assertEquals(
109
            1,
110
            $this->model->getVerifyCount()
111
        );
112
    }
113
114
    public function testFind()
115
    {
116
        $this->assertInstanceOf(
117
            TokenQuery::class,
118
            $this->model->find()
119
        );
120
    }
121
122
    public function testValidation()
123
    {
124
        $this->assertFalse(
125
            $this->model->validate()
126
        );
127
        $this->assertTrue(
128
            $this->model->hasErrors('token')
129
        );
130
        $this->assertTrue(
131
            $this->model->hasErrors('recipient')
132
        );
133
134
        $this->model->setRecipient(mt_rand());
135
        $this->model->setToken(mt_rand());
136
        $this->assertTrue(
137
            $this->model->validate(),
138
            "Only token and recipient should by required attributes"
139
        );
140
    }
141
142
    public function testToStringConversion()
143
    {
144
        $token = 'test';
145
146
        $this->model->setToken($token);
147
148
        $this->assertEquals($token, $this->model->__toString());
149
    }
150
}
151