1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Test\Unit\Model;
use Magento\Setup\Model\UninstallCollector;
class UninstallCollectorTest extends \PHPUnit\Framework\TestCase
{
/**
* @var UninstallCollector
*/
private $collector;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Adapter\AdapterInterface
*/
private $adapterInterface;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Select
*/
private $result;
public function setUp()
{
$objectManagerProvider = $this->createMock(\Magento\Setup\Model\ObjectManagerProvider::class);
$objectManager =
$this->getMockForAbstractClass(\Magento\Framework\ObjectManagerInterface::class, [], '', false);
$objectManagerProvider->expects($this->once())->method('get')->willReturn($objectManager);
$setup = $this->createMock(\Magento\Setup\Module\DataSetup::class);
$this->adapterInterface = $this->getMockForAbstractClass(
\Magento\Framework\DB\Adapter\AdapterInterface::class,
[],
'',
false
);
$select = $this->createPartialMock(\Magento\Framework\DB\Select::class, ['from']);
$this->adapterInterface->expects($this->once())->method('select')->willReturn($select);
$setup->expects($this->exactly(2))->method('getConnection')->willReturn($this->adapterInterface);
$this->result = $this->createMock(\Magento\Framework\DB\Select::class);
$select->expects($this->once())->method('from')->willReturn($this->result);
$uninstallA = 'Uninstall Class A';
$uninstallB = 'Uninstall Class B';
$objectManager->expects($this->any())
->method('create')
->will(
$this->returnValueMap([
['Magento\A\Setup\Uninstall', [], $uninstallA],
['Magento\B\Setup\Uninstall', [], $uninstallB],
])
);
$setupFactory = $this->createMock(\Magento\Setup\Module\DataSetupFactory::class);
$setupFactory->expects($this->once())->method('create')->willReturn($setup);
$this->collector = new UninstallCollector($objectManagerProvider, $setupFactory);
}
public function testUninstallCollector()
{
$this->result->expects($this->never())->method('where');
$this->adapterInterface->expects($this->once())
->method('fetchAll')
->with($this->result)
->willReturn([['module' => 'Magento_A'], ['module' => 'Magento_B'], ['module' => 'Magento_C']]);
$this->assertEquals(
['Magento_A' => 'Uninstall Class A', 'Magento_B' => 'Uninstall Class B'],
$this->collector->collectUninstall()
);
}
public function testUninstallCollectorWithInput()
{
$this->result->expects($this->once())->method('where')->willReturn($this->result);
$this->adapterInterface->expects($this->once())
->method('fetchAll')
->with($this->result)
->willReturn([['module' => 'Magento_A']]);
$this->assertEquals(['Magento_A' => 'Uninstall Class A'], $this->collector->collectUninstall(['Magento_A']));
}
}
namespace Magento\Setup\Model;
/**
* This function overrides the native function for the purpose of testing
*
* @param string $obj
* @param string $className
* @return bool
*/
function is_subclass_of($obj, $className)
{
if ($obj == 'Uninstall Class A' && $className == \Magento\Framework\Setup\UninstallInterface::class) {
return true;
}
if ($obj == 'Uninstall Class B' && $className == \Magento\Framework\Setup\UninstallInterface::class) {
return true;
}
return false;
}
/**
* This function overrides the native function for the purpose of testing
*
* @param string $className
* @return bool
*/
function class_exists($className)
{
if ($className == 'Magento\A\Setup\Uninstall' || $className == 'Magento\B\Setup\Uninstall') {
return true;
}
return false;
}