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

Commit

Permalink
Merge branch 'hotfix/167'
Browse files Browse the repository at this point in the history
Close #167
  • Loading branch information
weierophinney committed Dec 19, 2016
2 parents b6b646b + 1f7791f commit 8a60789
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## 3.1.2 - TBD
## 3.1.2 - 2016-12-19

### Added

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

### Fixed

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

## 3.1.1 - 2016-07-15

Expand Down
2 changes: 1 addition & 1 deletion src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ private function doCreate($resolvedName, array $options = null)
'Service with name "%s" could not be created. Reason: %s',
$resolvedName,
$exception->getMessage()
), $exception->getCode(), $exception);
), (int) $exception->getCode(), $exception);
}

foreach ($this->initializers as $initializer) {
Expand Down
14 changes: 14 additions & 0 deletions test/CommonServiceLocatorBehaviorsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Zend\ServiceManager\ServiceLocatorInterface;
use ZendTest\ServiceManager\TestAsset\FailingAbstractFactory;
use ZendTest\ServiceManager\TestAsset\FailingFactory;
use ZendTest\ServiceManager\TestAsset\FailingExceptionWithStringAsCodeFactory;
use ZendTest\ServiceManager\TestAsset\InvokableObject;
use ZendTest\ServiceManager\TestAsset\SimpleAbstractFactory;

Expand Down Expand Up @@ -227,6 +228,19 @@ public function testThrowExceptionIfServiceCannotBeCreated()
$serviceManager->get(stdClass::class);
}

public function testThrowExceptionWithStringAsCodeIfServiceCannotBeCreated()
{
$serviceManager = $this->createContainer([
'factories' => [
stdClass::class => FailingExceptionWithStringAsCodeFactory::class
]
]);

$this->setExpectedException(ServiceNotCreatedException::class);

$serviceManager->get(stdClass::class);
}

public function testConfigureCanAddNewServices()
{
$serviceManager = $this->createContainer([
Expand Down
16 changes: 16 additions & 0 deletions test/TestAsset/ExceptionWithStringAsCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\ServiceManager\TestAsset;

use Exception;

class ExceptionWithStringAsCode extends Exception
{
/** @var string */
protected $code = 'ExceptionString';
}
22 changes: 22 additions & 0 deletions test/TestAsset/FailingExceptionWithStringAsCodeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\ServiceManager\TestAsset;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class FailingExceptionWithStringAsCodeFactory implements FactoryInterface
{
/**
* {@inheritDoc}
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
throw (new ExceptionWithStringAsCode('There is an error'));
}
}

0 comments on commit 8a60789

Please sign in to comment.