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

Commit e722159

Browse files
committed
Merge branch 'hotfix/167' into develop
Forward port #167 Conflicts: CHANGELOG.md
2 parents 4720001 + 1f7791f commit e722159

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
@@ -51,7 +51,7 @@ All notable changes to this project will be documented in this file, in reverse
5151
factory, using a previous instance when detected. You may still use multiple
5252
discrete instances, however.
5353

54-
## 3.1.2 - TBD
54+
## 3.1.2 - 2016-12-19
5555

5656
### Added
5757

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

6868
### Fixed
6969

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

7277
## 3.1.1 - 2016-07-15
7378

src/ServiceManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ private function doCreate($resolvedName, array $options = null)
766766
'Service with name "%s" could not be created. Reason: %s',
767767
$resolvedName,
768768
$exception->getMessage()
769-
), $exception->getCode(), $exception);
769+
), (int) $exception->getCode(), $exception);
770770
}
771771

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

test/CommonServiceLocatorBehaviorsTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use ZendTest\ServiceManager\TestAsset\CallTimesAbstractFactory;
2323
use ZendTest\ServiceManager\TestAsset\FailingAbstractFactory;
2424
use ZendTest\ServiceManager\TestAsset\FailingFactory;
25+
use ZendTest\ServiceManager\TestAsset\FailingExceptionWithStringAsCodeFactory;
2526
use ZendTest\ServiceManager\TestAsset\InvokableObject;
2627
use ZendTest\ServiceManager\TestAsset\SimpleAbstractFactory;
2728

@@ -262,6 +263,19 @@ public function testThrowExceptionIfServiceCannotBeCreated()
262263
$serviceManager->get(stdClass::class);
263264
}
264265

266+
public function testThrowExceptionWithStringAsCodeIfServiceCannotBeCreated()
267+
{
268+
$serviceManager = $this->createContainer([
269+
'factories' => [
270+
stdClass::class => FailingExceptionWithStringAsCodeFactory::class
271+
]
272+
]);
273+
274+
$this->setExpectedException(ServiceNotCreatedException::class);
275+
276+
$serviceManager->get(stdClass::class);
277+
}
278+
265279
public function testConfigureCanAddNewServices()
266280
{
267281
$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)