Passed
Push — master ( 19a915...d9efc8 )
by Gabriel
14:56
created

SubscriptionTraitTest::data_getChargeMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
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\ChargeMethods\{Gateway, Internal};
12
use ByTIC\Payments\Tests\AbstractTest;
13
use Nip\Database\Query\Insert;
14
use Nip\Records\Locator\ModelLocator;
15
16
/**
17
 * Class SubscriptionTraitTest
18
 * @package ByTIC\Payments\Tests\Models\Subscriptions
19
 */
20
class SubscriptionTraitTest extends AbstractTest
21
{
22
23
    /**
24
     * @dataProvider data_getChargeMethod
25
     */
26
    public function test_getChargeMethod($value, $class)
27
    {
28
        $repository = Subscriptions::instance();
29
30
        $item = new Subscription();
31
        $item->fill(['charge_method' => $value]);
32
        $item->setManager($repository);
33
34
        self::assertInstanceOf($class, $item->getChargeMethod());
35
    }
36
37
    public function data_getChargeMethod(): array
38
    {
39
        return [
40
            ['', Internal::class],
41
            [Internal::NAME, Internal::class],
42
            [Gateway::NAME, Gateway::class],
43
        ];
44
    }
45
46
    public function test_populateFromToken()
47
    {
48
        ModelLocator::set(Tokens::class, Tokens::instance());
49
50
        $repository = \Mockery::mock(Subscriptions::class)->shouldAllowMockingProtectedMethods()->makePartial();
51
        $repository->shouldReceive('initRelationsTransactions');
52
        $repository->shouldReceive('initRelationsLastTransaction');
53
        $repository->setPrimaryKey('id');
54
55
        $item = new Subscription();
56
        $item->setManager($repository);
57
58
        $token = new Token();
59
        $token->id = 7;
60
        $item->populateFromToken($token);
61
62
        self::assertSame(7, $item->getPropertyRaw('id_token'));
63
        self::assertSame($token, $item->getToken());
64
    }
65
66
    public function test_cast_metadata()
67
    {
68
        $item = new Subscription();
69
70
        $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...
71
        self::assertInstanceOf(\ByTIC\DataObjects\Casts\Metadata\Metadata::class, $metadata);
72
73
        $item->addMedata('test', 99);
74
        self::assertSame(99, $item->metadata['test']);
75
76
        self::assertSame('{"test":99}', $item->getPropertyRaw('metadata'));
77
    }
78
79
    public function test_cast_metadata_empty()
80
    {
81
        $repository = \Mockery::mock(Transactions::class)->shouldAllowMockingProtectedMethods()->makePartial();
82
        $repository->shouldReceive('insertQuery')->once()->andReturn(new Insert());
83
        $repository->shouldReceive('performInsert')->once();
84
        $repository->bootTransactionsTrait();
85
86
        $item = new Transaction();
87
        $item->setManager($repository);
88
        $item->insert();
89
90
        self::assertSame('{}', $item->getPropertyRaw('metadata'));
91
    }
92
}