Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CouponGeneratorTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 use Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory;
14 
18 class CouponGeneratorTest extends \PHPUnit\Framework\TestCase
19 {
25  private $couponGenerator;
26 
30  private $couponManagementServiceMock;
31 
35  private $generationSpecFactoryMock;
36 
40  private $generationSpecMock;
41 
47  protected function setUp()
48  {
49  $this->generationSpecFactoryMock = $this->getMockBuilder(CouponGenerationSpecInterfaceFactory::class)
50  ->disableOriginalConstructor()->setMethods(['create'])->getMock();
51  $this->couponManagementServiceMock = $this->createMock(CouponManagementService::class);
52  $this->generationSpecMock = $this->createMock(CouponGenerationSpecInterface::class);
53  $this->couponGenerator = new CouponGenerator(
54  $this->couponManagementServiceMock,
55  $this->generationSpecFactoryMock
56  );
57  }
58 
64  public function testBeforeSave()
65  {
66  $expected = ['test'];
67  $this->generationSpecFactoryMock->expects($this->once())->method('create')
68  ->willReturn($this->generationSpecMock);
69  $this->couponManagementServiceMock->expects($this->once())->method('generate')
70  ->with($this->generationSpecMock)->willReturn($expected);
71  $actual = $this->couponGenerator->generateCodes([]);
72  self::assertEquals($expected, $actual);
73  }
74 }