|
5 | 5 | */
|
6 | 6 | namespace Magento\CacheInvalidate\Test\Unit\Model;
|
7 | 7 |
|
| 8 | +use \Magento\Framework\Config\ConfigOptionsListConstants; |
| 9 | + |
8 | 10 | class PurgeCacheTest extends \PHPUnit_Framework_TestCase
|
9 | 11 | {
|
10 | 12 | /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\PurgeCache */
|
11 | 13 | protected $model;
|
12 | 14 |
|
13 |
| - /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\HTTP\Adapter\Curl */ |
14 |
| - protected $curlMock; |
| 15 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Uri\Uri */ |
| 16 | + protected $uriMock; |
| 17 | + |
| 18 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Http\Client\Adapter\Socket */ |
| 19 | + protected $socketAdapterMock; |
| 20 | + |
| 21 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Cache\InvalidateLogger */ |
| 22 | + protected $loggerMock; |
15 | 23 |
|
16 |
| - /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Helper\Data */ |
17 |
| - protected $helperMock; |
| 24 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\DeploymentConfig\Reader */ |
| 25 | + protected $configReaderMock; |
18 | 26 |
|
19 |
| - /** @var \PHPUnit_Framework_MockObject_MockObject */ |
20 |
| - protected $logger; |
| 27 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\RequestInterface */ |
| 28 | + protected $requestMock; |
21 | 29 |
|
22 | 30 | /**
|
23 | 31 | * Set up all mocks and data for test
|
24 | 32 | */
|
25 | 33 | public function setUp()
|
26 | 34 | {
|
27 |
| - $this->helperMock = $this->getMock('Magento\PageCache\Helper\Data', ['getUrl'], [], '', false); |
28 |
| - $this->curlMock = $this->getMock( |
29 |
| - '\Magento\Framework\HTTP\Adapter\Curl', |
30 |
| - ['setOptions', 'write', 'read', 'close'], |
31 |
| - [], |
32 |
| - '', |
33 |
| - false |
34 |
| - ); |
35 |
| - $this->logger = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false); |
| 35 | + $this->uriMock = $this->getMock('\Zend\Uri\Uri', [], [], '', false); |
| 36 | + $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 | + $this->socketAdapterMock->expects($this->once()) |
| 41 | + ->method('setOptions') |
| 42 | + ->with(['timeout' => 10]); |
36 | 43 | $this->model = new \Magento\CacheInvalidate\Model\PurgeCache(
|
37 |
| - $this->helperMock, |
38 |
| - $this->curlMock, |
39 |
| - $this->logger |
| 44 | + $this->uriMock, |
| 45 | + $this->socketAdapterMock, |
| 46 | + $this->loggerMock, |
| 47 | + $this->configMock, |
| 48 | + $this->requestMock |
40 | 49 | );
|
41 | 50 | }
|
42 | 51 |
|
43 |
| - public function testSendPurgeRequest() |
| 52 | + public function testSendPurgeRequestEmptyConfig() |
44 | 53 | {
|
45 |
| - $tags = 'tags'; |
46 |
| - $url = 'http://mangento.index.php'; |
47 |
| - $httpVersion = '1.1'; |
48 |
| - $headers = ["X-Magento-Tags-Pattern: {$tags}"]; |
49 |
| - $this->helperMock->expects( |
50 |
| - $this->any() |
51 |
| - )->method( |
52 |
| - 'getUrl' |
53 |
| - )->with( |
54 |
| - $this->equalTo('*'), |
55 |
| - [] |
56 |
| - )->will( |
57 |
| - $this->returnValue($url) |
58 |
| - ); |
59 |
| - $this->curlMock->expects($this->once())->method('setOptions')->with([CURLOPT_CUSTOMREQUEST => 'PURGE']); |
60 |
| - $this->curlMock->expects( |
61 |
| - $this->once() |
62 |
| - )->method( |
63 |
| - 'write' |
64 |
| - )->with( |
65 |
| - $this->equalTo(''), |
66 |
| - $this->equalTo($url), |
67 |
| - $httpVersion, |
68 |
| - $this->equalTo($headers) |
69 |
| - ); |
70 |
| - $this->curlMock->expects($this->once())->method('read'); |
71 |
| - $this->curlMock->expects($this->once())->method('close'); |
72 |
| - $this->model->sendPurgeRequest($tags); |
| 54 | + $this->socketAdapterMock->expects($this->once()) |
| 55 | + ->method('write') |
| 56 | + ->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags'])); |
| 57 | + $this->socketAdapterMock->expects($this->once()) |
| 58 | + ->method('close'); |
| 59 | + $this->configMock->expects($this->once()) |
| 60 | + ->method('get') |
| 61 | + ->willReturn(''); |
| 62 | + $this->requestMock->expects($this->any()) |
| 63 | + ->method('getHttpHost') |
| 64 | + ->willReturn('127.0.0.1'); |
| 65 | + $this->uriMock->expects($this->once()) |
| 66 | + ->method('setScheme') |
| 67 | + ->with('http') |
| 68 | + ->willReturnSelf(); |
| 69 | + $this->uriMock->expects($this->once()) |
| 70 | + ->method('setHost') |
| 71 | + ->with('127.0.0.1') |
| 72 | + ->willReturnSelf(); |
| 73 | + $this->uriMock->expects($this->once()) |
| 74 | + ->method('setPort') |
| 75 | + ->with(\Magento\CacheInvalidate\Model\PurgeCache::DEFAULT_PORT); |
| 76 | + $this->model->sendPurgeRequest('tags'); |
| 77 | + } |
| 78 | + |
| 79 | + public function testSendPurgeRequestOneServer() |
| 80 | + { |
| 81 | + $this->socketAdapterMock->expects($this->once()) |
| 82 | + ->method('write') |
| 83 | + ->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags'])); |
| 84 | + $this->socketAdapterMock->expects($this->once()) |
| 85 | + ->method('close'); |
| 86 | + $this->configMock->expects($this->once()) |
| 87 | + ->method('get') |
| 88 | + ->willReturn([['host' => '127.0.0.2', 'port' => 1234]]); |
| 89 | + $this->uriMock->expects($this->once()) |
| 90 | + ->method('setScheme') |
| 91 | + ->with('http') |
| 92 | + ->willReturnSelf(); |
| 93 | + $this->uriMock->expects($this->once()) |
| 94 | + ->method('setHost') |
| 95 | + ->with('127.0.0.2') |
| 96 | + ->willReturnSelf(); |
| 97 | + $this->uriMock->expects($this->once()) |
| 98 | + ->method('setPort') |
| 99 | + ->with(1234); |
| 100 | + $this->model->sendPurgeRequest('tags'); |
| 101 | + } |
| 102 | + |
| 103 | + public function testSendPurgeRequestMultipleServers() |
| 104 | + { |
| 105 | + $this->socketAdapterMock->expects($this->exactly(2)) |
| 106 | + ->method('write') |
| 107 | + ->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags'])); |
| 108 | + $this->socketAdapterMock->expects($this->exactly(2)) |
| 109 | + ->method('close'); |
| 110 | + $this->configMock->expects($this->once()) |
| 111 | + ->method('get') |
| 112 | + ->willReturn( |
| 113 | + [ |
| 114 | + ['host' => '127.0.0.1', 'port' => 8080], |
| 115 | + ['host' => '127.0.0.2', 'port' => 1234] |
| 116 | + ] |
| 117 | + ); |
| 118 | + $this->uriMock->expects($this->at(0)) |
| 119 | + ->method('setScheme') |
| 120 | + ->with('http') |
| 121 | + ->willReturnSelf(); |
| 122 | + $this->uriMock->expects($this->at(1)) |
| 123 | + ->method('setHost') |
| 124 | + ->with('127.0.0.1') |
| 125 | + ->willReturnSelf(); |
| 126 | + $this->uriMock->expects($this->at(2)) |
| 127 | + ->method('setPort') |
| 128 | + ->with(8080); |
| 129 | + $this->uriMock->expects($this->at(3)) |
| 130 | + ->method('setScheme') |
| 131 | + ->with('http') |
| 132 | + ->willReturnSelf(); |
| 133 | + $this->uriMock->expects($this->at(4)) |
| 134 | + ->method('setHost') |
| 135 | + ->with('127.0.0.2') |
| 136 | + ->willReturnSelf(); |
| 137 | + $this->uriMock->expects($this->at(5)) |
| 138 | + ->method('setPort') |
| 139 | + ->with(1234); |
| 140 | + $this->model->sendPurgeRequest('tags'); |
73 | 141 | }
|
74 | 142 | }
|
0 commit comments