ProfileTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Profile;
  8. /**
  9. * Class ProfileTest
  10. */
  11. class ProfileTest 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\ProfileFactory | \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $profileFactory;
  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 Profile
  35. */
  36. private $resource;
  37. /**
  38. * @var Resource | \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $resourceMock;
  41. /**
  42. * @var \Magento\Framework\DB\Select | \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $select;
  45. /**
  46. * Initialization
  47. */
  48. protected function setUp()
  49. {
  50. $this->connectionMock = $this->getMockForAbstractClass(
  51. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  52. [],
  53. '',
  54. false,
  55. false,
  56. true,
  57. ['query']
  58. );
  59. $this->dbContext = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
  60. $this->profileFactory = $this->createPartialMock(
  61. \Magento\SalesSequence\Model\ProfileFactory::class,
  62. ['create']
  63. );
  64. $this->resourceMock = $this->createPartialMock(
  65. \Magento\Framework\App\ResourceConnection::class,
  66. ['getConnection', 'getTableName']
  67. );
  68. $this->dbContext->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
  69. $this->select = $this->createMock(\Magento\Framework\DB\Select::class);
  70. $this->meta = $this->createMock(\Magento\SalesSequence\Model\Meta::class);
  71. $this->profile = $this->createMock(\Magento\SalesSequence\Model\Profile::class);
  72. $this->resource = new Profile(
  73. $this->dbContext,
  74. $this->profileFactory
  75. );
  76. }
  77. public function testLoadActiveProfile()
  78. {
  79. $profileTableName = 'sequence_profile';
  80. $profileIdFieldName = 'profile_id';
  81. $metaId = 1;
  82. $profileId = 20;
  83. $profileData = [
  84. 'profile_id' => 20,
  85. 'meta_id' => 1
  86. ];
  87. $this->profileFactory->expects($this->once())->method('create')->willReturn($this->profile);
  88. $this->resourceMock->expects($this->any())
  89. ->method('getConnection')
  90. ->willReturn($this->connectionMock);
  91. $this->resourceMock->expects($this->once())
  92. ->method('getTableName')
  93. ->willReturn($profileTableName);
  94. $this->connectionMock->expects($this->any())->method('select')->willReturn($this->select);
  95. $this->select->expects($this->at(0))
  96. ->method('from')
  97. ->with($profileTableName, [$profileIdFieldName])
  98. ->willReturn($this->select);
  99. $this->select->expects($this->at(1))
  100. ->method('where')
  101. ->with('meta_id = :meta_id')
  102. ->willReturn($this->select);
  103. $this->select->expects($this->at(2))
  104. ->method('where')
  105. ->with('is_active = 1')
  106. ->willReturn($this->select);
  107. $this->connectionMock->expects($this->once())
  108. ->method('fetchOne')
  109. ->with($this->select, ['meta_id' => $metaId])
  110. ->willReturn($profileId);
  111. $this->select->expects($this->at(3))
  112. ->method('from')
  113. ->with($profileTableName, '*', null)
  114. ->willReturn($this->select);
  115. $this->connectionMock->expects($this->any())
  116. ->method('quoteIdentifier');
  117. $this->connectionMock->expects($this->once())->method('fetchRow')->willReturn($profileData);
  118. $this->profile->expects($this->at(1))->method('setData')->with($profileData);
  119. $this->assertEquals($this->profile, $this->resource->loadActiveProfile($metaId));
  120. }
  121. }