Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SourceRepositoryTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
20 use PHPUnit\Framework\TestCase;
21 
22 class SourceRepositoryTest extends TestCase
23 {
27  private $commandSave;
28 
32  private $commandGet;
33 
37  private $commandGetList;
38 
42  private $source;
43 
47  private $searchResult;
48 
52  private $sourceRepository;
53 
54  protected function setUp()
55  {
56  $this->commandSave = $this->getMockBuilder(SaveInterface::class)->getMock();
57  $this->commandGet = $this->getMockBuilder(GetInterface::class)->getMock();
58  $this->commandGetList = $this->getMockBuilder(GetListInterface::class)->getMock();
59  $this->source = $this->getMockBuilder(SourceInterface::class)->getMock();
60  $this->searchResult = $this->getMockBuilder(SourceSearchResultsInterface::class)->getMock();
61 
62  $this->sourceRepository = (new ObjectManager($this))->getObject(
63  SourceRepository::class,
64  [
65  'commandSave' => $this->commandSave,
66  'commandGet' => $this->commandGet,
67  'commandGetList' => $this->commandGetList,
68  ]
69  );
70  }
71 
72  public function testSave()
73  {
74  $sourceCode = 'source-code';
75 
76  $this->commandSave
77  ->expects($this->once())
78  ->method('execute')
79  ->with($this->source)
80  ->willReturn($sourceCode);
81 
82  $this->sourceRepository->save($this->source);
83  }
84 
90  {
91  $this->commandSave
92  ->expects($this->once())
93  ->method('execute')
94  ->with($this->source)
95  ->willThrowException(new CouldNotSaveException(__('Some error')));
96 
97  $this->sourceRepository->save($this->source);
98  }
99 
100  public function testGet()
101  {
102  $sourceCode = 'source-code';
103 
104  $this->commandGet
105  ->expects($this->once())
106  ->method('execute')
107  ->with($sourceCode)
108  ->willReturn($this->source);
109 
110  self::assertEquals($this->source, $this->sourceRepository->get($sourceCode));
111  }
112 
118  {
119  $sourceCode = 'source-code';
120 
121  $this->commandGet
122  ->expects($this->once())
123  ->method('execute')
124  ->with($sourceCode)
125  ->willThrowException(new NoSuchEntityException(__('Some error')));
126 
127  $this->sourceRepository->get($sourceCode);
128  }
129 
131  {
132  $this->commandGetList
133  ->expects($this->once())
134  ->method('execute')
135  ->with(null)
136  ->willReturn($this->searchResult);
137 
138  self::assertEquals($this->searchResult, $this->sourceRepository->getList());
139  }
140 
142  {
143  $searchCriteria = $this->getMockBuilder(SearchCriteriaInterface::class)->getMock();
144 
145  $this->commandGetList
146  ->expects($this->once())
147  ->method('execute')
148  ->with($searchCriteria)
149  ->willReturn($this->searchResult);
150 
151  self::assertEquals($this->searchResult, $this->sourceRepository->getList($searchCriteria));
152  }
153 }
__()
Definition: __.php:13
$searchCriteria
$sourceCode
Definition: inventory.phtml:11