TransactionsCollectionTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Test\Unit\Model\Report;
  7. use Magento\Braintree\Model\Adapter\BraintreeAdapter;
  8. use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
  9. use Magento\Braintree\Model\Report\FilterMapper;
  10. use Magento\Braintree\Model\Report\TransactionsCollection;
  11. use Magento\Framework\Api\Search\DocumentInterface;
  12. use Magento\Framework\Data\Collection\EntityFactoryInterface;
  13. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  14. /**
  15. * Class TransactionsCollectionTest
  16. *
  17. * Test for class \Magento\Braintree\Model\Report\TransactionsCollection
  18. */
  19. class TransactionsCollectionTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var BraintreeAdapter|MockObject
  23. */
  24. private $braintreeAdapterMock;
  25. /**
  26. * @var BraintreeAdapterFactory|MockObject
  27. */
  28. private $adapterFactoryMock;
  29. /**
  30. * @var EntityFactoryInterface|MockObject
  31. */
  32. private $entityFactoryMock;
  33. /**
  34. * @var FilterMapper|MockObject
  35. */
  36. private $filterMapperMock;
  37. /**
  38. * @var DocumentInterface|MockObject
  39. */
  40. private $transactionMapMock;
  41. /**
  42. * Setup
  43. */
  44. protected function setUp()
  45. {
  46. $this->transactionMapMock = $this->getMockBuilder(DocumentInterface::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->entityFactoryMock = $this->getMockBuilder(EntityFactoryInterface::class)
  50. ->setMethods(['create'])
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->filterMapperMock = $this->getMockBuilder(FilterMapper::class)
  54. ->setMethods(['getFilter'])
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->braintreeAdapterMock = $this->getMockBuilder(BraintreeAdapter::class)
  58. ->setMethods(['search'])
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->adapterFactoryMock->method('create')
  65. ->willReturn($this->braintreeAdapterMock);
  66. }
  67. /**
  68. * Get items
  69. */
  70. public function testGetItems()
  71. {
  72. $this->filterMapperMock->expects($this->once())
  73. ->method('getFilter')
  74. ->willReturn(new BraintreeSearchNodeStub());
  75. $this->braintreeAdapterMock->expects($this->once())
  76. ->method('search')
  77. ->willReturn(['transaction1', 'transaction2']);
  78. $this->entityFactoryMock->expects($this->exactly(2))
  79. ->method('create')
  80. ->willReturn($this->transactionMapMock);
  81. $collection = new TransactionsCollection(
  82. $this->entityFactoryMock,
  83. $this->adapterFactoryMock,
  84. $this->filterMapperMock
  85. );
  86. $collection->addFieldToFilter('orderId', ['like' => '0']);
  87. $items = $collection->getItems();
  88. $this->assertEquals(2, count($items));
  89. $this->assertInstanceOf(DocumentInterface::class, $items[1]);
  90. }
  91. /**
  92. * Get empty result
  93. */
  94. public function testGetItemsEmptyCollection()
  95. {
  96. $this->filterMapperMock->expects($this->once())
  97. ->method('getFilter')
  98. ->willReturn(new BraintreeSearchNodeStub());
  99. $this->braintreeAdapterMock->expects($this->once())
  100. ->method('search')
  101. ->willReturn(null);
  102. $this->entityFactoryMock->expects($this->never())
  103. ->method('create')
  104. ->willReturn($this->transactionMapMock);
  105. $collection = new TransactionsCollection(
  106. $this->entityFactoryMock,
  107. $this->adapterFactoryMock,
  108. $this->filterMapperMock
  109. );
  110. $collection->addFieldToFilter('orderId', ['like' => '0']);
  111. $items = $collection->getItems();
  112. $this->assertEquals(0, count($items));
  113. }
  114. /**
  115. * Get items with limit
  116. */
  117. public function testGetItemsWithLimit()
  118. {
  119. $transactions = range(1, TransactionsCollection::TRANSACTION_MAXIMUM_COUNT + 10);
  120. $this->filterMapperMock->expects($this->once())
  121. ->method('getFilter')
  122. ->willReturn(new BraintreeSearchNodeStub());
  123. $this->braintreeAdapterMock->expects($this->once())
  124. ->method('search')
  125. ->willReturn($transactions);
  126. $this->entityFactoryMock->expects($this->exactly(TransactionsCollection::TRANSACTION_MAXIMUM_COUNT))
  127. ->method('create')
  128. ->willReturn($this->transactionMapMock);
  129. $collection = new TransactionsCollection(
  130. $this->entityFactoryMock,
  131. $this->adapterFactoryMock,
  132. $this->filterMapperMock
  133. );
  134. $collection->setPageSize(TransactionsCollection::TRANSACTION_MAXIMUM_COUNT);
  135. $collection->addFieldToFilter('orderId', ['like' => '0']);
  136. $items = $collection->getItems();
  137. $this->assertEquals(TransactionsCollection::TRANSACTION_MAXIMUM_COUNT, count($items));
  138. $this->assertInstanceOf(DocumentInterface::class, $items[1]);
  139. }
  140. /**
  141. * Get items with limit
  142. */
  143. public function testGetItemsWithNullLimit()
  144. {
  145. $transactions = range(1, TransactionsCollection::TRANSACTION_MAXIMUM_COUNT + 10);
  146. $this->filterMapperMock->expects($this->once())
  147. ->method('getFilter')
  148. ->willReturn(new BraintreeSearchNodeStub());
  149. $this->braintreeAdapterMock->expects($this->once())
  150. ->method('search')
  151. ->willReturn($transactions);
  152. $this->entityFactoryMock->expects($this->exactly(TransactionsCollection::TRANSACTION_MAXIMUM_COUNT))
  153. ->method('create')
  154. ->willReturn($this->transactionMapMock);
  155. $collection = new TransactionsCollection(
  156. $this->entityFactoryMock,
  157. $this->adapterFactoryMock,
  158. $this->filterMapperMock
  159. );
  160. $collection->setPageSize(null);
  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. /**
  167. * Add fields to filter
  168. *
  169. * @dataProvider addToFilterDataProvider
  170. */
  171. public function testAddToFilter($field, $condition, $filterMapperCall, $expectedCondition)
  172. {
  173. $this->filterMapperMock->expects(static::exactly($filterMapperCall))
  174. ->method('getFilter')
  175. ->with($field, $expectedCondition)
  176. ->willReturn(new BraintreeSearchNodeStub());
  177. $collection = new TransactionsCollection(
  178. $this->entityFactoryMock,
  179. $this->adapterFactoryMock,
  180. $this->filterMapperMock
  181. );
  182. static::assertInstanceOf(
  183. TransactionsCollection::class,
  184. $collection->addFieldToFilter($field, $condition)
  185. );
  186. }
  187. /**
  188. * addToFilter DataProvider
  189. *
  190. * @return array
  191. */
  192. public function addToFilterDataProvider()
  193. {
  194. return [
  195. ['orderId', ['like' => 1], 1, ['like' => 1]],
  196. ['type', 'sale', 1, ['eq' => 'sale']],
  197. [['type', 'orderId'], [], 0, []],
  198. ];
  199. }
  200. }