Passed
Push — main ( 5658fd...05477b )
by Dylan
03:10
created

ModelTest::testCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Lifeboat\Tests\Models;
4
5
use Lifeboat\Factory\ClassMap;
6
use Lifeboat\Models\Collection;
7
use Lifeboat\Models\Model;
8
use Lifeboat\Services\ApiService;
9
use Lifeboat\Tests\TestCase;
10
11
class ModelTest extends TestCase {
12
13
    /**
14
     * @test
15
     * @covers \Lifeboat\Models\Model::getService
16
     */
17
    public function testGetService()
18
    {
19
        /** @var Model $class */
20
        foreach (ClassMap::MODELS as $class) {
21
            $mock = new $class($this->getMockClient(), ['ID' => 0]);
22
            $this->assertInstanceOf(Model::class, $mock);
23
            $this->assertInstanceOf(ApiService::class, $mock->getService());
24
        }
25
    }
26
27
    /**
28
     * @test
29
     * @covers \Lifeboat\Models\Model::exists
30
     * @covers \Lifeboat\Models\Model::toArray
31
     * @covers \Lifeboat\Models\Model::getIterator
32
     * @covers \Lifeboat\Models\Model::__set
33
     * @covers \Lifeboat\Models\Model::__get
34
     * @covers \Lifeboat\Models\Model::offsetGet
35
     * @covers \Lifeboat\Models\Model::offsetSet
36
     * @covers \Lifeboat\Models\Model::offsetExists
37
     * @covers \Lifeboat\Models\Model::count
38
     * @covers \Lifeboat\Models\Model::keys
39
     * @covers \Lifeboat\Models\Model::values
40
     */
41
    public function test_model_base_functions()
42
    {
43
        /** @var Model $class */
44
        foreach (ClassMap::MODELS as $class) {
45
            /** @var Model $mock */
46
            $mock = new $class($this->getMockClient(), ['ID' => 0]);
47
            $this->assertFalse($mock->exists());
48
            $mock->test = 'x';
0 ignored issues
show
Bug Best Practice introduced by
The property test does not exist on Lifeboat\Models\Model. Since you implemented __set, consider adding a @property annotation.
Loading history...
49
            $this->assertEquals('x', $mock->test);
0 ignored issues
show
Bug Best Practice introduced by
The property test does not exist on Lifeboat\Models\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
50
            $mock->offsetSet('test2', 'y');
51
            $this->assertEquals(['ID' => 0, 'test' => 'x', 'test2' => 'y'], $mock->toArray());
52
            $this->assertEquals('x', $mock->offsetGet('test'));
53
            $this->assertTrue($mock->offsetExists('test'));
54
            $mock->offsetUnset('test2');
55
            $this->assertEquals(['ID' => 0, 'test' => 'x'], $mock->toArray());
56
            $this->assertEquals(2, $mock->count());
57
            $this->assertEquals(['ID', 'test'], $mock->keys());
58
            $this->assertEquals([0, 'x'], $mock->values());
59
60
            /** @var Model $exists */
61
            $exists = new $class($this->getMockClient(), ['ID' => 1]);
62
            $this->assertTrue($exists->exists());
63
            $exists->test = 'y';
64
            $this->assertEquals(['ID' => 1, 'test' => 'y'], $exists->toArray());
65
            $this->assertEquals('y', $exists->offsetGet('test'));
66
        }
67
    }
68
69
    /**
70
     * @test
71
     * @covers \Lifeboat\Models\Collection::__construct
72
     */
73
    public function testCollection()
74
    {
75
        $data = ['ID' => 1, 'Rules' => '{"1":1}'];
76
        $collection = new Collection($this->getMockClient(), $data);
77
        $this->assertEquals([1 => 1], $collection->Rules);
78
    }
79
}
80