UrlRewriteCollectionTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\UrlRewrite\Test\Unit\Model\ResourceModel;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class UrlRewriteCollectionTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $storeManager;
  14. /**
  15. * @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $resource;
  18. /**
  19. * @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $select;
  22. /**
  23. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $connectionMock;
  26. /**
  27. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $connection;
  30. /**
  31. * @var \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection
  32. */
  33. protected $collection;
  34. protected function setUp()
  35. {
  36. $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  37. $this->select = $this->createPartialMock(\Magento\Framework\DB\Select::class, ['from', 'where']);
  38. $this->connectionMock = $this->createPartialMock(
  39. \Magento\Framework\DB\Adapter\Pdo\Mysql::class,
  40. ['select', 'prepareSqlCondition', 'quoteIdentifier']
  41. );
  42. $this->resource = $this->getMockForAbstractClass(
  43. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  44. [],
  45. '',
  46. false,
  47. true,
  48. true,
  49. ['getConnection', '__wakeup', 'getMainTable', 'getTable']
  50. );
  51. $this->select->expects($this->any())
  52. ->method('where')
  53. ->will($this->returnSelf());
  54. $this->connectionMock->expects($this->any())
  55. ->method('select')
  56. ->will($this->returnValue($this->select));
  57. $this->connectionMock->expects($this->any())
  58. ->method('quoteIdentifier')
  59. ->will($this->returnArgument(0));
  60. $this->resource->expects($this->any())
  61. ->method('getConnection')
  62. ->will($this->returnValue($this->connectionMock));
  63. $this->resource->expects($this->any())
  64. ->method('getMainTable')
  65. ->will($this->returnValue('test_main_table'));
  66. $this->resource->expects($this->any())
  67. ->method('getTable')
  68. ->with('test_main_table')
  69. ->will($this->returnValue('test_main_table'));
  70. $this->collection = (new ObjectManager($this))->getObject(
  71. \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection::class,
  72. [
  73. 'storeManager' => $this->storeManager,
  74. 'resource' => $this->resource,
  75. ]
  76. );
  77. }
  78. /**
  79. * @param array $storeId
  80. * @param bool $withAdmin
  81. * @param array $condition
  82. * @dataProvider dataProviderForTestAddStoreIfStoreIsArray
  83. * @covers \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection
  84. */
  85. public function testAddStoreFilterIfStoreIsArray($storeId, $withAdmin, $condition)
  86. {
  87. $this->connectionMock->expects($this->once())
  88. ->method('prepareSqlCondition')
  89. ->with('store_id', ['in' => $condition]);
  90. $this->collection->addStoreFilter($storeId, $withAdmin);
  91. }
  92. /**
  93. * @return array
  94. */
  95. public function dataProviderForTestAddStoreIfStoreIsArray()
  96. {
  97. return [
  98. [[112, 113], false, [112, 113]],
  99. [[112, 113], true, [112, 113, 0]],
  100. ];
  101. }
  102. /**
  103. * @param int $storeId
  104. * @param bool $withAdmin
  105. * @param array $condition
  106. * @dataProvider dataProviderForTestAddStoreFilterIfStoreIsInt
  107. * @covers \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection
  108. */
  109. public function testAddStoreFilterIfStoreIsInt($storeId, $withAdmin, $condition)
  110. {
  111. $store = $this->createMock(\Magento\Store\Model\Store::class);
  112. $store->expects($this->once())->method('getId')->will($this->returnValue($storeId));
  113. $this->storeManager->expects($this->once())->method('getStore')->will($this->returnValue($store));
  114. $this->connectionMock->expects($this->once())
  115. ->method('prepareSqlCondition')
  116. ->with('store_id', ['in' => $condition]);
  117. $this->collection->addStoreFilter($storeId, $withAdmin);
  118. }
  119. /**
  120. * @return array
  121. */
  122. public function dataProviderForTestAddStoreFilterIfStoreIsInt()
  123. {
  124. return [
  125. [112, false, [112]],
  126. [112, true, [112, 0]],
  127. ];
  128. }
  129. }