CatalogAttributeSaveAfterObserverTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Test\Unit\Observer;
  7. class CatalogAttributeSaveAfterObserverTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\SalesRule\Observer\CatalogAttributeSaveAfterObserver|\PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $model;
  13. /**
  14. * @var \Magento\SalesRule\Observer\CheckSalesRulesAvailability|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $checkSalesRulesAvailability;
  17. protected function setUp()
  18. {
  19. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  20. $this->initMocks();
  21. $this->model = $helper->getObject(
  22. \Magento\SalesRule\Observer\CatalogAttributeSaveAfterObserver::class,
  23. [
  24. 'checkSalesRulesAvailability' => $this->checkSalesRulesAvailability
  25. ]
  26. );
  27. }
  28. protected function initMocks()
  29. {
  30. $this->checkSalesRulesAvailability = $this->createMock(
  31. \Magento\SalesRule\Observer\CheckSalesRulesAvailability::class
  32. );
  33. }
  34. public function testCatalogAttributeSaveAfter()
  35. {
  36. $attributeCode = 'attributeCode';
  37. $observer = $this->createMock(\Magento\Framework\Event\Observer::class);
  38. $event = $this->createPartialMock(\Magento\Framework\Event::class, ['getAttribute', '__wakeup']);
  39. $attribute = $this->createPartialMock(
  40. \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class,
  41. ['dataHasChangedFor', 'getIsUsedForPromoRules', 'getAttributeCode', '__wakeup']
  42. );
  43. $observer->expects($this->once())
  44. ->method('getEvent')
  45. ->will($this->returnValue($event));
  46. $event->expects($this->any())
  47. ->method('getAttribute')
  48. ->will($this->returnValue($attribute));
  49. $attribute->expects($this->any())
  50. ->method('dataHasChangedFor')
  51. ->with('is_used_for_promo_rules')
  52. ->will($this->returnValue(true));
  53. $attribute->expects($this->any())
  54. ->method('getIsUsedForPromoRules')
  55. ->will($this->returnValue(false));
  56. $attribute->expects($this->any())
  57. ->method('getAttributeCode')
  58. ->will($this->returnValue($attributeCode));
  59. $this->checkSalesRulesAvailability
  60. ->expects($this->once())
  61. ->method('checkSalesRulesAvailability')
  62. ->willReturn('true');
  63. $this->assertEquals($this->model, $this->model->execute($observer));
  64. }
  65. }