SequenceTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesSequence\Test\Unit\Model;
  7. use Magento\SalesSequence\Model\Sequence;
  8. /**
  9. * Class SequenceTest
  10. */
  11. class SequenceTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\DB\Adapter\AdapterInterface | \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $connectionMock;
  17. /**
  18. * @var \Magento\Framework\App\ResourceConnection | \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $resource;
  21. /**
  22. * @var \Magento\SalesSequence\Model\Profile | \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $profile;
  25. /**
  26. * @var \Magento\SalesSequence\Model\Meta | \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $meta;
  29. /**
  30. * @var \Magento\SalesSequence\Model\Sequence
  31. */
  32. private $sequence;
  33. protected function setUp()
  34. {
  35. $this->meta = $this->createPartialMock(
  36. \Magento\SalesSequence\Model\Meta::class,
  37. ['getSequenceTable', 'getActiveProfile']
  38. );
  39. $this->profile = $this->createPartialMock(
  40. \Magento\SalesSequence\Model\Profile::class,
  41. ['getSuffix', 'getPrefix', 'getStep', 'getStartValue']
  42. );
  43. $this->resource = $this->createPartialMock(\Magento\Framework\App\ResourceConnection::class, ['getConnection']);
  44. $this->connectionMock = $this->getMockForAbstractClass(
  45. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  46. [],
  47. '',
  48. false,
  49. false,
  50. true,
  51. ['insert', 'lastInsertId']
  52. );
  53. $this->resource->expects($this->any())->method('getConnection')->willReturn($this->connectionMock);
  54. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  55. $this->sequence = $helper->getObject(
  56. \Magento\SalesSequence\Model\Sequence::class,
  57. [
  58. 'meta' => $this->meta,
  59. 'resource' => $this->resource,
  60. ]
  61. );
  62. }
  63. public function testSequenceInitialNull()
  64. {
  65. $this->assertNull($this->sequence->getCurrentValue());
  66. }
  67. public function testSequenceNextValue()
  68. {
  69. $step = 777;
  70. $startValue = 3;
  71. $lastInsertId = 3; //at this step it will represents 777
  72. $this->profile->expects($this->atLeastOnce())->method('getStartValue')->willReturn($startValue);
  73. $this->meta->expects($this->atLeastOnce())
  74. ->method('getActiveProfile')
  75. ->willReturn(
  76. $this->profile
  77. );
  78. $this->meta->expects($this->atLeastOnce())
  79. ->method('getSequenceTable')
  80. ->willReturn(
  81. $this->sequenceParameters()->testTable
  82. );
  83. $this->connectionMock->expects($this->exactly(3))->method('insert')->with(
  84. $this->sequenceParameters()->testTable,
  85. []
  86. );
  87. $this->profile->expects($this->exactly(3))->method('getSuffix')->willReturn(
  88. $this->sequenceParameters()->suffix
  89. );
  90. $this->profile->expects($this->exactly(3))->method('getPrefix')->willReturn(
  91. $this->sequenceParameters()->prefix
  92. );
  93. $this->profile->expects($this->exactly(3))->method('getStep')->willReturn($step);
  94. $lastInsertId = $this->nextIncrementStep($lastInsertId, 780);
  95. $lastInsertId = $this->nextIncrementStep($lastInsertId, 1557);
  96. $this->nextIncrementStep($lastInsertId, 2334);
  97. }
  98. /**
  99. * @param $lastInsertId
  100. * @param $sequenceNumber
  101. * @return mixed
  102. */
  103. private function nextIncrementStep($lastInsertId, $sequenceNumber)
  104. {
  105. $lastInsertId++;
  106. $this->connectionMock->expects($this->at(1))->method('lastInsertId')->with(
  107. $this->sequenceParameters()->testTable
  108. )->willReturn(
  109. $lastInsertId
  110. );
  111. $this->assertEquals(
  112. sprintf(
  113. Sequence::DEFAULT_PATTERN,
  114. $this->sequenceParameters()->prefix,
  115. $sequenceNumber,
  116. $this->sequenceParameters()->suffix
  117. ),
  118. $this->sequence->getNextValue()
  119. );
  120. return $lastInsertId;
  121. }
  122. /**
  123. * @return \stdClass
  124. */
  125. private function sequenceParameters()
  126. {
  127. $data = new \stdClass();
  128. $data->prefix = 'AA-';
  129. $data->suffix = '-0';
  130. $data->testTable = 'testSequence';
  131. return $data;
  132. }
  133. }