Skip to content

Commit d8be7ed

Browse files
committed
MAGETWO-58893: [Backport] [GitHub] IndexerHandlerFactory: Indexer Object cast to String - 2.1
1 parent 2347bc6 commit d8be7ed

File tree

2 files changed

+178
-2
lines changed

2 files changed

+178
-2
lines changed

app/code/Magento/CatalogSearch/Model/Indexer/IndexerHandlerFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,17 @@ public function create(array $data = [])
7575
$indexer = $this->_objectManager->create($this->handlers[$currentHandler], $data);
7676

7777
if (!$indexer instanceof IndexerInterface) {
78-
throw new \InvalidArgumentException($indexer . ' doesn\'t implement \Magento\Framework\IndexerInterface');
78+
throw new \InvalidArgumentException(
79+
$currentHandler . ' indexer handler doesn\'t implement ' . IndexerInterface::class
80+
);
7981
}
8082

8183
if ($indexer && !$indexer->isAvailable()) {
8284
throw new \LogicException(
83-
'Indexer handler is not available: ' . $indexer
85+
'Indexer handler is not available: ' . $currentHandler
8486
);
8587
}
88+
8689
return $indexer;
8790
}
8891
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Test\Unit\Model\Indexer;
7+
8+
use Magento\CatalogSearch\Model\Indexer\IndexerHandlerFactory;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\Indexer\SaveHandler\IndexerInterface;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
14+
class IndexerHandlerFactoryTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/** @var IndexerHandlerFactory */
17+
protected $model;
18+
19+
/** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
20+
protected $objectManagerMock;
21+
22+
/** @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
23+
protected $scopeConfigMock;
24+
25+
protected function setUp()
26+
{
27+
$this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
28+
->getMockForAbstractClass();
29+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
30+
->getMockForAbstractClass();
31+
}
32+
33+
public function testCreate()
34+
{
35+
$configPath = 'config_path';
36+
$currentHandler = 'current_handler';
37+
$currentHandlerClass = 'current_handler_class';
38+
$handlers = [
39+
$currentHandler => $currentHandlerClass,
40+
];
41+
$data = ['data'];
42+
43+
$this->scopeConfigMock->expects($this->once())
44+
->method('getValue')
45+
->with($configPath, ScopeInterface::SCOPE_STORE)
46+
->willReturn($currentHandler);
47+
48+
$indexerMock = $this->getMockBuilder(IndexerInterface::class)
49+
->getMockForAbstractClass();
50+
51+
$this->objectManagerMock->expects($this->once())
52+
->method('create')
53+
->with($currentHandlerClass, $data)
54+
->willReturn($indexerMock);
55+
56+
$indexerMock->expects($this->once())
57+
->method('isAvailable')
58+
->willReturn(true);
59+
60+
$this->model = new IndexerHandlerFactory(
61+
$this->objectManagerMock,
62+
$this->scopeConfigMock,
63+
$configPath,
64+
$handlers
65+
);
66+
67+
$this->assertEquals($indexerMock, $this->model->create($data));
68+
}
69+
70+
/**
71+
* @expectedException \LogicException
72+
* @expectedExceptionMessage There is no such indexer handler: current_handler
73+
*/
74+
public function testCreateWithoutHandlers()
75+
{
76+
$configPath = 'config_path';
77+
$currentHandler = 'current_handler';
78+
$handlers = [];
79+
$data = ['data'];
80+
81+
$this->scopeConfigMock->expects($this->once())
82+
->method('getValue')
83+
->with($configPath, ScopeInterface::SCOPE_STORE)
84+
->willReturn($currentHandler);
85+
86+
$this->model = new IndexerHandlerFactory(
87+
$this->objectManagerMock,
88+
$this->scopeConfigMock,
89+
$configPath,
90+
$handlers
91+
);
92+
93+
$this->model->create($data);
94+
}
95+
96+
/**
97+
* @expectedException \InvalidArgumentException
98+
* @expectedExceptionMessage current_handler indexer handler doesn't implement
99+
*/
100+
public function testCreateWithWrongHandler()
101+
{
102+
$configPath = 'config_path';
103+
$currentHandler = 'current_handler';
104+
$currentHandlerClass = 'current_handler_class';
105+
$handlers = [
106+
$currentHandler => $currentHandlerClass,
107+
];
108+
$data = ['data'];
109+
110+
$this->scopeConfigMock->expects($this->once())
111+
->method('getValue')
112+
->with($configPath, ScopeInterface::SCOPE_STORE)
113+
->willReturn($currentHandler);
114+
115+
$indexerMock = $this->getMockBuilder(\stdClass::class)
116+
->getMockForAbstractClass();
117+
118+
$this->objectManagerMock->expects($this->once())
119+
->method('create')
120+
->with($currentHandlerClass, $data)
121+
->willReturn($indexerMock);
122+
123+
$this->model = new IndexerHandlerFactory(
124+
$this->objectManagerMock,
125+
$this->scopeConfigMock,
126+
$configPath,
127+
$handlers
128+
);
129+
130+
$this->model->create($data);
131+
}
132+
133+
/**
134+
* @expectedException \LogicException
135+
* @expectedExceptionMessage Indexer handler is not available: current_handler
136+
*/
137+
public function testCreateWithoutAvailableHandler()
138+
{
139+
$configPath = 'config_path';
140+
$currentHandler = 'current_handler';
141+
$currentHandlerClass = 'current_handler_class';
142+
$handlers = [
143+
$currentHandler => $currentHandlerClass,
144+
];
145+
$data = ['data'];
146+
147+
$this->scopeConfigMock->expects($this->once())
148+
->method('getValue')
149+
->with($configPath, ScopeInterface::SCOPE_STORE)
150+
->willReturn($currentHandler);
151+
152+
$indexerMock = $this->getMockBuilder(IndexerInterface::class)
153+
->getMockForAbstractClass();
154+
155+
$this->objectManagerMock->expects($this->once())
156+
->method('create')
157+
->with($currentHandlerClass, $data)
158+
->willReturn($indexerMock);
159+
160+
$indexerMock->expects($this->once())
161+
->method('isAvailable')
162+
->willReturn(false);
163+
164+
$this->model = new IndexerHandlerFactory(
165+
$this->objectManagerMock,
166+
$this->scopeConfigMock,
167+
$configPath,
168+
$handlers
169+
);
170+
171+
$this->model->create($data);
172+
}
173+
}

0 commit comments

Comments
 (0)