Skip to content

Commit ef57302

Browse files
author
Mykola Palamar
committed
MAGETWO-66100: Remove usages of unserialize in module Magento/Setup
1 parent 3311cf4 commit ef57302

File tree

2 files changed

+55
-67
lines changed

2 files changed

+55
-67
lines changed

lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use Magento\Framework\ObjectManager\ConfigInterface;
99
use Magento\Framework\Serialize\SerializerInterface;
10-
use Magento\Framework\Serialize\Serializer\Serialize;
1110
use Magento\Framework\ObjectManager\ConfigCacheInterface;
1211
use Magento\Framework\ObjectManager\RelationsInterface;
1312

@@ -171,19 +170,4 @@ public function getPreferences()
171170
{
172171
return $this->preferences;
173172
}
174-
175-
/**
176-
* Get serializer
177-
*
178-
* @return SerializerInterface
179-
* @deprecated
180-
*/
181-
private function getSerializer()
182-
{
183-
if (null === $this->serializer) {
184-
$this->serializer = \Magento\Framework\App\ObjectManager::getInstance()
185-
->get(Serialize::class);
186-
}
187-
return $this->serializer;
188-
}
189173
}

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/CompiledTest.php

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use Magento\Framework\ObjectManager\Config\Compiled;
99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManager;
10-
use Magento\Framework\Serialize\SerializerInterface;
1110

1211
class CompiledTest extends \PHPUnit_Framework_TestCase
1312
{
@@ -16,11 +15,6 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
1615
*/
1716
private $objectManager;
1817

19-
/**
20-
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
21-
*/
22-
private $serializerMock;
23-
2418
/**
2519
* @var \Magento\Framework\ObjectManager\Config\Compiled
2620
*/
@@ -29,15 +23,14 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
2923
protected function setUp()
3024
{
3125
$this->objectManager = new ObjectManager($this);
32-
$this->serializerMock = $this->getMock(SerializerInterface::class);
3326

3427
$initialData = [
3528
'arguments' => [
3629
'type1' => 'initial serialized configuration for type1',
3730
'class_with_no_arguments_serialized' => null,
38-
'class_with_arguments_serialized' => 'serialized arguments',
39-
'class_with_arguments_unserialized' => ['unserialized', 'arguments'],
40-
'class_with_no_arguments_unserialized' => [],
31+
'class_with_arguments_string' => 'string arguments',
32+
'class_with_arguments_array' => ['unserialized', 'arguments'],
33+
'class_with_no_arguments_empty_array' => [],
4134
],
4235
'instanceTypes' => [
4336
'instanceType1' => 'instanceTypeValue1',
@@ -53,59 +46,79 @@ protected function setUp()
5346
Compiled::class,
5447
[
5548
'data' => $initialData,
56-
'serializer' => $this->serializerMock
5749
]
5850
);
5951
}
6052

61-
public function testExtend()
53+
/**
54+
* Test is it possible extend/overwrite arguments for the DI.
55+
*
56+
*/
57+
public function testExtendArguments()
6258
{
63-
6459
$configuration = [
6560
'arguments' => [
66-
'type1' => 'serialized configuration for type1',
67-
'type2' => 'serialized configuration for type2'
61+
'type1' => 'configuration for type1',
62+
'type2' => [
63+
'argument2_1' => 'newArgumentValue2_1',
64+
]
6865
],
6966
'instanceTypes' => [
7067
'instanceType2' => 'newInstanceTypeValue2',
71-
'instanceType3' => 'newInstanceTypeValue3'
68+
'instanceType3' => 'newInstanceTypeValue3',
7269
],
7370
'preferences' => [
74-
'preference1' => 'newPreferenceValue1'
75-
]
71+
'preference1' => 'newPreferenceValue1',
72+
],
7673
];
7774
$expectedArguments = [
78-
'type1' => [
79-
'argument1_1' => 'newArgumentValue1_1'
80-
],
75+
'type1' => 'configuration for type1',
8176
'type2' => [
82-
'argument2_1' => 'newArgumentValue2_1'
77+
'argument2_1' => 'newArgumentValue2_1',
8378
]
8479
];
85-
$expectedVirtualTypes = [
80+
81+
$this->compiled->extend($configuration);
82+
foreach ($expectedArguments as $type => $arguments) {
83+
$this->assertEquals($arguments, $this->compiled->getArguments($type));
84+
}
85+
}
86+
87+
/**
88+
* Test getting virtual types from the DI.
89+
*/
90+
public function testVirtualTypes()
91+
{
92+
$configuration = [
93+
'instanceTypes' => [
94+
'instanceType2' => 'newInstanceTypeValue2',
95+
'instanceType3' => 'newInstanceTypeValue3'
96+
],
97+
];
98+
$expectedTypes = [
8699
'instanceType1' => 'instanceTypeValue1',
87100
'instanceType2' => 'newInstanceTypeValue2',
88101
'instanceType3' => 'newInstanceTypeValue3'
89102
];
103+
$this->compiled->extend($configuration);
104+
$this->assertEquals($expectedTypes, $this->compiled->getVirtualTypes());
105+
}
106+
107+
/**
108+
* Test getting preferences from the DI.
109+
*/
110+
public function testPreferences()
111+
{
112+
$configuration = [
113+
'preferences' => [
114+
'preference1' => 'newPreferenceValue1'
115+
]
116+
];
90117
$expectedPreferences = [
91118
'preference1' => 'newPreferenceValue1',
92119
'preference2' => 'preferenceValue2'
93120
];
94-
95-
$this->serializerMock->expects($this->at(0))
96-
->method('unserialize')
97-
->with($configuration['arguments']['type1'])
98-
->willReturn($expectedArguments['type1']);
99-
$this->serializerMock->expects($this->at(1))
100-
->method('unserialize')
101-
->with($configuration['arguments']['type2'])
102-
->willReturn($expectedArguments['type2']);
103-
104121
$this->compiled->extend($configuration);
105-
foreach ($expectedArguments as $type => $arguments) {
106-
$this->assertEquals($arguments, $this->compiled->getArguments($type));
107-
}
108-
$this->assertEquals($expectedVirtualTypes, $this->compiled->getVirtualTypes());
109122
$this->assertEquals($expectedPreferences, $this->compiled->getPreferences());
110123
}
111124

@@ -114,23 +127,17 @@ public function testExtend()
114127
*/
115128
public function testGetArgumentsSerialized()
116129
{
117-
$unserializedArguments = ['unserialized', 'arguments'];
118-
119-
// method called twice but after one unserialization, unserialized version should be stored
120-
$this->serializerMock->expects($this->once())->method('unserialize')
121-
->with('serialized arguments')
122-
->willReturn($unserializedArguments);
130+
$unserializedArguments = 'string arguments';
123131

124-
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_serialized'));
125-
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_serialized'));
132+
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_string'));
133+
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_string'));
126134
}
127135

128136
/**
129137
* Arguments defined in array, have not previously been unserialized
130138
*/
131139
public function testGetArgumentsSerializedEmpty()
132140
{
133-
$this->serializerMock->expects($this->never())->method('unserialize');
134141
$this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_serialized'));
135142
}
136143

@@ -140,25 +147,22 @@ public function testGetArgumentsSerializedEmpty()
140147
public function testGetArgumentsUnserialized()
141148
{
142149
$unserializedArguments = ['unserialized', 'arguments'];
143-
$this->serializerMock->expects($this->never())->method('unserialize');
144-
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_unserialized'));
150+
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_array'));
145151
}
146152

147153
/**
148154
* Arguments are defined but empty
149155
*/
150156
public function testGetArgumentsUnserializedEmpty()
151157
{
152-
$this->serializerMock->expects($this->never())->method('unserialize');
153-
$this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_unserialized'));
158+
$this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_empty_array'));
154159
}
155160

156161
/**
157162
* Arguments not defined in array
158163
*/
159164
public function testGetArgumentsNotDefined()
160165
{
161-
$this->serializerMock->expects($this->never())->method('unserialize');
162166
$this->assertSame(null, $this->compiled->getArguments('class_not_stored_in_config'));
163167
}
164168
}

0 commit comments

Comments
 (0)