|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\CacheInvalidate\Test\Unit\Model; |
| 7 | + |
| 8 | +class ObserverTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\Observer */ |
| 11 | + protected $_model; |
| 12 | + |
| 13 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Event\Observer */ |
| 14 | + protected $_observerMock; |
| 15 | + |
| 16 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\HTTP\Adapter\Curl */ |
| 17 | + protected $_curlMock; |
| 18 | + |
| 19 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Model\Config */ |
| 20 | + protected $_configMock; |
| 21 | + |
| 22 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Helper\Data */ |
| 23 | + protected $_helperMock; |
| 24 | + |
| 25 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Object\ */ |
| 26 | + protected $_observerObject; |
| 27 | + |
| 28 | + /** |
| 29 | + * Set up all mocks and data for test |
| 30 | + */ |
| 31 | + public function setUp() |
| 32 | + { |
| 33 | + $this->_configMock = $this->getMock( |
| 34 | + 'Magento\PageCache\Model\Config', |
| 35 | + ['getType', 'isEnabled'], |
| 36 | + [], |
| 37 | + '', |
| 38 | + false |
| 39 | + ); |
| 40 | + $this->_helperMock = $this->getMock('Magento\PageCache\Helper\Data', ['getUrl'], [], '', false); |
| 41 | + $this->_curlMock = $this->getMock( |
| 42 | + '\Magento\Framework\HTTP\Adapter\Curl', |
| 43 | + ['setOptions', 'write', 'read', 'close'], |
| 44 | + [], |
| 45 | + '', |
| 46 | + false |
| 47 | + ); |
| 48 | + $this->_model = new \Magento\CacheInvalidate\Model\Observer( |
| 49 | + $this->_configMock, |
| 50 | + $this->_helperMock, |
| 51 | + $this->_curlMock |
| 52 | + ); |
| 53 | + $this->_observerMock = $this->getMock( |
| 54 | + 'Magento\Framework\Event\Observer', |
| 55 | + ['getEvent'], |
| 56 | + [], |
| 57 | + '', |
| 58 | + false |
| 59 | + ); |
| 60 | + $this->_observerObject = $this->getMock('\Magento\Store\Model\Store', [], [], '', false); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test case for cache invalidation |
| 65 | + */ |
| 66 | + public function testInvalidateVarnish() |
| 67 | + { |
| 68 | + $tags = ['cache_1', 'cache_group']; |
| 69 | + $pattern = '((^|,)cache(,|$))|((^|,)cache_1(,|$))|((^|,)cache_group(,|$))'; |
| 70 | + |
| 71 | + $this->_configMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); |
| 72 | + $this->_configMock->expects( |
| 73 | + $this->once() |
| 74 | + )->method( |
| 75 | + 'getType' |
| 76 | + )->will( |
| 77 | + $this->returnValue(\Magento\PageCache\Model\Config::VARNISH) |
| 78 | + ); |
| 79 | + $eventMock = $this->getMock('Magento\Framework\Event', ['getObject'], [], '', false); |
| 80 | + $eventMock->expects($this->once())->method('getObject')->will($this->returnValue($this->_observerObject)); |
| 81 | + $this->_observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock)); |
| 82 | + $this->_observerObject->expects($this->once())->method('getIdentities')->will($this->returnValue($tags)); |
| 83 | + $this->sendPurgeRequest($pattern); |
| 84 | + |
| 85 | + $this->_model->invalidateVarnish($this->_observerMock); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test case for flushing all the cache |
| 90 | + */ |
| 91 | + public function testFlushAllCache() |
| 92 | + { |
| 93 | + $this->_configMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); |
| 94 | + $this->_configMock->expects( |
| 95 | + $this->once() |
| 96 | + )->method( |
| 97 | + 'getType' |
| 98 | + )->will( |
| 99 | + $this->returnValue(\Magento\PageCache\Model\Config::VARNISH) |
| 100 | + ); |
| 101 | + |
| 102 | + $this->sendPurgeRequest('.*'); |
| 103 | + $this->_model->flushAllCache($this->_observerMock); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param string $tags |
| 108 | + */ |
| 109 | + protected function sendPurgeRequest($tags) |
| 110 | + { |
| 111 | + $url = 'http://mangento.index.php'; |
| 112 | + $httpVersion = '1.1'; |
| 113 | + $headers = ["X-Magento-Tags-Pattern: {$tags}"]; |
| 114 | + $this->_helperMock->expects( |
| 115 | + $this->any() |
| 116 | + )->method( |
| 117 | + 'getUrl' |
| 118 | + )->with( |
| 119 | + $this->equalTo('*'), |
| 120 | + [] |
| 121 | + )->will( |
| 122 | + $this->returnValue($url) |
| 123 | + ); |
| 124 | + $this->_curlMock->expects($this->once())->method('setOptions')->with([CURLOPT_CUSTOMREQUEST => 'PURGE']); |
| 125 | + $this->_curlMock->expects( |
| 126 | + $this->once() |
| 127 | + )->method( |
| 128 | + 'write' |
| 129 | + )->with( |
| 130 | + $this->equalTo(''), |
| 131 | + $this->equalTo($url), |
| 132 | + $httpVersion, |
| 133 | + $this->equalTo($headers) |
| 134 | + ); |
| 135 | + $this->_curlMock->expects($this->once())->method('read'); |
| 136 | + $this->_curlMock->expects($this->once())->method('close'); |
| 137 | + } |
| 138 | +} |
0 commit comments