InvalidateVarnishObserverTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CacheInvalidate\Test\Unit\Observer;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class InvalidateVarnishObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Observer\InvalidateVarnishObserver */
  11. protected $model;
  12. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Event\Observer */
  13. protected $observerMock;
  14. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Model\Config */
  15. protected $configMock;
  16. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\PurgeCache */
  17. protected $purgeCache;
  18. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\DataObject\ */
  19. protected $observerObject;
  20. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Cache\Tag\Resolver */
  21. private $tagResolver;
  22. /**
  23. * Set up all mocks and data for test
  24. */
  25. protected function setUp()
  26. {
  27. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  28. $this->configMock = $this->createPartialMock(\Magento\PageCache\Model\Config::class, ['getType', 'isEnabled']);
  29. $this->purgeCache = $this->createMock(\Magento\CacheInvalidate\Model\PurgeCache::class);
  30. $this->model = new \Magento\CacheInvalidate\Observer\InvalidateVarnishObserver(
  31. $this->configMock,
  32. $this->purgeCache
  33. );
  34. $this->tagResolver = $this->createMock(\Magento\Framework\App\Cache\Tag\Resolver::class);
  35. $helper->setBackwardCompatibleProperty($this->model, 'tagResolver', $this->tagResolver);
  36. $this->observerMock = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getEvent']);
  37. $this->observerObject = $this->createMock(\Magento\Store\Model\Store::class);
  38. }
  39. /**
  40. * Test case for cache invalidation
  41. */
  42. public function testInvalidateVarnish()
  43. {
  44. $tags = ['cache_1', 'cache_group'];
  45. $pattern = '((^|,)cache_1(,|$))|((^|,)cache_group(,|$))';
  46. $this->configMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
  47. $this->configMock->expects(
  48. $this->once()
  49. )->method(
  50. 'getType'
  51. )->will(
  52. $this->returnValue(\Magento\PageCache\Model\Config::VARNISH)
  53. );
  54. $eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getObject']);
  55. $eventMock->expects($this->once())->method('getObject')->will($this->returnValue($this->observerObject));
  56. $this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock));
  57. $this->tagResolver->expects($this->once())->method('getTags')->with($this->observerObject)
  58. ->will($this->returnValue($tags));
  59. $this->purgeCache->expects($this->once())->method('sendPurgeRequest')->with($pattern);
  60. $this->model->execute($this->observerMock);
  61. }
  62. }