Skip to content

Commit d05317c

Browse files
committed
#15895: Handle type errors for objects creation
- Updated test
1 parent d44148c commit d05317c

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

dev/tests/integration/testsuite/Magento/Framework/ObjectManager/ObjectManagerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
namespace Magento\Framework\ObjectManager;
77

8-
use TypeError;
9-
108
class ObjectManagerTest extends \PHPUnit\Framework\TestCase
119
{
1210
/**#@+
@@ -147,13 +145,15 @@ public function testNewInstance($actualClassName, array $properties = [], $expec
147145
}
148146

149147
/**
150-
* Test create instance with TypeError
148+
* Test creating an object and passing incorrect type of arguments to the constructor.
151149
*
152-
* @expectedException TypeError
150+
* @expectedException \Magento\Framework\Exception\RuntimeException
151+
* @expectedExceptionMessage Error occurred when creating object
153152
*/
154153
public function testNewInstanceWithTypeError()
155154
{
156-
self::$_objectManager->create(self::TEST_CLASS_WITH_TYPE_ERROR);
157-
$this->fail('No instance for class with TypeError should be created');
155+
self::$_objectManager->create(self::TEST_CLASS_WITH_TYPE_ERROR, [
156+
'testArgument' => new \stdClass()
157+
]);
158158
}
159159
}

dev/tests/integration/testsuite/Magento/Framework/ObjectManager/TestAsset/ConstructorWithTypeError.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@
66

77
namespace Magento\Framework\ObjectManager\TestAsset;
88

9+
/**
10+
* Test asset used to test invalid argument types on the constructor invocation.
11+
*/
912
class ConstructorWithTypeError
1013
{
11-
public function __construct()
14+
/**
15+
* @var Basic
16+
*/
17+
private $testArgument;
18+
19+
/**
20+
* @param Basic $testArgument
21+
*/
22+
public function __construct(Basic $testArgument)
1223
{
13-
// set non-exists property to trigger TypeError
14-
throw new \TypeError('test error');
24+
$this->testArgument = $testArgument;
1525
}
1626
}

lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ protected function createObject($type, $args)
114114
{
115115
try {
116116
return new $type(...array_values($args));
117-
} catch (\TypeError $e) {
117+
} catch (\TypeError $exception) {
118118
/** @var LoggerInterface $logger */
119119
$logger = ObjectManager::getInstance()->get(LoggerInterface::class);
120120
$logger->critical(
121-
sprintf('Create object error: %s, %s', $type, $e->getMessage())
121+
sprintf('Type Error occurred when creating object: %s, %s', $type, $exception->getMessage())
122122
);
123123

124124
throw new RuntimeException(
125-
new Phrase('Create object error: '.$e->getMessage())
125+
new Phrase('Type Error occurred when creating object: %type', ['type' => $type])
126126
);
127127
}
128128
}

0 commit comments

Comments
 (0)