AbstractTestCase.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Widget\Test\Unit\Model\ResourceModel\Layout;
  7. abstract class AbstractTestCase extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * Test 'where' condition for assertion
  11. */
  12. const TEST_WHERE_CONDITION = 'condition = 1';
  13. /**
  14. * Test interval in days
  15. */
  16. const TEST_DAYS_BEFORE = 3;
  17. /**
  18. * @var \Magento\Widget\Model\ResourceModel\Layout\Update\Collection
  19. */
  20. protected $_collection;
  21. /**
  22. * Name of main table alias
  23. *
  24. * @var string
  25. */
  26. protected $_tableAlias = 'main_table';
  27. /**
  28. * Expected conditions for testAddUpdatedDaysBeforeFilter
  29. *
  30. * @var array
  31. */
  32. protected $_expectedConditions = [];
  33. protected function setUp()
  34. {
  35. $this->_expectedConditions = [
  36. 'counter' => 0,
  37. 'data' => [
  38. 0 => [$this->_tableAlias . '.updated_at', ['notnull' => true]],
  39. 1 => [$this->_tableAlias . '.updated_at', ['lt' => 'date']],
  40. ],
  41. ];
  42. }
  43. /**
  44. * Retrieve resource model instance
  45. *
  46. * @param \Magento\Framework\DB\Select $select
  47. * @return \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected function _getResource(\Magento\Framework\DB\Select $select)
  50. {
  51. $connection = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
  52. $connection->expects($this->once())->method('select')->will($this->returnValue($select));
  53. $connection->expects($this->any())->method('quoteIdentifier')->will($this->returnArgument(0));
  54. $resource = $this->getMockForAbstractClass(
  55. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  56. [],
  57. '',
  58. false,
  59. true,
  60. true,
  61. ['getConnection', 'getMainTable', 'getTable', '__wakeup']
  62. );
  63. $resource->expects($this->any())->method('getConnection')->will($this->returnValue($connection));
  64. $resource->expects($this->any())->method('getTable')->will($this->returnArgument(0));
  65. return $resource;
  66. }
  67. /**
  68. * @abstract
  69. * @param \Magento\Framework\DB\Select $select
  70. * @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  71. */
  72. abstract protected function _getCollection(\Magento\Framework\DB\Select $select);
  73. public function testAddUpdatedDaysBeforeFilter()
  74. {
  75. $select = $this->createMock(\Magento\Framework\DB\Select::class);
  76. $select->expects($this->any())->method('where')->with(self::TEST_WHERE_CONDITION);
  77. $collection = $this->_getCollection($select);
  78. /** @var $connection \PHPUnit_Framework_MockObject_MockObject */
  79. $connection = $collection->getResource()->getConnection();
  80. $connection->expects(
  81. $this->any()
  82. )->method(
  83. 'prepareSqlCondition'
  84. )->will(
  85. $this->returnCallback([$this, 'verifyPrepareSqlCondition'])
  86. );
  87. // expected date without time
  88. $datetime = new \DateTime('now', new \DateTimeZone('UTC'));
  89. $storeInterval = new \DateInterval('P' . self::TEST_DAYS_BEFORE . 'D');
  90. $datetime->sub($storeInterval);
  91. $dateTimeLib = new \Magento\Framework\Stdlib\DateTime();
  92. $expectedDate = $dateTimeLib->formatDate($datetime->getTimestamp());
  93. $this->_expectedConditions['data'][1][1]['lt'] = $expectedDate;
  94. $collection->addUpdatedDaysBeforeFilter(self::TEST_DAYS_BEFORE);
  95. }
  96. /**
  97. * Assert SQL condition
  98. *
  99. * @param string $fieldName
  100. * @param array $condition
  101. * @return string
  102. */
  103. public function verifyPrepareSqlCondition($fieldName, $condition)
  104. {
  105. $counter = $this->_expectedConditions['counter'];
  106. $data = $this->_expectedConditions['data'][$counter];
  107. $this->_expectedConditions['counter']++;
  108. $this->assertEquals($data[0], $fieldName);
  109. $this->assertCount(1, $data[1]);
  110. $key = array_keys($data[1]);
  111. $key = reset($key);
  112. $value = reset($data[1]);
  113. $this->assertArrayHasKey($key, $condition);
  114. if ($key == 'lt') {
  115. $this->assertContains($value, $condition[$key]);
  116. } else {
  117. $this->assertContains($value, $condition);
  118. }
  119. return self::TEST_WHERE_CONDITION;
  120. }
  121. }