|
5 | 5 | */
|
6 | 6 | namespace Magento\CacheInvalidate\Test\Unit\Model;
|
7 | 7 |
|
| 8 | +use \Zend\Uri\UriFactory; |
| 9 | + |
8 | 10 | class PurgeCacheTest extends \PHPUnit_Framework_TestCase
|
9 | 11 | {
|
10 |
| - /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\PurgeCache */ |
| 12 | + /** @var \Magento\CacheInvalidate\Model\PurgeCache */ |
11 | 13 | protected $model;
|
12 | 14 |
|
13 |
| - /** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Uri\Uri */ |
14 |
| - protected $uriMock; |
15 |
| - |
16 | 15 | /** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Http\Client\Adapter\Socket */
|
17 | 16 | protected $socketAdapterMock;
|
18 | 17 |
|
19 | 18 | /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Cache\InvalidateLogger */
|
20 | 19 | protected $loggerMock;
|
21 | 20 |
|
22 |
| - /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\DeploymentConfig\Reader */ |
23 |
| - protected $configReaderMock; |
| 21 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Model\Cache\Server */ |
| 22 | + protected $cacheServer; |
24 | 23 |
|
25 |
| - /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\RequestInterface */ |
26 |
| - protected $requestMock; |
27 |
| - |
28 |
| - /** |
29 |
| - * Set up all mocks and data for test |
30 |
| - */ |
31 | 24 | public function setUp()
|
32 | 25 | {
|
33 |
| - $this->uriFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\UriFactory', [], [], '', false); |
34 |
| - $this->uriMock = $this->getMock('\Zend\Uri\Uri', [], [], '', false); |
35 |
| - $this->socketFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\SocketFactory', [], [], '', false); |
| 26 | + $socketFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\SocketFactory', [], [], '', false); |
36 | 27 | $this->socketAdapterMock = $this->getMock('\Zend\Http\Client\Adapter\Socket', [], [], '', false);
|
37 |
| - $this->configMock = $this->getMock('Magento\Framework\App\DeploymentConfig', [], [], '', false); |
38 |
| - $this->loggerMock = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false); |
39 |
| - $this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false); |
40 | 28 | $this->socketAdapterMock->expects($this->once())
|
41 | 29 | ->method('setOptions')
|
42 | 30 | ->with(['timeout' => 10]);
|
43 |
| - $this->uriFactoryMock->expects($this->once()) |
44 |
| - ->method('create') |
45 |
| - ->willReturn($this->uriMock); |
46 |
| - $this->socketFactoryMock->expects($this->once()) |
| 31 | + $socketFactoryMock->expects($this->once()) |
47 | 32 | ->method('create')
|
48 | 33 | ->willReturn($this->socketAdapterMock);
|
49 |
| - $this->model = new \Magento\CacheInvalidate\Model\PurgeCache( |
50 |
| - $this->uriFactoryMock, |
51 |
| - $this->socketFactoryMock, |
52 |
| - $this->loggerMock, |
53 |
| - $this->configMock, |
54 |
| - $this->requestMock |
| 34 | + |
| 35 | + $this->loggerMock = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false); |
| 36 | + $this->cacheServer = $this->getMock('Magento\PageCache\Model\Cache\Server', [], [], '', false); |
| 37 | + |
| 38 | + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 39 | + $this->model = $objectManager->getObject( |
| 40 | + 'Magento\CacheInvalidate\Model\PurgeCache', |
| 41 | + [ |
| 42 | + 'cacheServer' => $this->cacheServer, |
| 43 | + 'socketAdapterFactory' => $socketFactoryMock, |
| 44 | + 'logger' => $this->loggerMock, |
| 45 | + ] |
55 | 46 | );
|
56 | 47 | }
|
57 | 48 |
|
58 |
| - public function testSendPurgeRequestEmptyConfig() |
| 49 | + /** |
| 50 | + * @param string[] $hosts |
| 51 | + * @dataProvider sendPurgeRequestDataProvider |
| 52 | + */ |
| 53 | + public function testSendPurgeRequest($hosts) |
59 | 54 | {
|
60 |
| - $this->socketAdapterMock->expects($this->once()) |
61 |
| - ->method('write') |
62 |
| - ->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags'])); |
63 |
| - $this->socketAdapterMock->expects($this->once()) |
64 |
| - ->method('close'); |
65 |
| - $this->configMock->expects($this->once()) |
66 |
| - ->method('get') |
67 |
| - ->willReturn(''); |
68 |
| - $this->requestMock->expects($this->any()) |
69 |
| - ->method('getHttpHost') |
70 |
| - ->willReturn('127.0.0.1'); |
71 |
| - $this->uriMock->expects($this->once()) |
72 |
| - ->method('setScheme') |
73 |
| - ->with('http') |
74 |
| - ->willReturnSelf(); |
75 |
| - $this->uriMock->expects($this->once()) |
76 |
| - ->method('setHost') |
77 |
| - ->with('127.0.0.1') |
78 |
| - ->willReturnSelf(); |
79 |
| - $this->uriMock->expects($this->once()) |
80 |
| - ->method('setPort') |
81 |
| - ->with(\Magento\CacheInvalidate\Model\PurgeCache::DEFAULT_PORT); |
82 |
| - $this->model->sendPurgeRequest('tags'); |
83 |
| - } |
| 55 | + $uris = []; |
| 56 | + foreach ($hosts as $host) { |
| 57 | + $port = isset($host['port']) ? $host['port'] : \Magento\PageCache\Model\Cache\Server::DEFAULT_PORT; |
| 58 | + $uris[] = UriFactory::factory('')->setHost($host['host']) |
| 59 | + ->setPort($port) |
| 60 | + ->setScheme('http'); |
| 61 | + } |
| 62 | + $this->cacheServer->expects($this->once()) |
| 63 | + ->method('getUris') |
| 64 | + ->willReturn($uris); |
84 | 65 |
|
85 |
| - public function testSendPurgeRequestOneServer() |
86 |
| - { |
87 |
| - $this->socketAdapterMock->expects($this->once()) |
88 |
| - ->method('write') |
89 |
| - ->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags'])); |
90 |
| - $this->socketAdapterMock->expects($this->once()) |
| 66 | + $i = 1; |
| 67 | + foreach ($uris as $uri) { |
| 68 | + $this->socketAdapterMock->expects($this->at($i++)) |
| 69 | + ->method('connect') |
| 70 | + ->with($uri->getHost(), $uri->getPort()); |
| 71 | + $this->socketAdapterMock->expects($this->at($i++)) |
| 72 | + ->method('write') |
| 73 | + ->with('PURGE', $uri, '1.1', ['X-Magento-Tags-Pattern' => 'tags']); |
| 74 | + $i++; |
| 75 | + } |
| 76 | + $this->socketAdapterMock->expects($this->exactly(count($uris))) |
91 | 77 | ->method('close');
|
92 |
| - $this->configMock->expects($this->once()) |
93 |
| - ->method('get') |
94 |
| - ->willReturn([['host' => '127.0.0.2', 'port' => 1234]]); |
95 |
| - $this->uriMock->expects($this->once()) |
96 |
| - ->method('setScheme') |
97 |
| - ->with('http') |
98 |
| - ->willReturnSelf(); |
99 |
| - $this->uriMock->expects($this->once()) |
100 |
| - ->method('setHost') |
101 |
| - ->with('127.0.0.2') |
102 |
| - ->willReturnSelf(); |
103 |
| - $this->uriMock->expects($this->once()) |
104 |
| - ->method('setPort') |
105 |
| - ->with(1234); |
106 |
| - $this->model->sendPurgeRequest('tags'); |
| 78 | + |
| 79 | + $this->loggerMock->expects($this->once()) |
| 80 | + ->method('execute'); |
| 81 | + |
| 82 | + $this->assertTrue($this->model->sendPurgeRequest('tags')); |
107 | 83 | }
|
108 | 84 |
|
109 |
| - public function testSendPurgeRequestMultipleServers() |
| 85 | + public function sendPurgeRequestDataProvider() |
110 | 86 | {
|
111 |
| - $this->socketAdapterMock->expects($this->exactly(2)) |
112 |
| - ->method('write') |
113 |
| - ->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags'])); |
114 |
| - $this->socketAdapterMock->expects($this->exactly(2)) |
115 |
| - ->method('close'); |
116 |
| - $this->configMock->expects($this->once()) |
117 |
| - ->method('get') |
118 |
| - ->willReturn( |
| 87 | + return [ |
| 88 | + [ |
| 89 | + [['host' => '127.0.0.1', 'port' => 8080],] |
| 90 | + ], |
| 91 | + [ |
119 | 92 | [
|
120 | 93 | ['host' => '127.0.0.1', 'port' => 8080],
|
121 |
| - ['host' => '127.0.0.2', 'port' => 1234] |
| 94 | + ['host' => '127.0.0.2', 'port' => 1234], |
| 95 | + ['host' => 'host'] |
122 | 96 | ]
|
123 |
| - ); |
124 |
| - $this->uriMock->expects($this->at(0)) |
125 |
| - ->method('setScheme') |
126 |
| - ->with('http') |
127 |
| - ->willReturnSelf(); |
128 |
| - $this->uriMock->expects($this->at(1)) |
129 |
| - ->method('setHost') |
130 |
| - ->with('127.0.0.1') |
131 |
| - ->willReturnSelf(); |
132 |
| - $this->uriMock->expects($this->at(2)) |
133 |
| - ->method('setPort') |
134 |
| - ->with(8080); |
135 |
| - $this->uriMock->expects($this->at(3)) |
136 |
| - ->method('setScheme') |
137 |
| - ->with('http') |
138 |
| - ->willReturnSelf(); |
139 |
| - $this->uriMock->expects($this->at(4)) |
140 |
| - ->method('setHost') |
141 |
| - ->with('127.0.0.2') |
142 |
| - ->willReturnSelf(); |
143 |
| - $this->uriMock->expects($this->at(5)) |
144 |
| - ->method('setPort') |
145 |
| - ->with(1234); |
146 |
| - $this->model->sendPurgeRequest('tags'); |
| 97 | + ] |
| 98 | + ]; |
| 99 | + } |
| 100 | + |
| 101 | + public function testSendPurgeRequestWithException() |
| 102 | + { |
| 103 | + $uris[] = UriFactory::factory('')->setHost('127.0.0.1') |
| 104 | + ->setPort(8080) |
| 105 | + ->setScheme('http'); |
| 106 | + |
| 107 | + $this->cacheServer->expects($this->once()) |
| 108 | + ->method('getUris') |
| 109 | + ->willReturn($uris); |
| 110 | + $this->socketAdapterMock->method('connect') |
| 111 | + ->willThrowException(new \Zend\Http\Client\Adapter\Exception\RuntimeException()); |
| 112 | + $this->loggerMock->expects($this->never()) |
| 113 | + ->method('execute'); |
| 114 | + $this->loggerMock->expects($this->once()) |
| 115 | + ->method('critical'); |
| 116 | + |
| 117 | + $this->assertFalse($this->model->sendPurgeRequest('tags')); |
147 | 118 | }
|
148 | 119 | }
|
0 commit comments