Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SuffixTest.php
Go to the documentation of this file.
1 <?php
7 
12 class SuffixTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $context;
18 
22  protected $eventDispatcher;
23 
27  protected $registry;
28 
32  protected $config;
33 
37  protected $cacheTypeList;
38 
42  protected $urlRewriteHelper;
43 
47  protected $storeManager;
48 
52  protected $appResource;
53 
57  protected $urlFinder;
58 
62  protected $suffixModel;
63 
64  public function setUp()
65  {
66  $this->eventDispatcher = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
67  ->disableOriginalConstructor()
68  ->setMethods(['dispatch'])
69  ->getMock();
70  $this->eventDispatcher->method('dispatch')->willReturnSelf();
71  $this->context = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
72  ->disableOriginalConstructor()
73  ->setMethods(['getEventDispatcher'])
74  ->getMock();
75  $this->context->method('getEventDispatcher')->willReturn($this->eventDispatcher);
76 
77  $this->registry = $this->createMock(\Magento\Framework\Registry::class);
78  $this->config = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
79  $this->cacheTypeList = $this->getMockBuilder(\Magento\Framework\App\Cache\TypeList::class)
80  ->disableOriginalConstructor()
81  ->setMethods(['invalidate'])
82  ->getMock();
83 
84  $this->urlRewriteHelper = $this->getMockBuilder(\Magento\UrlRewrite\Helper\UrlRewrite::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManager::class)
88  ->disableOriginalConstructor()
89  ->setMethods(['getStores'])
90  ->getMock();
91  $this->storeManager->method('getStores')->willReturn([]);
92 
93  $this->appResource =$this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
94  ->disableOriginalConstructor()
95  ->getMock();
96  $this->urlFinder =$this->getMockBuilder(\Magento\UrlRewrite\Model\UrlFinderInterface::class)
97  ->setMethods(['findAllByData', 'findOneByData'])
98  ->getMock();
99  $this->urlFinder->method('findAllByData')->willReturn([]);
100 
101  $this->suffixModel = new \Magento\Catalog\Model\System\Config\Backend\Catalog\Url\Rewrite\Suffix(
102  $this->context,
103  $this->registry,
104  $this->config,
105  $this->cacheTypeList,
106  $this->urlRewriteHelper,
107  $this->storeManager,
108  $this->appResource,
109  $this->urlFinder
110  );
111  }
112 
113  public function testAfterSaveCleanCache()
114  {
115  $this->suffixModel->setValue('new');
116  $this->suffixModel->setPath(
117  \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX
118  );
119  $this->cacheTypeList->expects($this->exactly(2))->method('invalidate')->withConsecutive(
120  [$this->equalTo([
121  \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER,
122  \Magento\Framework\App\Cache\Type\Collection::TYPE_IDENTIFIER
123  ])],
124  [$this->equalTo('config')]
125  );
126  $this->suffixModel->afterSave();
127  }
128 
129  public function testAfterSaveWithoutChanges()
130  {
131  $this->suffixModel->setValue('');
132  $this->suffixModel->setPath(
133  \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX
134  );
135  $this->cacheTypeList->expects($this->never())->method('invalidate');
136  $this->suffixModel->afterSave();
137  }
138 
139  public function testAfterSaveProduct()
140  {
141  $this->suffixModel->setValue('new');
142  $this->suffixModel->setPath(
143  \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX
144  );
145  $this->cacheTypeList->expects($this->once())->method('invalidate')->with('config');
146  $this->suffixModel->afterSave();
147  }
148 }