Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 8a60789

Browse files
committed
Merge branch 'hotfix/167'
Close #167
2 parents b6b646b + 1f7791f commit 8a60789

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 3.1.2 - TBD
5+
## 3.1.2 - 2016-12-19
66

77
### Added
88

@@ -18,7 +18,12 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#167](https://github.com/zendframework/zend-servicemanager/pull/167) fixes
22+
how exception codes are provided to ServiceNotCreatedException. Previously,
23+
the code was provided as-is. However, some PHP internal exception classes,
24+
notably PDOException, can sometimes return other values (such as strings),
25+
which can lead to fatal errors when instantiating the new exception. The
26+
patch provided casts exception codes to integers to prevent these errors.
2227

2328
## 3.1.1 - 2016-07-15
2429

src/ServiceManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ private function doCreate($resolvedName, array $options = null)
755755
'Service with name "%s" could not be created. Reason: %s',
756756
$resolvedName,
757757
$exception->getMessage()
758-
), $exception->getCode(), $exception);
758+
), (int) $exception->getCode(), $exception);
759759
}
760760

761761
foreach ($this->initializers as $initializer) {

test/CommonServiceLocatorBehaviorsTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Zend\ServiceManager\ServiceLocatorInterface;
2424
use ZendTest\ServiceManager\TestAsset\FailingAbstractFactory;
2525
use ZendTest\ServiceManager\TestAsset\FailingFactory;
26+
use ZendTest\ServiceManager\TestAsset\FailingExceptionWithStringAsCodeFactory;
2627
use ZendTest\ServiceManager\TestAsset\InvokableObject;
2728
use ZendTest\ServiceManager\TestAsset\SimpleAbstractFactory;
2829

@@ -227,6 +228,19 @@ public function testThrowExceptionIfServiceCannotBeCreated()
227228
$serviceManager->get(stdClass::class);
228229
}
229230

231+
public function testThrowExceptionWithStringAsCodeIfServiceCannotBeCreated()
232+
{
233+
$serviceManager = $this->createContainer([
234+
'factories' => [
235+
stdClass::class => FailingExceptionWithStringAsCodeFactory::class
236+
]
237+
]);
238+
239+
$this->setExpectedException(ServiceNotCreatedException::class);
240+
241+
$serviceManager->get(stdClass::class);
242+
}
243+
230244
public function testConfigureCanAddNewServices()
231245
{
232246
$serviceManager = $this->createContainer([
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace ZendTest\ServiceManager\TestAsset;
9+
10+
use Exception;
11+
12+
class ExceptionWithStringAsCode extends Exception
13+
{
14+
/** @var string */
15+
protected $code = 'ExceptionString';
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace ZendTest\ServiceManager\TestAsset;
9+
10+
use Interop\Container\ContainerInterface;
11+
use Zend\ServiceManager\Factory\FactoryInterface;
12+
13+
class FailingExceptionWithStringAsCodeFactory implements FactoryInterface
14+
{
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
19+
{
20+
throw (new ExceptionWithStringAsCode('There is an error'));
21+
}
22+
}

0 commit comments

Comments
 (0)