Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CatalogPriceRulesFixtureTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Setup\Fixtures\CatalogPriceRulesFixture;
10 
14 class CatalogPriceRulesFixtureTest extends \PHPUnit\Framework\TestCase
15 {
19  private $fixtureModelMock;
20 
24  private $model;
25 
26  public function setUp()
27  {
28  $this->fixtureModelMock = $this->createMock(\Magento\Setup\Fixtures\FixtureModel::class);
29 
30  $this->model = new CatalogPriceRulesFixture($this->fixtureModelMock);
31  }
32 
33  public function testExecute()
34  {
35  $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
36  $storeMock->expects($this->once())
37  ->method('getRootCategoryId')
38  ->will($this->returnValue(2));
39 
40  $websiteMock = $this->createMock(\Magento\Store\Model\Website::class);
41  $websiteMock->expects($this->once())
42  ->method('getGroups')
43  ->will($this->returnValue([$storeMock]));
44  $websiteMock->expects($this->once())
45  ->method('getId')
46  ->will($this->returnValue('website_id'));
47 
48  $storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
49  $storeManagerMock->expects($this->once())
50  ->method('getWebsites')
51  ->will($this->returnValue([$websiteMock]));
52 
53  $contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
54  $abstractDbMock = $this->getMockForAbstractClass(
55  \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
56  [$contextMock],
57  '',
58  true,
59  true,
60  true,
61  ['getAllChildren']
62  );
63  $abstractDbMock->expects($this->once())
64  ->method('getAllChildren')
65  ->will($this->returnValue([1]));
66 
67  $categoryMock = $this->createMock(\Magento\Catalog\Model\Category::class);
68  $categoryMock->expects($this->once())
69  ->method('getResource')
70  ->will($this->returnValue($abstractDbMock));
71  $categoryMock->expects($this->once())
72  ->method('getPath')
73  ->will($this->returnValue('path/to/file'));
74  $categoryMock->expects($this->once())
75  ->method('getId')
76  ->will($this->returnValue('category_id'));
77 
78  $modelMock = $this->createMock(\Magento\CatalogRule\Model\Rule::class);
79  $metadataMock = $this->createMock(\Magento\Framework\EntityManager\EntityMetadata::class);
80  $metadataPoolMock = $this->createMock(\Magento\Framework\EntityManager\MetadataPool::class);
81  $metadataMock->expects($this->once())
82  ->method('getLinkField')
83  ->will($this->returnValue('Field Id Name'));
84 
85  $valueMap = [
86  [\Magento\CatalogRule\Model\Rule::class, $modelMock],
87  [\Magento\Catalog\Model\Category::class, $categoryMock],
88  [\Magento\Framework\EntityManager\MetadataPool::class, $metadataPoolMock]
89  ];
90  $metadataPoolMock
91  ->expects($this->once())
92  ->method('getMetadata')
93  ->with(\Magento\CatalogRule\Api\Data\RuleInterface::class)
94  ->willReturn($metadataMock);
95  $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManager\ObjectManager::class);
96  $objectManagerMock->expects($this->once())
97  ->method('create')
98  ->will($this->returnValue($storeManagerMock));
99  $objectManagerMock->expects($this->exactly(3))
100  ->method('get')
101  ->will($this->returnValueMap($valueMap));
102 
103  $this->fixtureModelMock
104  ->expects($this->once())
105  ->method('getValue')
106  ->will($this->returnValue(1));
107  $this->fixtureModelMock
108  ->expects($this->exactly(4))
109  ->method('getObjectManager')
110  ->will($this->returnValue($objectManagerMock));
111 
112  $this->model->execute();
113  }
114 
115  public function testNoFixtureConfigValue()
116  {
117  $ruleMock = $this->createMock(\Magento\SalesRule\Model\Rule::class);
118  $ruleMock->expects($this->never())->method('save');
119 
120  $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManager\ObjectManager::class);
121  $objectManagerMock->expects($this->never())
122  ->method('get')
123  ->with($this->equalTo(\Magento\SalesRule\Model\Rule::class))
124  ->willReturn($ruleMock);
125 
126  $this->fixtureModelMock
127  ->expects($this->never())
128  ->method('getObjectManager')
129  ->willReturn($objectManagerMock);
130  $this->fixtureModelMock
131  ->expects($this->once())
132  ->method('getValue')
133  ->willReturn(false);
134 
135  $this->model->execute();
136  }
137 
138  public function testGetActionTitle()
139  {
140  $this->assertSame('Generating catalog price rules', $this->model->getActionTitle());
141  }
142 
143  public function testIntroduceParamLabels()
144  {
145  $this->assertSame([
146  'catalog_price_rules' => 'Catalog Price Rules'
147  ], $this->model->introduceParamLabels());
148  }
149 }