ModelTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 27
rs 9.552
c 0
b 0
f 0
1
<?php
2
/**
3
 * File: ModelTest.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\Otomoto\Middleware\Test\Unit\App\Model;
10
11
use MSlwk\Otomoto\App\Manufacturer\Data\ManufacturerDTO;
12
use MSlwk\Otomoto\App\Model\Data\ModelDTOArray;
13
use MSlwk\Otomoto\App\Model\ModelProvider;
14
use MSlwk\Otomoto\Middleware\App\Model\Model;
15
use MSlwk\Otomoto\Middleware\App\Model\ModelSerializer;
16
use MSlwk\Otomoto\Middleware\Cache\Adapter\CacheAdapter;
17
use PHPUnit\Framework\MockObject\MockObject;
18
use PHPUnit\Framework\TestCase;
19
use Stash\Interfaces\ItemInterface;
20
21
/**
22
 * Class ModelTest
23
 * @package MSlwk\Otomoto\Middleware\Test\Unit\App\Model
24
 */
25
class ModelTest extends TestCase
26
{
27
    /**
28
     * @var MockObject|CacheAdapter
29
     */
30
    private $cacheAdapter;
31
32
    /**
33
     * @var MockObject|ModelProvider
34
     */
35
    private $modelProvider;
36
37
    /**
38
     * @var MockObject|ModelSerializer
39
     */
40
    private $modelSerializer;
41
42
    /**
43
     * @var Model
44
     */
45
    private $modelMiddleware;
46
47
    /**
48
     * @var MockObject|ItemInterface
49
     */
50
    private $cacheItem;
51
52
    /**
53
     * @var ManufacturerDTO
54
     */
55
    private $manufacturer;
56
57
    /**
58
     * @return void
59
     */
60
    protected function setUp()
61
    {
62
        $this->manufacturer = new ManufacturerDTO('manufacturer');
63
64
        $this->cacheAdapter = $this->getMockBuilder(CacheAdapter::class)
65
            ->disableOriginalConstructor()
66
            ->getMock();
67
        $this->cacheItem = $this->getMockBuilder(ItemInterface::class)
68
            ->getMock();
69
        $this->cacheAdapter->expects($this->atLeastOnce())
70
            ->method('retrieve')
71
            ->with('models_manufacturer')
72
            ->will($this->returnValue($this->cacheItem));
73
        $this->modelProvider = $this->getMockBuilder(ModelProvider::class)
74
            ->disableOriginalConstructor()
75
            ->getMock();
76
        $this->modelSerializer = $this->getMockBuilder(ModelSerializer::class)
77
            ->disableOriginalConstructor()
78
            ->getMock();
79
        $this->modelSerializer->expects($this->once())
80
            ->method('unserialize')
81
            ->will($this->returnValue(new ModelDTOArray()));
82
83
        $this->modelMiddleware = new Model(
84
            $this->cacheAdapter,
85
            $this->modelProvider,
86
            $this->modelSerializer
87
        );
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function testMiddlewareReturnsModelsFromCache()
94
    {
95
        $this->cacheItem->expects($this->exactly(2))
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Stash\Interfaces\ItemInterface. ( Ignorable by Annotation )

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

95
        $this->cacheItem->/** @scrutinizer ignore-call */ 
96
                          expects($this->exactly(2))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
            ->method('get')
97
            ->will($this->returnValue('a:1:{i:0;a:1:{s:4:"name";s:6:"model1";}}'));
98
99
        $result = $this->modelMiddleware->getModels($this->manufacturer);
100
        $expectedType = ModelDTOArray::class;
101
102
        $this->assertInstanceOf($expectedType, $result);
103
    }
104
105
    /**
106
     * @test
107
     */
108
    public function testMiddlewareReturnsModelsFromRequest()
109
    {
110
        $this->cacheItem->expects($this->exactly(2))
111
            ->method('get')
112
            ->willReturnOnConsecutiveCalls(
113
                null,
114
                'a:1:{i:0;a:1:{s:4:"name";s:6:"model1";}}'
115
            );
116
117
        $result = $this->modelMiddleware->getModels($this->manufacturer);
118
        $expectedType = ModelDTOArray::class;
119
120
        $this->assertInstanceOf($expectedType, $result);
121
    }
122
}
123