Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PurgeCacheTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Zend\Uri\UriFactory;
9 
10 class PurgeCacheTest extends \PHPUnit\Framework\TestCase
11 {
13  protected $model;
14 
16  protected $socketAdapterMock;
17 
19  protected $loggerMock;
20 
22  protected $cacheServer;
23 
24  protected function setUp()
25  {
26  $socketFactoryMock = $this->createMock(\Magento\CacheInvalidate\Model\SocketFactory::class);
27  $this->socketAdapterMock = $this->createMock(\Zend\Http\Client\Adapter\Socket::class);
28  $this->socketAdapterMock->expects($this->once())
29  ->method('setOptions')
30  ->with(['timeout' => 10]);
31  $socketFactoryMock->expects($this->once())
32  ->method('create')
33  ->willReturn($this->socketAdapterMock);
34 
35  $this->loggerMock = $this->createMock(\Magento\Framework\Cache\InvalidateLogger::class);
36  $this->cacheServer = $this->createMock(\Magento\PageCache\Model\Cache\Server::class);
37 
38  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
39  $this->model = $objectManager->getObject(
40  \Magento\CacheInvalidate\Model\PurgeCache::class,
41  [
42  'cacheServer' => $this->cacheServer,
43  'socketAdapterFactory' => $socketFactoryMock,
44  'logger' => $this->loggerMock,
45  ]
46  );
47  }
48 
53  public function testSendPurgeRequest($hosts)
54  {
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);
65 
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', 'Host' => $uri->getHost()]);
74  $this->socketAdapterMock->expects($this->at($i++))
75  ->method('read');
76  $i++;
77  }
78  $this->socketAdapterMock->expects($this->exactly(count($uris)))
79  ->method('close');
80 
81  $this->loggerMock->expects($this->once())
82  ->method('execute');
83 
84  $this->assertTrue($this->model->sendPurgeRequest('tags'));
85  }
86 
90  public function sendPurgeRequestDataProvider()
91  {
92  return [
93  [
94  [['host' => '127.0.0.1', 'port' => 8080]]
95  ],
96  [
97  [
98  ['host' => '127.0.0.1', 'port' => 8080],
99  ['host' => '127.0.0.2', 'port' => 1234],
100  ['host' => 'host']
101  ]
102  ]
103  ];
104  }
105 
107  {
108  $uris[] = UriFactory::factory('')->setHost('127.0.0.1')
109  ->setPort(8080)
110  ->setScheme('http');
111 
112  $this->cacheServer->expects($this->once())
113  ->method('getUris')
114  ->willReturn($uris);
115  $this->socketAdapterMock->method('connect')
116  ->willThrowException(new \Zend\Http\Client\Adapter\Exception\RuntimeException());
117  $this->loggerMock->expects($this->never())
118  ->method('execute');
119  $this->loggerMock->expects($this->once())
120  ->method('critical');
121 
122  $this->assertFalse($this->model->sendPurgeRequest('tags'));
123  }
124 }
$objectManager
Definition: bootstrap.php:17
$i
Definition: gallery.phtml:31