Passed
Push — master ( 5391c3...c65274 )
by Alexander
02:26
created

TokenValidatorTest::testCustomLimitReached()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wearesho\Yii\Tests\Validators;
4
5
use Carbon\CarbonInterval;
6
7
use Wearesho\Delivery;
8
9
use Horat1us\Yii\Exceptions\ModelException;
10
use Wearesho\Yii\Interfaces\TokenRepositoryInterface;
11
use Wearesho\Yii\Repositories\TokenRepository;
12
13
use Wearesho\Yii\Tests\AbstractTestCase;
14
use Wearesho\Yii\Tests\Mocks\TokenCheckModelMock;
15
use Wearesho\Yii\Tests\Mocks\TokenGeneratorMock;
16
use Wearesho\Yii\Tests\Mocks\TokenRecordMock;
17
use Wearesho\Yii\Tests\Mocks\TokenRepositoryConfigMock;
18
use Wearesho\Yii\Validators\TokenValidator;
19
20
/**
21
 * Class TokenValidatorTest
22
 * @package Wearesho\Yii\Tests\Validators
23
 *
24
 * @internal
25
 */
26
class TokenValidatorTest extends AbstractTestCase
27
{
28
    /** @var  TokenRepository */
29
    protected $repository;
30
31
    /** @var  TokenCheckModelMock */
32
    protected $model;
33
34
    protected function setUp()
35
    {
36
        parent::setUp();
37
        $this->repository = new TokenRepository(
38
            new TokenRecordMock(),
39
            $config = new TokenRepositoryConfigMock,
40
            new TokenGeneratorMock,
41
            new Delivery\ServiceMock()
42
        );
43
        $config->setExpirePeriod(CarbonInterval::years(5));
44
45
        \Yii::$container->set(
46
            TokenRepositoryInterface::class,
47
            function () {
48
                return $this->repository;
49
            }
50
        );
51
52
        $this->model = new TokenCheckModelMock;
53
54
        $token = new TokenRecordMock([
55
            'id' => 1,
56
            'recipient' => "380500000001",
57
            'token' => "000001",
58
        ]);
59
        ModelException::saveOrThrow($token);
60
    }
61
62
    public function testInvalidRecipient()
63
    {
64
        $this->model->recipient = "380500000000";
65
        $this->model->token = "000001";
66
        $this->model->validate();
67
68
        $this->assertTrue(
69
            $this->model->hasErrors('token')
70
        );
71
        $this->assertFalse(
72
            $this->model->hasErrors('recipient')
73
        );
74
    }
75
76
    public function testInvalidToken()
77
    {
78
        $this->model->recipient = "380500000001";
79
        $this->model->token = "111111";
80
        $this->model->validate();
81
82
        $this->assertTrue(
83
            $this->model->hasErrors('token')
84
        );
85
        $this->assertFalse(
86
            $this->model->hasErrors('recipient')
87
        );
88
        $this->assertEquals('Token is invalid.', $this->model->errors['token'][0]);
89
    }
90
91
    public function testValid()
92
    {
93
        $this->model->recipient = "380500000001";
94
        $this->model->token = "000001";
95
        $this->model->validate();
96
97
        $this->assertFalse($this->model->hasErrors());
98
    }
99
100
    public function testCustomMessage(): void
101
    {
102
        $this->model->errorMessage = 'invalid token';
103
        $this->model->recipient = "380500000001";
104
        $this->model->token = "111111";
105
        $this->model->validate();
106
107
        $this->assertEquals('invalid token', $this->model->errors['token'][0]);
108
    }
109
110
    public function testCustomLimitReached(): void
111
    {
112
        $validator = new TokenValidator($this->createMock(TokenRepository::class));
0 ignored issues
show
Documentation introduced by
$this->createMock(\Weare...TokenRepository::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wearesho\Yii\Inte...kenRepositoryInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
        $this->assertEquals('Limit of messages is reached', $validator->limitReachedMessage);
114
        $validator = new TokenValidator($this->createMock(TokenRepository::class), [
0 ignored issues
show
Documentation introduced by
$this->createMock(\Weare...TokenRepository::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wearesho\Yii\Inte...kenRepositoryInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
            'limitReachedMessage' => 'custom message',
116
        ]);
117
118
        $this->assertEquals('custom message', $validator->limitReachedMessage);
119
    }
120
}
121