Test Failed
Push — master ( c65274...a98fd4 )
by Alexander
18:33
created

TokenQueryTest::testNotExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Wearesho\Yii\Tests\Queries;
6
7
use Carbon\Carbon;
8
use Carbon\CarbonInterval;
9
10
use Horat1us\Yii\Validation;
11
use Wearesho\Yii\Models\Token;
12
use Wearesho\Yii\Queries\TokenQuery;
13
14
use Wearesho\Yii\Tests\AbstractTestCase;
15
use Wearesho\Yii\Tests\Mocks\TokenRecordMock;
16
17
class TokenQueryTest extends AbstractTestCase
18
{
19
    protected TokenQuery $query;
20
21
    protected function setUp(): void
22
    {
23
        parent::setUp();
24
        $this->query = new TokenQuery(TokenRecordMock::class);
25
26
        Carbon::setTestNow(Carbon::create(2014, 1, 1, 1));
27
        $token = new TokenRecordMock([
28
            'id' => 1,
29
            'recipient' => "380500000001",
30
            'token' => "000001",
31
        ]);
32
        Validation\Exception::saveOrThrow($token);
33
        Carbon::setTestNow();
34
    }
35
36
    public function testModelClass(): void
37
    {
38
        $this->assertEquals(
39
            TokenRecordMock::class,
40
            $this->query->modelClass,
41
            "Query should be equal to passed to constructor class"
42
        );
43
    }
44
45
    public function testWhereRecipient(): void
46
    {
47
        $tokenInstance = $this->query
48
            ->whereRecipient($firstUserRecipient = '380500000001')
49
            ->andWhere(['=', 'id', $fieldUserId = 1])
50
            ->one();
51
52
        $this->assertInstanceOf(
53
            Token::class,
54
            $tokenInstance,
55
            "Query should use whereRecipient argument to find"
56
        );
57
58
        $tokenInstance = $this->query
59
            ->whereRecipient($invalidRecipient = '380500000000')
60
            ->andWhere(['=', 'id', 1])
61
            ->one();
62
63
        $this->assertNull(
64
            $tokenInstance
65
        );
66
    }
67
68
    public function testNotExpired(): void
69
    {
70
        // Setting now to hour bigger than first token created_at
71
        Carbon::setTestNow(Carbon::create(2014, 1, 1, 2));
72
73
        $tokenInstance = $this->query
74
            ->notExpired(CarbonInterval::hour(1))
75
            ->andWhere(['=', 'id', 1])
76
            ->one();
77
        $this->assertInstanceOf(
78
            Token::class,
79
            $tokenInstance
80
        );
81
82
        $tokenInstance = $this->query
83
            ->notExpired(CarbonInterval::minutes(30))
84
            ->andWhere(['=', 'id', 1])
85
            ->one();
86
        $this->assertNull(
87
            $tokenInstance
88
        );
89
90
        Carbon::setTestNow();
91
    }
92
}
93