ManufacturerTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 24 1
A testMiddlewareReturnsManufacturersFromRequest() 0 13 1
A testMiddlewareReturnsManufacturersFromCache() 0 10 1
1
<?php
2
/**
3
 * File: ManufacturerTest.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\Manufacturer;
10
11
use MSlwk\Otomoto\App\Manufacturer\Data\ManufacturerDTOArray;
12
use MSlwk\Otomoto\App\Manufacturer\ManufacturerProvider;
13
use MSlwk\Otomoto\Middleware\App\Manufacturer\Manufacturer;
14
use MSlwk\Otomoto\Middleware\App\Manufacturer\ManufacturerSerializer;
15
use MSlwk\Otomoto\Middleware\Cache\Adapter\CacheAdapter;
16
use PHPUnit\Framework\MockObject\MockObject;
17
use PHPUnit\Framework\TestCase;
18
use Stash\Interfaces\ItemInterface;
19
20
/**
21
 * Class ManufacturerTest
22
 * @package MSlwk\Otomoto\Middleware\Test\Unit\App\Manufacturer
23
 */
24
class ManufacturerTest extends TestCase
25
{
26
    /**
27
     * @var MockObject|CacheAdapter
28
     */
29
    private $cacheAdapter;
30
31
    /**
32
     * @var MockObject|ManufacturerProvider
33
     */
34
    private $manufacturerProvider;
35
36
    /**
37
     * @var MockObject|ManufacturerSerializer
38
     */
39
    private $manufacturerSerializer;
40
41
    /**
42
     * @var Manufacturer
43
     */
44
    private $manufacturerMiddleware;
45
46
    /**
47
     * @var MockObject|ItemInterface
48
     */
49
    private $cacheItem;
50
51
    /**
52
     * @return void
53
     */
54
    protected function setUp()
55
    {
56
        $this->cacheAdapter = $this->getMockBuilder(CacheAdapter::class)
57
            ->disableOriginalConstructor()
58
            ->getMock();
59
        $this->cacheItem = $this->getMockBuilder(ItemInterface::class)
60
            ->getMock();
61
        $this->cacheAdapter->expects($this->atLeastOnce())
62
            ->method('retrieve')
63
            ->will($this->returnValue($this->cacheItem));
64
        $this->manufacturerProvider = $this->getMockBuilder(ManufacturerProvider::class)
65
            ->disableOriginalConstructor()
66
            ->getMock();
67
        $this->manufacturerSerializer = $this->getMockBuilder(ManufacturerSerializer::class)
68
            ->disableOriginalConstructor()
69
            ->getMock();
70
        $this->manufacturerSerializer->expects($this->once())
71
            ->method('unserialize')
72
            ->will($this->returnValue(new ManufacturerDTOArray()));
73
74
        $this->manufacturerMiddleware = new Manufacturer(
75
            $this->cacheAdapter,
76
            $this->manufacturerProvider,
77
            $this->manufacturerSerializer
78
        );
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function testMiddlewareReturnsManufacturersFromCache()
85
    {
86
        $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

86
        $this->cacheItem->/** @scrutinizer ignore-call */ 
87
                          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...
87
            ->method('get')
88
            ->will($this->returnValue('a:1:{i:0;a:1:{s:4:"name";s:13:"manufacturer1";}}'));
89
90
        $result = $this->manufacturerMiddleware->getManufacturers();
91
        $expectedType = ManufacturerDTOArray::class;
92
93
        $this->assertInstanceOf($expectedType, $result);
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function testMiddlewareReturnsManufacturersFromRequest()
100
    {
101
        $this->cacheItem->expects($this->exactly(2))
102
            ->method('get')
103
            ->willReturnOnConsecutiveCalls(
104
                null,
105
                'a:1:{i:0;a:1:{s:4:"name";s:13:"manufacturer1";}}'
106
            );
107
108
        $result = $this->manufacturerMiddleware->getManufacturers();
109
        $expectedType = ManufacturerDTOArray::class;
110
111
        $this->assertInstanceOf($expectedType, $result);
112
    }
113
}
114