Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InvalidateVarnishObserverTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class InvalidateVarnishObserverTest extends \PHPUnit\Framework\TestCase
11 {
13  protected $model;
14 
16  protected $observerMock;
17 
19  protected $configMock;
20 
22  protected $purgeCache;
23 
25  protected $observerObject;
26 
28  private $tagResolver;
29 
33  protected function setUp()
34  {
35  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
36 
37  $this->configMock = $this->createPartialMock(\Magento\PageCache\Model\Config::class, ['getType', 'isEnabled']);
38  $this->purgeCache = $this->createMock(\Magento\CacheInvalidate\Model\PurgeCache::class);
39  $this->model = new \Magento\CacheInvalidate\Observer\InvalidateVarnishObserver(
40  $this->configMock,
41  $this->purgeCache
42  );
43 
44  $this->tagResolver = $this->createMock(\Magento\Framework\App\Cache\Tag\Resolver::class);
45  $helper->setBackwardCompatibleProperty($this->model, 'tagResolver', $this->tagResolver);
46 
47  $this->observerMock = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getEvent']);
48  $this->observerObject = $this->createMock(\Magento\Store\Model\Store::class);
49  }
50 
54  public function testInvalidateVarnish()
55  {
56  $tags = ['cache_1', 'cache_group'];
57  $pattern = '((^|,)cache_1(,|$))|((^|,)cache_group(,|$))';
58 
59  $this->configMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
60  $this->configMock->expects(
61  $this->once()
62  )->method(
63  'getType'
64  )->will(
65  $this->returnValue(\Magento\PageCache\Model\Config::VARNISH)
66  );
67 
68  $eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getObject']);
69  $eventMock->expects($this->once())->method('getObject')->will($this->returnValue($this->observerObject));
70  $this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock));
71  $this->tagResolver->expects($this->once())->method('getTags')->with($this->observerObject)
72  ->will($this->returnValue($tags));
73  $this->purgeCache->expects($this->once())->method('sendPurgeRequest')->with($pattern);
74 
75  $this->model->execute($this->observerMock);
76  }
77 }
$helper
Definition: iframe.phtml:13
$pattern
Definition: website.php:22