AgreementTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Model\ResourceModel\Billing;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * Class AgreementTest
  10. */
  11. class AgreementTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Paypal\Model\ResourceModel\Billing\Agreement
  15. */
  16. protected $agreementResource;
  17. /**
  18. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $connectionMock;
  21. /**
  22. * @var \Magento\Framework\Data\Collection\AbstractDb|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $collectionMock;
  25. /**
  26. * @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $selectMock;
  29. /**
  30. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $resourceConnectionMock;
  33. protected function setUp()
  34. {
  35. $objectManager = new ObjectManagerHelper($this);
  36. $contextMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\Context::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->resourceConnectionMock = $this->createPartialMock(\Magento\Framework\App\ResourceConnection::class, [
  40. 'getConnection',
  41. 'getTableName'
  42. ]);
  43. $this->collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
  44. ->disableOriginalConstructor()
  45. ->setMethods(['getSelect'])
  46. ->getMockForAbstractClass();
  47. $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
  48. $this->selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  49. $contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceConnectionMock);
  50. $this->agreementResource = $objectManager->getObject(
  51. \Magento\Paypal\Model\ResourceModel\Billing\Agreement::class,
  52. [
  53. 'context' => $contextMock,
  54. ]
  55. );
  56. }
  57. public function testAddOrdersFilter()
  58. {
  59. $this->resourceConnectionMock->expects($this->exactly(2))
  60. ->method('getConnection')
  61. ->willReturn($this->connectionMock);
  62. $this->resourceConnectionMock->expects($this->once())
  63. ->method('getTableName')
  64. ->with('paypal_billing_agreement_order')
  65. ->willReturn('pref_paypal_billing_agreement_order');
  66. $this->connectionMock->expects($this->once())
  67. ->method('select')
  68. ->willReturn($this->selectMock);
  69. $this->selectMock->expects($this->once())
  70. ->method('from')
  71. ->with(['pbao' => 'pref_paypal_billing_agreement_order'], ['order_id'], null)
  72. ->willReturnSelf();
  73. $this->selectMock->expects($this->exactly(2))
  74. ->method('where')
  75. ->withConsecutive(
  76. ['pbao.agreement_id IN(?)', [100]],
  77. ['main_table.entity_id IN (?)', [500]]
  78. )
  79. ->willReturnSelf();
  80. $this->connectionMock->expects($this->once())
  81. ->method('fetchCol')
  82. ->with($this->selectMock, [])
  83. ->willReturn([500]);
  84. $this->collectionMock->expects($this->once())
  85. ->method('getSelect')
  86. ->willReturn($this->selectMock);
  87. $this->assertEquals(
  88. $this->agreementResource,
  89. $this->agreementResource->addOrdersFilter($this->collectionMock, 100)
  90. );
  91. }
  92. }