SourceRepositoryTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Inventory\Test\Unit\Model;
  8. use Magento\Framework\Api\SearchCriteriaInterface;
  9. use Magento\Framework\Exception\CouldNotSaveException;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  12. use Magento\Inventory\Model\Source\Command\GetInterface;
  13. use Magento\Inventory\Model\Source\Command\GetListInterface;
  14. use Magento\Inventory\Model\Source\Command\SaveInterface;
  15. use Magento\Inventory\Model\SourceRepository;
  16. use Magento\InventoryApi\Api\Data\SourceInterface;
  17. use Magento\InventoryApi\Api\Data\SourceSearchResultsInterface;
  18. use PHPUnit\Framework\TestCase;
  19. class SourceRepositoryTest extends TestCase
  20. {
  21. /**
  22. * @var SaveInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $commandSave;
  25. /**
  26. * @var GetInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $commandGet;
  29. /**
  30. * @var GetListInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $commandGetList;
  33. /**
  34. * @var SourceInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $source;
  37. /**
  38. * @var SourceSearchResultsInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $searchResult;
  41. /**
  42. * @var SourceRepository
  43. */
  44. private $sourceRepository;
  45. protected function setUp()
  46. {
  47. $this->commandSave = $this->getMockBuilder(SaveInterface::class)->getMock();
  48. $this->commandGet = $this->getMockBuilder(GetInterface::class)->getMock();
  49. $this->commandGetList = $this->getMockBuilder(GetListInterface::class)->getMock();
  50. $this->source = $this->getMockBuilder(SourceInterface::class)->getMock();
  51. $this->searchResult = $this->getMockBuilder(SourceSearchResultsInterface::class)->getMock();
  52. $this->sourceRepository = (new ObjectManager($this))->getObject(
  53. SourceRepository::class,
  54. [
  55. 'commandSave' => $this->commandSave,
  56. 'commandGet' => $this->commandGet,
  57. 'commandGetList' => $this->commandGetList,
  58. ]
  59. );
  60. }
  61. public function testSave()
  62. {
  63. $sourceCode = 'source-code';
  64. $this->commandSave
  65. ->expects($this->once())
  66. ->method('execute')
  67. ->with($this->source)
  68. ->willReturn($sourceCode);
  69. $this->sourceRepository->save($this->source);
  70. }
  71. /**
  72. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  73. * @expectedExceptionMessage Some error
  74. */
  75. public function testSaveWithCouldNotSaveException()
  76. {
  77. $this->commandSave
  78. ->expects($this->once())
  79. ->method('execute')
  80. ->with($this->source)
  81. ->willThrowException(new CouldNotSaveException(__('Some error')));
  82. $this->sourceRepository->save($this->source);
  83. }
  84. public function testGet()
  85. {
  86. $sourceCode = 'source-code';
  87. $this->commandGet
  88. ->expects($this->once())
  89. ->method('execute')
  90. ->with($sourceCode)
  91. ->willReturn($this->source);
  92. self::assertEquals($this->source, $this->sourceRepository->get($sourceCode));
  93. }
  94. /**
  95. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  96. * @expectedExceptionMessage Some error
  97. */
  98. public function testGetWithNoSuchEntityException()
  99. {
  100. $sourceCode = 'source-code';
  101. $this->commandGet
  102. ->expects($this->once())
  103. ->method('execute')
  104. ->with($sourceCode)
  105. ->willThrowException(new NoSuchEntityException(__('Some error')));
  106. $this->sourceRepository->get($sourceCode);
  107. }
  108. public function testGetListWithoutSearchCriteria()
  109. {
  110. $this->commandGetList
  111. ->expects($this->once())
  112. ->method('execute')
  113. ->with(null)
  114. ->willReturn($this->searchResult);
  115. self::assertEquals($this->searchResult, $this->sourceRepository->getList());
  116. }
  117. public function testGetListWithSearchCriteria()
  118. {
  119. $searchCriteria = $this->getMockBuilder(SearchCriteriaInterface::class)->getMock();
  120. $this->commandGetList
  121. ->expects($this->once())
  122. ->method('execute')
  123. ->with($searchCriteria)
  124. ->willReturn($this->searchResult);
  125. self::assertEquals($this->searchResult, $this->sourceRepository->getList($searchCriteria));
  126. }
  127. }