Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ClearProductUrlsObserverTest.php
Go to the documentation of this file.
1 <?php
7 
14 
19 class ClearProductUrlsObserverTest extends \PHPUnit\Framework\TestCase
20 {
25 
29  protected $urlPersist;
30 
34  protected $observer;
35 
39  protected $event;
40 
44  protected $importProduct;
45 
50 
56  protected $products = [
57  [
58  'sku' => 'sku',
59  'url_key' => 'value1',
60  ],
61  [
62  'sku' => 'sku3',
63  'url_key' => 'value3',
64  ],
65  [
66  'sku' => 'SKU5',
67  'url_key' => 'value5',
68  ]
69  ];
70 
74  protected function setUp()
75  {
76  $this->importProduct = $this->getMockBuilder(Product::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79  $this->event = $this->getMockBuilder(Event::class)
80  ->setMethods(['getBunch', 'getAdapter'])
81  ->disableOriginalConstructor()
82  ->getMock();
83  $this->event->expects($this->once())
84  ->method('getAdapter')
85  ->willReturn($this->importProduct);
86  $this->event->expects($this->once())
87  ->method('getBunch')
88  ->willReturn($this->products);
89  $this->observer = $this->getMockBuilder(Observer::class)
90  ->setMethods(['getEvent'])
91  ->disableOriginalConstructor()
92  ->getMock();
93  $this->observer->expects($this->exactly(2))
94  ->method('getEvent')
95  ->willReturn($this->event);
96  $this->urlPersist = $this->getMockBuilder(UrlPersistInterface::class)
97  ->disableOriginalConstructor()
98  ->getMock();
99 
100  $this->clearProductUrlsObserver = new ClearProductUrlsObserver($this->urlPersist);
101  }
102 
106  public function testClearProductUrls()
107  {
108  $oldSKus = [
109  'sku' => ['entity_id' => 1],
110  'sku5' => ['entity_id' => 5],
111  ];
112  $this->importProduct->expects($this->once())
113  ->method('getOldSku')
114  ->willReturn($oldSKus);
115  $this->urlPersist->expects($this->once())
116  ->method('deleteByData')
117  ->with([
118  'entity_id' => [1, 5],
119  'entity_type' => 'product'
120  ]);
121 
122  $this->clearProductUrlsObserver->execute($this->observer);
123  }
124 }