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

Commit

Permalink
Merge pull request #167 from kokspflanze/bugfix/pdoexception_code_ret…
Browse files Browse the repository at this point in the history
…urn_string

bugfix PDOException return string as code
  • Loading branch information
weierophinney committed Dec 19, 2016
2 parents b6b646b + 63b0109 commit 36f38ff
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
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
18 changes: 18 additions & 0 deletions test/TestAsset/ExceptionWithStringAsCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-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';
}
24 changes: 24 additions & 0 deletions test/TestAsset/FailingExceptionWithStringAsCodeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-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 36f38ff

Please sign in to comment.