Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StoreViewServiceTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class StoreViewServiceTest extends \PHPUnit\Framework\TestCase
11 {
13  protected $storeViewService;
14 
16  protected $config;
17 
19  protected $resource;
20 
22  protected $connection;
23 
25  protected $select;
26 
27  protected function setUp()
28  {
29  $this->config = $this->createMock(\Magento\Eav\Model\Config::class);
30  $this->select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
31  ->setMethods(['select', 'from', 'where', 'join'])
32  ->disableOriginalConstructor()
33  ->getMock();
34  $this->connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
35  ->disableOriginalConstructor()
36  ->getMockForAbstractClass();
37  $this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
38  $this->resource->expects($this->any())->method('getConnection')->willReturn($this->connection);
39 
40  $this->storeViewService = (new ObjectManager($this))->getObject(
41  \Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class,
42  [
43  'eavConfig' => $this->config,
44  'resource' => $this->resource,
45  ]
46  );
47  }
48 
53  {
54  return [
55  [1, 1, 1, true],
56  [1, 2, 1, false],
57  [2, 0, 1, false],
58  ];
59  }
60 
65  {
66  return [
67  [1, [1, 2], true],
68  [1, [2, 3], false],
69  [1, [], false],
70  ];
71  }
72 
79  public function testDoesEntityHaveOverriddenUrlKeyForStore($storeId, $fetchedStoreIds, $result)
80  {
81  $entityType = 'entity_type';
82  $productId = 'product_id';
83  $attribute = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
84  ->disableOriginalConstructor()
85  ->setMethods(['__wakeup', 'getBackendTable', 'getId', 'getEntity'])
86  ->getMockForAbstractClass();
87  $this->config->expects($this->once())->method('getAttribute')->with($entityType, 'url_key')
88  ->will($this->returnValue($attribute));
89  $entity = $this->getMockBuilder(\Magento\Eav\Model\Entity\AbstractEntity::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $attribute->expects($this->exactly(2))->method('getEntity')->willReturn($entity);
93  $entity->expects($this->once())->method('getEntityTable')->will($this->returnValue('entity_table'));
94  $entity->expects($this->once())->method('getLinkField')->willReturn('link_field');
95  $attribute->expects($this->once())->method('getBackendTable')->will($this->returnValue('backend_table'));
96  $attribute->expects($this->once())->method('getId')->will($this->returnValue('attribute-id'));
97  $this->select->expects($this->once())->method('from')->with(['e' => 'entity_table'], [])
98  ->will($this->returnSelf());
99  $this->select->expects($this->any())->method('where')->will($this->returnSelf());
100  $this->select->expects($this->once())->method('join')->with(
101  ['e_attr' => 'backend_table'],
102  "e.link_field = e_attr.link_field",
103  'store_id'
104  )->will($this->returnSelf());
105  $this->connection->expects($this->once())->method('select')->will($this->returnValue($this->select));
106  $this->connection->expects($this->once())->method('fetchCol')->will($this->returnValue($fetchedStoreIds));
107 
108  $this->assertEquals(
109  $result,
110  $this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($storeId, $productId, $entityType)
111  );
112  }
113 
119  {
120  $invalidEntityType = 'invalid_type';
121  $this->config->expects($this->once())->method('getAttribute')->will($this->returnValue(false));
122 
123  $this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore(1, 1, $invalidEntityType);
124  }
125 }
$entity
Definition: element.phtml:22