Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BuilderTest.php
Go to the documentation of this file.
1 <?php
7 
11 class BuilderTest extends \PHPUnit\Framework\TestCase
12 {
16  private $sequenceBuilder;
17 
21  private $resourceSequenceMeta;
22 
26  private $meta;
27 
31  private $profile;
32 
36  private $metaFactory;
37 
41  private $profileFactory;
42 
46  private $connectionMock;
47 
51  private $sequence;
52 
56  private $resourceMock;
57 
58  protected function setUp()
59  {
60  $this->connectionMock = $this->getMockForAbstractClass(
61  \Magento\Framework\DB\Adapter\AdapterInterface::class,
62  [],
63  '',
64  false,
65  false,
66  true,
67  ['query']
68  );
69  $this->resourceSequenceMeta = $this->createPartialMock(
70  \Magento\SalesSequence\Model\ResourceModel\Meta::class,
71  ['loadByEntityTypeAndStore', 'save', 'createSequence']
72  );
73  $this->meta = $this->createPartialMock(
74  \Magento\SalesSequence\Model\Meta::class,
75  ['getId', 'setData', 'save', 'getSequenceTable']
76  );
77  $this->sequence = $this->createMock(\Magento\Framework\DB\Ddl\Sequence::class);
78  $this->resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
79  $this->profile = $this->createPartialMock(
80  \Magento\SalesSequence\Model\Profile::class,
81  ['getId', 'setData', 'getStartValue']
82  );
83  $this->metaFactory = $this->createPartialMock(\Magento\SalesSequence\Model\MetaFactory::class, ['create']);
84  $this->metaFactory->expects($this->any())->method('create')->willReturn($this->meta);
85  $this->profileFactory = $this->createPartialMock(
86  \Magento\SalesSequence\Model\ProfileFactory::class,
87  ['create']
88  );
89  $this->profileFactory->expects($this->any())->method('create')->willReturn($this->profile);
90  $this->resourceMock->expects($this->atLeastOnce())
91  ->method('getTableName')
92  ->willReturn('sequence_lalalka_1');
93 
94  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
95  $this->sequenceBuilder = $helper->getObject(
96  \Magento\SalesSequence\Model\Builder::class,
97  [
98  'resourceMetadata' => $this->resourceSequenceMeta,
99  'metaFactory' => $this->metaFactory,
100  'profileFactory' => $this->profileFactory,
101  'appResource' => $this->resourceMock,
102  'ddlSequence' => $this->sequence
103  ]
104  );
105  }
106 
107  public function testAddSequenceExistMeta()
108  {
109  $entityType = 'lalalka';
110  $storeId = 1;
111  $this->resourceSequenceMeta->expects($this->once())
112  ->method('loadByEntityTypeAndStore')
113  ->with($entityType, $storeId)
114  ->willReturn($this->meta);
115  $this->meta->expects($this->once())
116  ->method('getSequenceTable')
117  ->willReturn('sequence_lalalka_1');
118  $this->profileFactory->expects($this->never())
119  ->method('create');
120  $this->sequenceBuilder->setEntityType($entityType)
121  ->setStoreId($storeId)
122  ->setSuffix('SUFF')
123  ->setPrefix('PREF')
124  ->setStartValue(1)
125  ->setStep(1)
126  ->setWarningValue(9999999)
127  ->setMaxValue(912992192)
128  ->create();
129  }
130 
131  public function testAddSequence()
132  {
133  $entityType = 'lalalka';
134  $storeId = 1;
135  $prefix = 'PRE';
136  $suffix = 'SUF';
137  $startValue = 1;
138  $step = 1;
139  $maxValue = 120000;
140  $warningValue = 110000;
141  $this->resourceSequenceMeta->expects($this->once())
142  ->method('loadByEntityTypeAndStore')
143  ->with($entityType, $storeId)
144  ->willReturn($this->meta);
145  $this->meta->expects($this->once())
146  ->method('getSequenceTable')
147  ->willReturn(null);
148  $this->profileFactory->expects($this->once())
149  ->method('create')
150  ->with([
151  'data' => [
152  'prefix' => $prefix,
153  'suffix' => $suffix,
154  'start_value' => $startValue,
155  'step' => $step,
156  'max_value' => $maxValue,
157  'warning_value' => $warningValue,
158  'is_active' => 1
159  ]
160  ])->willReturn($this->profile);
161  $sequenceTable = sprintf('sequence_%s_%s', $entityType, $storeId);
162  $this->metaFactory->expects($this->once())
163  ->method('create')
164  ->with([
165  'data' => [
166  'entity_type' => $entityType,
167  'store_id' => $storeId,
168  'sequence_table' => $sequenceTable,
169  'active_profile' => $this->profile
170  ]
171  ])->willReturn($this->meta);
172  $this->resourceSequenceMeta->expects($this->once())->method('save')->willReturn($this->meta);
173  $this->stepCreateSequence($sequenceTable, $startValue);
174  $this->sequenceBuilder->setEntityType($entityType)
175  ->setStoreId($storeId)
176  ->setPrefix($prefix)
177  ->setSuffix($suffix)
178  ->setStartValue($startValue)
179  ->setStep($step)
180  ->setMaxValue($maxValue)
181  ->setWarningValue($warningValue)
182  ->create();
183  }
184 
191  private function stepCreateSequence($sequenceName, $startNumber)
192  {
193  $sql = "some sql";
194  $this->resourceMock->expects($this->atLeastOnce())
195  ->method('getTableName');
196  $this->resourceMock->expects($this->any())
197  ->method('getConnection')
198  ->with('sales')
199  ->willReturn($this->connectionMock);
200  $this->sequence->expects($this->once())
201  ->method('getCreateSequenceDdl')
202  ->with($sequenceName, $startNumber)
203  ->willReturn($sql);
204  $this->connectionMock->expects($this->once())->method('query')->with($sql);
205  }
206 }
$helper
Definition: iframe.phtml:13
$suffix
Definition: name.phtml:27
$prefix
Definition: name.phtml:25