Passed
Push — master ( 96d33b...43186b )
by Gabriel
13:27
created

SubscriptionTraitTest::test_getStatuses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace ByTIC\Payments\Tests\Models\Subscriptions;
4
5
use ByTIC\Payments\Models\Subscriptions\Subscription;
6
use ByTIC\Payments\Models\Subscriptions\Subscriptions;
7
use ByTIC\Payments\Models\Tokens\Token;
8
use ByTIC\Payments\Models\Tokens\Tokens;
9
use ByTIC\Payments\Models\Transactions\Transaction;
10
use ByTIC\Payments\Models\Transactions\Transactions;
11
use ByTIC\Payments\Subscriptions\Statuses\Active;
12
use ByTIC\Payments\Subscriptions\Statuses\NotStarted;
13
use ByTIC\Payments\Tests\AbstractTest;
14
use Nip\Database\Query\Insert;
15
use Nip\Records\AbstractModels\Record;
16
use Nip\Records\Locator\ModelLocator;
17
18
/**
19
 * Class SubscriptionTraitTest
20
 * @package ByTIC\Payments\Tests\Models\Subscriptions
21
 */
22
class SubscriptionTraitTest extends AbstractTest
23
{
24
25
    public function test_getStatuses()
26
    {
27
        $statuses = Subscriptions::instance()->getStatuses();
28
29
        self::assertCount(4, $statuses);
30
31
        self::assertInstanceOf(Active::class, $statuses[Active::NAME]);
32
        self::assertInstanceOf(NotStarted::class, $statuses[NotStarted::NAME]);
33
    }
34
35
    public function test_populateFromToken()
36
    {
37
        ModelLocator::set(Tokens::class, Tokens::instance());
38
39
        $repository = \Mockery::mock(Subscriptions::class)->shouldAllowMockingProtectedMethods()->makePartial();
40
        $repository->shouldReceive('initRelationsTransactions');
41
        $repository->shouldReceive('initRelationsLastTransaction');
42
        $repository->setPrimaryKey('id');
43
44
        $item = new Subscription();
45
        $item->setManager($repository);
46
47
        $token = new Token();
48
        $token->id = 7;
49
        $item->populateFromToken($token);
50
51
        self::assertSame(7, $item->getPropertyRaw('id_token'));
52
        self::assertSame($token, $item->getToken());
0 ignored issues
show
Bug introduced by
The method getToken() does not exist on ByTIC\Payments\Models\Subscriptions\Subscription. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        self::assertSame($token, $item->/** @scrutinizer ignore-call */ getToken());
Loading history...
53
    }
54
55
    public function test_cast_metadata()
56
    {
57
        $item = new Subscription();
58
59
        $metadata = $item->metadata;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata does not exist on ByTIC\Payments\Models\Subscriptions\Subscription. Since you implemented __get, consider adding a @property annotation.
Loading history...
60
        self::assertInstanceOf(\ByTIC\DataObjects\Casts\Metadata\Metadata::class, $metadata);
61
62
        $item->addMedata('test', 99);
63
        self::assertSame(99, $item->metadata['test']);
64
65
        self::assertSame('{"test":99}', $item->getPropertyRaw('metadata'));
66
    }
67
68
    public function test_cast_metadata_empty()
69
    {
70
        $repository = \Mockery::mock(Transactions::class)->shouldAllowMockingProtectedMethods()->makePartial();
71
        $repository->shouldReceive('insertQuery')->once()->andReturn(new Insert());
72
        $repository->shouldReceive('performInsert')->once();
73
        $repository->bootTransactionsTrait();
74
75
        $item = new Transaction();
76
        $item->setManager($repository);
77
        $item->insert();
78
79
        self::assertSame('{}', $item->getPropertyRaw('metadata'));
80
    }
81
}