Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TransactionsCollectionTest.php
Go to the documentation of this file.
1 <?php
7 
14 use PHPUnit_Framework_MockObject_MockObject as MockObject;
15 
21 class TransactionsCollectionTest extends \PHPUnit\Framework\TestCase
22 {
26  private $braintreeAdapterMock;
27 
31  private $adapterFactoryMock;
32 
36  private $entityFactoryMock;
37 
41  private $filterMapperMock;
42 
46  private $transactionMapMock;
47 
51  protected function setUp()
52  {
53  $this->transactionMapMock = $this->getMockBuilder(DocumentInterface::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->entityFactoryMock = $this->getMockBuilder(EntityFactoryInterface::class)
58  ->setMethods(['create'])
59  ->disableOriginalConstructor()
60  ->getMock();
61 
62  $this->filterMapperMock = $this->getMockBuilder(FilterMapper::class)
63  ->setMethods(['getFilter'])
64  ->disableOriginalConstructor()
65  ->getMock();
66 
67  $this->braintreeAdapterMock = $this->getMockBuilder(BraintreeAdapter::class)
68  ->setMethods(['search'])
69  ->disableOriginalConstructor()
70  ->getMock();
71  $this->adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
72  ->disableOriginalConstructor()
73  ->getMock();
74  $this->adapterFactoryMock->method('create')
75  ->willReturn($this->braintreeAdapterMock);
76  }
77 
81  public function testGetItems()
82  {
83  $this->filterMapperMock->expects($this->once())
84  ->method('getFilter')
85  ->willReturn(new BraintreeSearchNodeStub());
86 
87  $this->braintreeAdapterMock->expects($this->once())
88  ->method('search')
89  ->willReturn(['transaction1', 'transaction2']);
90 
91  $this->entityFactoryMock->expects($this->exactly(2))
92  ->method('create')
93  ->willReturn($this->transactionMapMock);
94 
96  $this->entityFactoryMock,
97  $this->adapterFactoryMock,
98  $this->filterMapperMock
99  );
100 
101  $collection->addFieldToFilter('orderId', ['like' => '0']);
102  $items = $collection->getItems();
103  $this->assertEquals(2, count($items));
104  $this->assertInstanceOf(DocumentInterface::class, $items[1]);
105  }
106 
110  public function testGetItemsEmptyCollection()
111  {
112  $this->filterMapperMock->expects($this->once())
113  ->method('getFilter')
114  ->willReturn(new BraintreeSearchNodeStub());
115 
116  $this->braintreeAdapterMock->expects($this->once())
117  ->method('search')
118  ->willReturn(null);
119 
120  $this->entityFactoryMock->expects($this->never())
121  ->method('create')
122  ->willReturn($this->transactionMapMock);
123 
125  $this->entityFactoryMock,
126  $this->adapterFactoryMock,
127  $this->filterMapperMock
128  );
129 
130  $collection->addFieldToFilter('orderId', ['like' => '0']);
131  $items = $collection->getItems();
132  $this->assertEquals(0, count($items));
133  }
134 
138  public function testGetItemsWithLimit()
139  {
141 
142  $this->filterMapperMock->expects($this->once())
143  ->method('getFilter')
144  ->willReturn(new BraintreeSearchNodeStub());
145 
146  $this->braintreeAdapterMock->expects($this->once())
147  ->method('search')
148  ->willReturn($transactions);
149 
150  $this->entityFactoryMock->expects($this->exactly(TransactionsCollection::TRANSACTION_MAXIMUM_COUNT))
151  ->method('create')
152  ->willReturn($this->transactionMapMock);
153 
155  $this->entityFactoryMock,
156  $this->adapterFactoryMock,
157  $this->filterMapperMock
158  );
160 
161  $collection->addFieldToFilter('orderId', ['like' => '0']);
162  $items = $collection->getItems();
163  $this->assertEquals(TransactionsCollection::TRANSACTION_MAXIMUM_COUNT, count($items));
164  $this->assertInstanceOf(DocumentInterface::class, $items[1]);
165  }
166 
170  public function testGetItemsWithNullLimit()
171  {
173 
174  $this->filterMapperMock->expects($this->once())
175  ->method('getFilter')
176  ->willReturn(new BraintreeSearchNodeStub());
177 
178  $this->braintreeAdapterMock->expects($this->once())
179  ->method('search')
180  ->willReturn($transactions);
181 
182  $this->entityFactoryMock->expects($this->exactly(TransactionsCollection::TRANSACTION_MAXIMUM_COUNT))
183  ->method('create')
184  ->willReturn($this->transactionMapMock);
185 
187  $this->entityFactoryMock,
188  $this->adapterFactoryMock,
189  $this->filterMapperMock
190  );
191  $collection->setPageSize(null);
192 
193  $collection->addFieldToFilter('orderId', ['like' => '0']);
194  $items = $collection->getItems();
195  $this->assertEquals(TransactionsCollection::TRANSACTION_MAXIMUM_COUNT, count($items));
196  $this->assertInstanceOf(DocumentInterface::class, $items[1]);
197  }
198 
204  public function testAddToFilter($field, $condition, $filterMapperCall, $expectedCondition)
205  {
206  $this->filterMapperMock->expects(static::exactly($filterMapperCall))
207  ->method('getFilter')
208  ->with($field, $expectedCondition)
209  ->willReturn(new BraintreeSearchNodeStub());
210 
212  $this->entityFactoryMock,
213  $this->adapterFactoryMock,
214  $this->filterMapperMock
215  );
216 
217  static::assertInstanceOf(
218  TransactionsCollection::class,
219  $collection->addFieldToFilter($field, $condition)
220  );
221  }
222 
228  public function addToFilterDataProvider()
229  {
230  return [
231  ['orderId', ['like' => 1], 1, ['like' => 1]],
232  ['type', 'sale', 1, ['eq' => 'sale']],
233  [['type', 'orderId'], [], 0, []],
234  ];
235  }
236 }
$transactions
testAddToFilter($field, $condition, $filterMapperCall, $expectedCondition)
$items