Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SequenceTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class SequenceTest extends \PHPUnit\Framework\TestCase
15 {
19  private $connectionMock;
20 
24  private $resource;
25 
29  private $profile;
30 
34  private $meta;
35 
39  private $sequence;
40 
41  protected function setUp()
42  {
43  $this->meta = $this->createPartialMock(
44  \Magento\SalesSequence\Model\Meta::class,
45  ['getSequenceTable', 'getActiveProfile']
46  );
47  $this->profile = $this->createPartialMock(
48  \Magento\SalesSequence\Model\Profile::class,
49  ['getSuffix', 'getPrefix', 'getStep', 'getStartValue']
50  );
51  $this->resource = $this->createPartialMock(\Magento\Framework\App\ResourceConnection::class, ['getConnection']);
52  $this->connectionMock = $this->getMockForAbstractClass(
53  \Magento\Framework\DB\Adapter\AdapterInterface::class,
54  [],
55  '',
56  false,
57  false,
58  true,
59  ['insert', 'lastInsertId']
60  );
61  $this->resource->expects($this->any())->method('getConnection')->willReturn($this->connectionMock);
62  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
63  $this->sequence = $helper->getObject(
64  \Magento\SalesSequence\Model\Sequence::class,
65  [
66  'meta' => $this->meta,
67  'resource' => $this->resource,
68  ]
69  );
70  }
71 
72  public function testSequenceInitialNull()
73  {
74  $this->assertNull($this->sequence->getCurrentValue());
75  }
76 
77  public function testSequenceNextValue()
78  {
79  $step = 777;
80  $startValue = 3;
81  $lastInsertId = 3; //at this step it will represents 777
82  $this->profile->expects($this->atLeastOnce())->method('getStartValue')->willReturn($startValue);
83  $this->meta->expects($this->atLeastOnce())
84  ->method('getActiveProfile')
85  ->willReturn(
86  $this->profile
87  );
88  $this->meta->expects($this->atLeastOnce())
89  ->method('getSequenceTable')
90  ->willReturn(
91  $this->sequenceParameters()->testTable
92  );
93  $this->connectionMock->expects($this->exactly(3))->method('insert')->with(
94  $this->sequenceParameters()->testTable,
95  []
96  );
97  $this->profile->expects($this->exactly(3))->method('getSuffix')->willReturn(
98  $this->sequenceParameters()->suffix
99  );
100  $this->profile->expects($this->exactly(3))->method('getPrefix')->willReturn(
101  $this->sequenceParameters()->prefix
102  );
103  $this->profile->expects($this->exactly(3))->method('getStep')->willReturn($step);
104  $lastInsertId = $this->nextIncrementStep($lastInsertId, 780);
105  $lastInsertId = $this->nextIncrementStep($lastInsertId, 1557);
106  $this->nextIncrementStep($lastInsertId, 2334);
107  }
108 
114  private function nextIncrementStep($lastInsertId, $sequenceNumber)
115  {
116  $lastInsertId++;
117  $this->connectionMock->expects($this->at(1))->method('lastInsertId')->with(
118  $this->sequenceParameters()->testTable
119  )->willReturn(
120  $lastInsertId
121  );
122  $this->assertEquals(
123  sprintf(
125  $this->sequenceParameters()->prefix,
126  $sequenceNumber,
127  $this->sequenceParameters()->suffix
128  ),
129  $this->sequence->getNextValue()
130  );
131  return $lastInsertId;
132  }
133 
137  private function sequenceParameters()
138  {
139  $data = new \stdClass();
140  $data->prefix = 'AA-';
141  $data->suffix = '-0';
142  $data->testTable = 'testSequence';
143  return $data;
144  }
145 }
$helper
Definition: iframe.phtml:13