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

Commit

Permalink
Merge pull request #54 from tux-rampage/hotfix/preference-resolver-test
Browse files Browse the repository at this point in the history
Add tests to ensure preference fallback behavior
  • Loading branch information
tux-rampage committed Jan 15, 2019
2 parents 615fc00 + dcf42bc commit 7a2e6d2
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
100 changes: 100 additions & 0 deletions test/Resolver/DependencyResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,4 +640,104 @@ public function testParametresResolverShouldNotCheckTheTypeForString()
$this->assertInstanceOf(TypeInjection::class, $parameters['a']);
$this->assertSame('my-service', $parameters['a']->__toString());
}

/**
* Ensures the documented preference resolver behavior as documented
*
* @see https://docs.zendframework.com/zend-di/config/#type-preferences
*/
public function testResolvePreferenceFallsBackToGlobalPreferenceWhenNotSuitableForClassRequirement()
{
$definition = new RuntimeDefinition();
$config = new Config();
$config->setTypePreference(TestAsset\A::class, TestAsset\B::class, TestAsset\RequiresA::class);
$config->setTypePreference(TestAsset\A::class, TestAsset\ExtendedA::class);
$resolver = new DependencyResolver($definition, $config);

$this->assertSame(
TestAsset\ExtendedA::class,
$resolver->resolvePreference(TestAsset\A::class, TestAsset\RequiresA::class)
);
}

/**
* Ensures the documented preference resolver behavior as documented
*
* @see https://docs.zendframework.com/zend-di/config/#type-preferences
*/
public function testResolvePreferenceReturnsNullWhenNothingIsSuitableForClassRequirement()
{
$definition = new RuntimeDefinition();
$config = new Config();
$config->setTypePreference(TestAsset\A::class, TestAsset\ExtendedB::class, TestAsset\RequiresA::class);
$config->setTypePreference(TestAsset\A::class, TestAsset\B::class);
$resolver = new DependencyResolver($definition, $config);

$this->assertNull($resolver->resolvePreference(TestAsset\A::class, TestAsset\RequiresA::class));
}

/**
* Ensures the documented preference resolver behavior as documented
*
* @see https://docs.zendframework.com/zend-di/config/#type-preferences
*/
public function testResolvePreferenceFallsBackToGlobalPreferenceWhenNotSuitableForInterfaceRequirement()
{
$definition = new RuntimeDefinition();
$config = new Config();
$config->setTypePreference(
TestAsset\Hierarchy\InterfaceB::class,
TestAsset\Hierarchy\InterfaceA::class,
TestAsset\A::class
);
$config->setTypePreference(TestAsset\Hierarchy\InterfaceB::class, TestAsset\Hierarchy\InterfaceC::class);
$resolver = new DependencyResolver($definition, $config);

$this->assertSame(
TestAsset\Hierarchy\InterfaceC::class,
$resolver->resolvePreference(TestAsset\Hierarchy\InterfaceB::class, TestAsset\A::class)
);
}

/**
* Ensures the documented preference resolver behavior as documented
*
* @see https://docs.zendframework.com/zend-di/config/#type-preferences
*/
public function testResolvePreferenceReturnsNullWhenNothingIsSuitableForInterfaceRequirement()
{
$definition = new RuntimeDefinition();
$config = new Config();
$config->setTypePreference(
TestAsset\Hierarchy\InterfaceB::class,
TestAsset\B::class,
TestAsset\A::class
);
$config->setTypePreference(
TestAsset\Hierarchy\InterfaceB::class,
TestAsset\Hierarchy\InterfaceA::class
);
$resolver = new DependencyResolver($definition, $config);

$this->assertNull(
$resolver->resolvePreference(TestAsset\Hierarchy\InterfaceB::class, TestAsset\A::class)
);
}

public function testResolvePreferenceUsesDefinedClassForInterfaceRequirements()
{
$definition = new RuntimeDefinition();
$config = new Config();
$config->setTypePreference(
TestAsset\Hierarchy\InterfaceB::class,
TestAsset\Hierarchy\B::class
);

$resolver = new DependencyResolver($definition, $config);

$this->assertSame(
TestAsset\Hierarchy\B::class,
$resolver->resolvePreference(TestAsset\Hierarchy\InterfaceB::class, TestAsset\A::class)
);
}
}
12 changes: 12 additions & 0 deletions test/TestAsset/ExtendedA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* @see https://github.com/zendframework/zend-di for the canonical source repository
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-di/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Di\TestAsset;

class ExtendedA extends A
{
}

0 comments on commit 7a2e6d2

Please sign in to comment.