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

Complete forwards compatibility migration #16

Merged
merged 6 commits into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,34 @@ cache:
directories:
- $HOME/.composer/cache

env:
global:
- CODE_VERSION="^3.0"

matrix:
fast_finish: true
include:
- php: 5.5
env:
- EXECUTE_CS_CHECK=true
- php: 5.5
env:
- CODE_VERSION="^2.6"
- php: 5.6
env:
- EXECUTE_TEST_COVERALLS=true
- php: 5.6
env:
- CODE_VERSION="^2.6"
- php: 7
- php: hhvm
allow_failures:
- php: 7
env:
- CODE_VERSION="^2.6"
- php: hhvm
- php: hhvm
env:
- CODE_VERSION="^2.6"
allow_failures:
- php: hhvm

notifications:
Expand All @@ -34,6 +49,7 @@ before_install:
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
- composer self-update
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls ; fi
- composer require --no-update "zendframework/zend-code:$CODE_VERSION"

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs
Expand Down
20 changes: 5 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,21 @@
}
},
"require": {
"php": ">=5.5",
"zendframework/zend-code": "~2.5",
"zendframework/zend-stdlib": "dev-develop as 2.8.0"
"php": "^5.5 || ^7.0",
"container-interop/container-interop": "^1.1",
"zendframework/zend-code": "^2.6 || ^3.0",
"zendframework/zend-stdlib": "^2.7 || ^3.0"
},
"require-dev": {
"zendframework/zend-config": "dev-develop as 2.6.0",
"zendframework/zend-db": "dev-develop as 2.7.0",
"zendframework/zend-filter": "~2.5",
"zendframework/zend-form": "~2.5",
"zendframework/zend-log": "~2.5",
"zendframework/zend-mvc": "dev-develop as 2.7.0",
"zendframework/zend-servicemanager": "dev-develop as 2.7.0",
"zendframework/zend-view": "dev-develop as 2.6.0",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
},
"suggest": {
"zendframework/zend-servicemanager": "Zend\\ServiceManager component"
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "2.5-dev",
"dev-develop": "3.0-dev"
"dev-develop": "2.6-dev"
}
},
"autoload-dev": {
Expand Down
24 changes: 21 additions & 3 deletions src/Di.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace Zend\Di;

use Closure;
use Interop\Container\Exception\ContainerException;
use Zend\Di\Exception\RuntimeException as DiRuntimeException;
use Zend\ServiceManager\Exception\ExceptionInterface as ServiceManagerException;

/**
* Dependency injector that can generate instances using class definitions and configured instance parameters
Expand Down Expand Up @@ -212,6 +212,24 @@ protected function getCallParameters($name, array $params, $method = "__construc
return $params;
}

/**
* Is the DI container capable of returning the named instance?
*
* @param string $name
* @return bool
*/
public function has($name)
{
$definitions = $this->definitions;
$instanceManager = $this->instanceManager();

$class = $instanceManager->hasAlias($name)
? $instanceManager->getClassFromAlias($name)
: $name;

return $definitions->hasClass($class);
}

/**
* Lazy-load a class
*
Expand Down Expand Up @@ -831,8 +849,8 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
}
return false;
}
} catch (ServiceManagerException $e) {
// Zend\ServiceManager\Exception\ServiceNotCreatedException
} catch (ContainerException $e) {
// Exceptions thrown by nested/peered containers (e.g., zend-servicemanager)
if ($methodRequirementType & self::RESOLVE_STRICT) {
//finally ( be aware to do at the end of flow)
array_pop($this->currentDependencies);
Expand Down
12 changes: 3 additions & 9 deletions src/LocatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@

namespace Zend\Di;

interface LocatorInterface
use Interop\Container\ContainerInterface;

interface LocatorInterface extends ContainerInterface
{
/**
* Retrieve a class instance
*
* @param string $name Class name or service name
* @param null|array $params Parameters to be used when instantiating a new instance of $name
* @return object|null
*/
public function get($name, array $params = []);
}
17 changes: 17 additions & 0 deletions src/ServiceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ public function set($name, $service)
return $this;
}

/**
* Can the locator return the named instance?
*
* @param string $name
* @return bool
*/
public function has($name)
{
if (! isset($this->services[$name])
&& ! isset($this->map[$name])
) {
return false;
}

return true;
}

/**
* Retrieve a registered service
*
Expand Down
36 changes: 20 additions & 16 deletions test/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

use Zend\Di\Config;
use Zend\Di\Di;
use Zend\Config\Factory as ConfigFactory;
use PHPUnit_Framework_TestCase as TestCase;

class ConfigTest extends TestCase
{
public function testConfigCanConfigureInstanceManagerWithIniFile()
{
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-a');
$config = new Config($ini->di);
$ini = include __DIR__ . '/_files/sample-definitions.php';
$ini = $ini['section-a'];
$config = new Config($ini['di']);
$di = new Di();
$di->configure($config);

Expand Down Expand Up @@ -52,8 +52,9 @@ public function testConfigCanConfigureInstanceManagerWithIniFile()
public function testConfigCanConfigureBuilderDefinitionFromIni()
{
$this->markTestIncomplete('Builder not updated to new DI yet');
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-b');
$config = new Config($ini->di);
$ini = include __DIR__ . '/_files/sample-definitions.php';
$ini = $ini['section-b'];
$config = new Config($ini['di']);
$di = new Di($config);
$definition = $di->getDefinition();

Expand All @@ -62,27 +63,28 @@ public function testConfigCanConfigureBuilderDefinitionFromIni()
$this->assertEquals(
['username' => null, 'password' => null],
$definition->getInjectionMethodParameters('My\DbAdapter', '__construct')
);
);

$this->assertTrue($definition->hasClass('My\Mapper'));
$this->assertEquals('__construct', $definition->getInstantiator('My\Mapper'));
$this->assertEquals(
['dbAdapter' => 'My\DbAdapter'],
$definition->getInjectionMethodParameters('My\Mapper', '__construct')
);
);

$this->assertTrue($definition->hasClass('My\Repository'));
$this->assertEquals('__construct', $definition->getInstantiator('My\Repository'));
$this->assertEquals(
['mapper' => 'My\Mapper'],
$definition->getInjectionMethodParameters('My\Repository', '__construct')
);
);
}

public function testConfigCanConfigureRuntimeDefinitionDefaultFromIni()
{
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-c');
$config = new Config($ini->di);
$ini = include __DIR__ . '/_files/sample-definitions.php';
$ini = $ini['section-c'];
$config = new Config($ini['di']);
$di = new Di();
$di->configure($config);
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\RuntimeDefinition');
Expand All @@ -92,8 +94,9 @@ public function testConfigCanConfigureRuntimeDefinitionDefaultFromIni()

public function testConfigCanConfigureRuntimeDefinitionDisabledFromIni()
{
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-d');
$config = new Config($ini->di);
$ini = include __DIR__ . '/_files/sample-definitions.php';
$ini = $ini['section-d'];
$config = new Config($ini['di']);
$di = new Di();
$di->configure($config);
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\RuntimeDefinition');
Expand All @@ -102,8 +105,9 @@ public function testConfigCanConfigureRuntimeDefinitionDisabledFromIni()

public function testConfigCanConfigureRuntimeDefinitionUseAnnotationFromIni()
{
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-e');
$config = new Config($ini->di);
$ini = include __DIR__ . '/_files/sample-definitions.php';
$ini = $ini['section-e'];
$config = new Config($ini['di']);
$di = new Di();
$di->configure($config);
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\RuntimeDefinition');
Expand All @@ -112,8 +116,8 @@ public function testConfigCanConfigureRuntimeDefinitionUseAnnotationFromIni()

public function testConfigCanConfigureCompiledDefinition()
{
$config = ConfigFactory::fromFile(__DIR__ . '/_files/sample.php', true);
$config = new Config($config->di);
$config = include __DIR__ . '/_files/sample.php';
$config = new Config($config['di']);
$di = new Di();
$di->configure($config);
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\ArrayDefinition');
Expand Down
3 changes: 1 addition & 2 deletions test/Definition/BuilderDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Zend\Di\Definition\BuilderDefinition;
use Zend\Di\Definition\Builder;
use Zend\Config\Factory as ConfigFactory;
use PHPUnit_Framework_TestCase as TestCase;

class BuilderDefinitionTest extends TestCase
Expand Down Expand Up @@ -73,7 +72,7 @@ public function testBuilderDefinitionHasMethods()

public function testBuilderCanBuildFromArray()
{
$ini = ConfigFactory::fromFile(__DIR__ . '/../_files/sample.ini');
$ini = include __DIR__ . '/../_files/sample-definitions.php';
$iniAsArray = $ini['section-b'];
$definitionArray = $iniAsArray['di']['definitions'][1];
unset($definitionArray['class']);
Expand Down
27 changes: 6 additions & 21 deletions test/DiCompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use BadMethodCallException;
use Exception;
use PHPUnit_Framework_Error;
use SplStack;
use Zend\Di\Di;

class DiCompatibilityTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -41,13 +42,9 @@ public function testDiSimple($class)
public function providesSimpleClasses()
{
return [
['Zend\Di\Di'],
['Zend\EventManager\EventManager'],
['Zend\Filter\ToNull'],
['Zend\Form\Form'],
['Zend\Log\Logger'],
['Zend\Stdlib\SplStack'],
['Zend\View\Model\ViewModel'],
[Di::class],
[SplStack::class],
[TestAsset\BasicClass::class],
];
}

Expand Down Expand Up @@ -123,21 +120,9 @@ public function testCanCreateInstanceWithConstructorRequiredParameterWithDi($cla

public function providesClassWithConstructionParameters()
{
$serviceManager = new \Zend\ServiceManager\ServiceManager;
$eventManager = new \Zend\EventManager\EventManager;
$request = $this->getMockBuilder('Zend\Stdlib\RequestInterface')->getMock();
$response = $this->getMockBuilder('Zend\Stdlib\ResponseInterface')->getMock();

return [
['Zend\Config\Config', ['array' => []]],
['Zend\Db\Adapter\Adapter', ['driver' => ['driver' => 'Pdo_Sqlite']]],
['Zend\Mvc\Application', [
'configuration' => [],
'serviceManager' => $serviceManager,
'events' => $eventManager,
'request' => $request,
'response' => $response
]],
[TestAsset\BasicClassWithParam::class, ['foo' => 'bar']],
[TestAsset\ConstructorInjection\X::class, ['one' => 1, 'two' => 2]],
];
}
}
47 changes: 47 additions & 0 deletions test/DiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,4 +1017,51 @@ public function testGetInstanceWithParamsHasSameNameAsDependencyParam()
$di = new Di(null, null, $config);
$this->assertCount(1, $di->get('ZendTest\Di\TestAsset\AggregatedParamClass')->aggregator->items);
}

public function hasInstanceProvider()
{
$config = new Config(['instance' => [
TestAsset\BasicClassWithParam::class => [
'params' => ['foo' => 'bar'],
],
]]);

$classDefB = new Definition\ClassDefinition(TestAsset\CallbackClasses\B::class);
$classDefC = new Definition\ClassDefinition(TestAsset\CallbackClasses\C::class);
$classDefB->setInstantiator(TestAsset\CallbackClasses\B::class . '::factory');
$classDefB->addMethod('factory', true);
$classDefB->addMethodParameter('factory', 'c', [
'type' => TestAsset\CallbackClasses\C::class,
'required' => true,
]);
$classDefB->addMethodParameter('factory', 'params', ['type' => 'Array', 'required' => false]);
$definitionList = new DefinitionList([
$classDefB,
$classDefC,
new Definition\RuntimeDefinition(),
]);

$instanceManager = new InstanceManager();
$instanceManager->setParameters(TestAsset\ConstructorInjection\X::class, ['one' => 1, 'two' => 2]);

return [
'no-config' => [null, null, null, TestAsset\BasicClass::class],
'config-instance' => [null, null, $config, TestAsset\BasicClassWithParam::class],
'definition-list' => [$definitionList, null, null, TestAsset\CallbackClasses\B::class],
'instance-manager' => [null, $instanceManager, null, TestAsset\ConstructorInjection\X::class],
];
}

/**
* @dataProvider hasInstanceProvider
*/
public function testCanQueryToSeeIfContainerHasOrCanCreateAnInstance(
$definitionList,
$instanceManager,
$config,
$testFor
) {
$di = new Di($definitionList, $instanceManager, $config);
$this->assertTrue($di->has($testFor), sprintf('Failed to find instance for %s', $testFor));
}
}
Loading