Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CouponTest.php
Go to the documentation of this file.
1 <?php
7 
11 class CouponTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $resourceMock;
17 
21  protected $eventManager;
22 
26  protected $couponModel;
27 
28  protected function setUp()
29  {
30  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
31 
32  $this->resourceMock = $this->createPartialMock(
33  \Magento\SalesRule\Model\ResourceModel\Coupon::class,
34  ['loadPrimaryByRule', 'load', '__wakeup', 'getIdFieldName']
35  );
36  $this->eventManager = $this->createPartialMock(\Magento\Framework\Event\Manager::class, ['dispatch']);
37 
38  $context = $this->createPartialMock(\Magento\Framework\Model\Context::class, ['getEventDispatcher']);
39 
40  $context->expects($this->once())->method('getEventDispatcher')->will($this->returnValue($this->eventManager));
41 
42  $this->couponModel = $objectManager->getObject(
43  \Magento\SalesRule\Model\Coupon::class,
44  [
45  'resource' => $this->resourceMock,
46  'context' => $context
47  ]
48  );
49  }
50 
54  public function testSetRule()
55  {
57  $ruleMock = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, ['getId', '__wakeup']);
58  $ruleMock->expects($this->once())->method('getId');
59 
60  $this->assertEquals($this->couponModel, $this->couponModel->setRule($ruleMock));
61  }
62 
66  public function testLoadPrimaryByRule()
67  {
68  $this->resourceMock->expects($this->once())->method('loadPrimaryByRule');
69 
70  $this->assertEquals($this->couponModel, $this->couponModel->loadPrimaryByRule(1));
71  }
72 
76  public function testLoadByCode()
77  {
78  $this->eventManager->expects($this->any())->method('dispatch');
79  $this->resourceMock->expects($this->once())->method('load');
80 
81  $this->assertEquals($this->couponModel, $this->couponModel->loadByCode('code-value'));
82  }
83 }
$objectManager
Definition: bootstrap.php:17