CollectionTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Test\Unit\Model\ResourceModel\Page;
  7. use Magento\Cms\Test\Unit\Model\ResourceModel\AbstractCollectionTest;
  8. use Magento\Framework\DataObject;
  9. class CollectionTest extends AbstractCollectionTest
  10. {
  11. /**
  12. * @var \Magento\Cms\Model\ResourceModel\Page\Collection
  13. */
  14. protected $collection;
  15. /**
  16. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $storeManagerMock;
  19. /**
  20. * @var \Magento\Framework\EntityManager\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $metadataPoolMock;
  23. protected function setUp()
  24. {
  25. parent::setUp();
  26. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  27. ->getMockForAbstractClass();
  28. $this->metadataPoolMock = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->collection = $this->objectManager->getObject(
  32. \Magento\Cms\Model\ResourceModel\Page\Collection::class,
  33. [
  34. 'resource' => $this->resource,
  35. 'connection' => $this->connection,
  36. 'storeManager' => $this->storeManagerMock,
  37. 'metadataPool' => $this->metadataPoolMock,
  38. ]
  39. );
  40. }
  41. public function testAddFieldToFilterStore()
  42. {
  43. $storeId = 1;
  44. $expectedFilter = new DataObject(
  45. [
  46. 'field' => 'store',
  47. 'value' => ['in' => [1]],
  48. 'type' => 'public'
  49. ]
  50. );
  51. $this->assertSame($this->collection, $this->collection->addFieldToFilter('store_id', $storeId));
  52. // addition call to make sure that correct value was set to filter
  53. $this->assertEquals($expectedFilter, $this->collection->getFilter('store'));
  54. }
  55. public function testAddFieldToFilter()
  56. {
  57. $field = 'title';
  58. $value = 'test_filter';
  59. $searchSql = 'sql query';
  60. $this->connection->expects($this->any())->method('quoteIdentifier')->willReturn($searchSql);
  61. $this->connection->expects($this->any())->method('prepareSqlCondition')->willReturn($searchSql);
  62. $this->select->expects($this->once())
  63. ->method('where')
  64. ->with($searchSql, null, \Magento\Framework\DB\Select::TYPE_CONDITION);
  65. $this->assertSame($this->collection, $this->collection->addFieldToFilter($field, $value));
  66. }
  67. /**
  68. * @param \Magento\Framework\DataObject $item
  69. * @param array $storesData
  70. * @dataProvider getItemsDataProvider
  71. * @throws \Exception
  72. */
  73. public function testAfterLoad($item, $storesData)
  74. {
  75. $linkField = 'row_id';
  76. $expectedResult = [];
  77. foreach ($storesData as $storeData) {
  78. $expectedResult[$storeData[$linkField]][] = $storeData['store_id'];
  79. }
  80. $entityMetadataMock = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadata::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $entityMetadataMock->expects($this->any())->method('getLinkField')->willReturn($linkField);
  84. $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($entityMetadataMock);
  85. $this->select->expects($this->any())->method('from')->willReturnSelf();
  86. $this->connection->expects($this->any())->method('fetchAll')->willReturn($storesData);
  87. $storeDataMock = $this->getMockBuilder(
  88. \Magento\Store\Api\Data\StoreInterface::class
  89. )->getMockForAbstractClass();
  90. $storeDataMock->expects($this->any())->method('getId')->willReturn(current($expectedResult[$item->getId()]));
  91. $storeDataMock->expects($this->any())->method('getCode')->willReturn('some_code');
  92. $this->storeManagerMock->expects($this->any())->method('getStores')->willReturn([$storeDataMock]);
  93. $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeDataMock);
  94. $this->collection->addItem($item);
  95. $this->assertEmpty($item->getStoreId());
  96. $this->collection->load();
  97. $this->assertEquals($expectedResult[$item->getId()], $item->getStoreId());
  98. }
  99. /**
  100. * @return array
  101. */
  102. public function getItemsDataProvider()
  103. {
  104. return [
  105. [
  106. new \Magento\Framework\DataObject(['id' => 1, 'row_id' => 1]),
  107. [
  108. ['row_id' => 1, 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID],
  109. ],
  110. ],
  111. [
  112. new \Magento\Framework\DataObject(['id' => 2, 'row_id' => 2]),
  113. [
  114. ['row_id' => 2, 'store_id' => 1],
  115. ['row_id' => 2, 'store_id' => 2],
  116. ],
  117. ],
  118. ];
  119. }
  120. }