MetaTest.php 4.9 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\ResourceModel;
  7. use Magento\SalesSequence\Model\ResourceModel\Meta;
  8. /**
  9. * Class MetaTest
  10. */
  11. class MetaTest 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\Model\ResourceModel\Db\Context | \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $dbContext;
  21. /**
  22. * @var \Magento\SalesSequence\Model\MetaFactory | \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $metaFactory;
  25. /**
  26. * @var \Magento\SalesSequence\Model\Meta | \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $meta;
  29. /**
  30. * @var \Magento\SalesSequence\Model\Profile | \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $profile;
  33. /**
  34. * @var \Magento\SalesSequence\Model\ResourceModel\Profile | \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $resourceProfile;
  37. /**
  38. * @var Meta
  39. */
  40. private $resource;
  41. /**
  42. * @var Resource | \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $resourceMock;
  45. /**
  46. * @var \Magento\Framework\DB\Select | \PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $select;
  49. /**
  50. * Initialization
  51. */
  52. protected function setUp()
  53. {
  54. $this->connectionMock = $this->getMockForAbstractClass(
  55. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  56. [],
  57. '',
  58. false,
  59. false,
  60. true,
  61. ['query']
  62. );
  63. $this->dbContext = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
  64. $this->metaFactory = $this->createPartialMock(\Magento\SalesSequence\Model\MetaFactory::class, ['create']);
  65. $this->resourceProfile = $this->createPartialMock(
  66. \Magento\SalesSequence\Model\ResourceModel\Profile::class,
  67. ['loadActiveProfile', 'save']
  68. );
  69. $this->resourceMock = $this->createPartialMock(
  70. \Magento\Framework\App\ResourceConnection::class,
  71. ['getConnection', 'getTableName']
  72. );
  73. $this->dbContext->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
  74. $this->select = $this->createMock(\Magento\Framework\DB\Select::class);
  75. $this->meta = $this->createMock(\Magento\SalesSequence\Model\Meta::class);
  76. $this->profile = $this->createMock(\Magento\SalesSequence\Model\Profile::class);
  77. $this->resource = new Meta(
  78. $this->dbContext,
  79. $this->metaFactory,
  80. $this->resourceProfile
  81. );
  82. }
  83. public function testLoadBy()
  84. {
  85. $metaTableName = 'sequence_meta';
  86. $metaIdFieldName = 'meta_id';
  87. $entityType = 'order';
  88. $storeId = 1;
  89. $metaId = 1;
  90. $metaData = [
  91. 'meta_id' => 1,
  92. 'profile_id' => 2
  93. ];
  94. $this->resourceMock->expects($this->any())
  95. ->method('getConnection')
  96. ->willReturn($this->connectionMock);
  97. $this->resourceMock->expects($this->once())
  98. ->method('getTableName')
  99. ->willReturn($metaTableName);
  100. $this->connectionMock->expects($this->any())->method('select')->willReturn($this->select);
  101. $this->select->expects($this->at(0))
  102. ->method('from')
  103. ->with($metaTableName, [$metaIdFieldName])
  104. ->willReturn($this->select);
  105. $this->select->expects($this->at(1))
  106. ->method('where')
  107. ->with('entity_type = :entity_type AND store_id = :store_id')
  108. ->willReturn($this->select);
  109. $this->connectionMock->expects($this->once())
  110. ->method('fetchOne')
  111. ->with($this->select, ['entity_type' => $entityType, 'store_id' => $storeId])
  112. ->willReturn($metaId);
  113. $this->metaFactory->expects($this->once())->method('create')->willReturn($this->meta);
  114. $this->stepCheckSaveWithActiveProfile($metaData);
  115. $this->meta->expects($this->once())->method('beforeLoad');
  116. $this->assertEquals($this->meta, $this->resource->loadByEntityTypeAndStore($entityType, $storeId));
  117. }
  118. /**
  119. * @param $metaData
  120. */
  121. private function stepCheckSaveWithActiveProfile($metaData)
  122. {
  123. $this->select->expects($this->at(2))
  124. ->method('from')
  125. ->with('sequence_meta', '*', null)
  126. ->willReturn($this->select);
  127. $this->connectionMock->expects($this->any())
  128. ->method('quoteIdentifier');
  129. $this->connectionMock->expects($this->once())->method('fetchRow')->willReturn($metaData);
  130. $this->resourceProfile->expects($this->once())->method('loadActiveProfile')->willReturn($this->profile);
  131. }
  132. }