/usr/share/php/tests/Horde_Injector/Horde/Injector/InjectorTest.php is in php-horde-injector 2.0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | <?php
class Horde_Injector_InjectorTest extends PHPUnit_Framework_TestCase
{
public function testShouldGetDefaultImplementationBinder()
{
$topLevel = $this->getMock('Horde_Injector_TopLevel', array('getBinder'));
$topLevel->expects($this->once())
->method('getBinder')
->with($this->equalTo('UNBOUND_INTERFACE'))
->will($this->returnValue('RETURNED_BINDING'));
$injector = new Horde_Injector($topLevel);
$this->assertEquals('RETURNED_BINDING', $injector->getBinder('UNBOUND_INTERFACE'));
}
public function testShouldGetManuallyBoundBinder()
{
$injector = new Horde_Injector(new Horde_Injector_TopLevel());
$binder = new Horde_Injector_Binder_Mock();
$injector->addBinder('BOUND_INTERFACE', $binder);
$this->assertSame($binder, $injector->getBinder('BOUND_INTERFACE'));
}
public function testShouldProvideMagicFactoryMethodForBinderAddition()
{
$injector = new Horde_Injector(new Horde_Injector_TopLevel());
// binds a Horde_Injector_Binder_Mock object
$this->assertInstanceOf('Horde_Injector_Binder_Mock', $injector->bindMock('BOUND_INTERFACE'));
$this->assertInstanceOf('Horde_Injector_Binder_Mock', $injector->getBinder('BOUND_INTERFACE'));
}
public function testShouldProvideMagicFactoryMethodForBinderAdditionWhereBinderHasDependencies()
{
$injector = new Horde_Injector(new Horde_Injector_TopLevel());
// binds a Horde_Injector_Binder_Mock object
$this->assertInstanceOf('Horde_Injector_Binder_MockWithDependencies',
$injector->bindMockWithDependencies('BOUND_INTERFACE', 'PARAMETER1'));
$this->assertInstanceOf('Horde_Injector_Binder_MockWithDependencies',
$injector->getBinder('BOUND_INTERFACE'));
}
/**
* @expectedException BadMethodCallException
*/
public function testShouldThrowExceptionIfInterfaceNameIsNotPassedToMagicFactoryMethodForBinderAddition()
{
$injector = new Horde_Injector($this->_getTopLevelNeverCalledMock());
$injector->bindMock();
}
/**
* @expectedException BadMethodCallException
*/
public function testShouldThrowExceptionIfMethodNameIsInvalid()
{
$injector = new Horde_Injector($this->_getTopLevelNeverCalledMock());
$injector->invalid();
}
public function testShouldReturnItselfWhenInjectorRequested()
{
$injector = new Horde_Injector($this->_getTopLevelNeverCalledMock());
$this->assertSame($injector, $injector->getInstance('Horde_Injector'));
}
/**
* Would love to use PHPUnit's mock object here istead of Horde_Injector_Binder_Mock but you
* can't be sure the expected resulting object is the same object you told the mock to return.
* This is because Mock clone objects passed to mocked methods.
*
* http://www.phpunit.de/ticket/120
*
* @author Bob McKee <bmckee@bywires.com>
*/
public function testCreateInstancePassesCurrentInjectorScopeToBinderForCreation()
{
$injector = new Horde_Injector(new Horde_Injector_TopLevel());
$injector->addBinder('BOUND_INTERFACE', new Horde_Injector_Binder_Mock());
// normally you wouldn't get an injector back; the binder would create something and return
// it to you. here we are just confirming that the proper injector was passed to the
// binder's create method.
$this->assertEquals($injector, $injector->createInstance('BOUND_INTERFACE'));
}
public function testShouldNotReturnSharedObjectOnCreate()
{
$injector = $this->_createInjector();
//this call will cache this class on the injector
$stdclass = $injector->getInstance('StdClass');
$this->assertNotSame($stdclass, $injector->createInstance('StdClass'));
}
public function testShouldNotShareObjectCreatedUsingCreate()
{
$injector = $this->_createInjector();
// this call should not store the instance on the injector
$stdclass = $injector->createInstance('StdClass');
$this->assertNotSame($stdclass, $injector->getInstance('StdClass'));
}
public function testChildSharesInstancesOfParent()
{
$injector = $this->_createInjector();
//this call will store the created instance on $injector
$stdclass = $injector->getInstance('StdClass');
// create a child injector and ensure that the stdclass returned is the same
$child = $injector->createChildInjector();
$this->assertSame($stdclass, $child->getInstance('StdClass'));
}
private function _createInjector()
{
return new Horde_Injector(new Horde_Injector_TopLevel());
}
public function testShouldReturnSharedInstanceIfRequested()
{
$injector = new Horde_Injector($this->_getTopLevelNeverCalledMock());
$instance = new StdClass();
$injector->setInstance('INSTANCE_INTERFACE', $instance);
$this->assertSame($instance, $injector->getInstance('INSTANCE_INTERFACE'));
}
/**
* this test should test that when you override a binding in a child injector,
* that the child does not create a new version of the object if the binding has not changed
*/
public function testChildInjectorDoNotSaveBindingLocallyWhenBinderIsSameAsParent()
{
// we need to set a class for an instance on the parent
$injector = new Horde_Injector(new Horde_Injector_TopLevel());
$df = new Horde_Injector_DependencyFinder();
$injector->addBinder('FooBarInterface', new Horde_Injector_Binder_Implementation('StdClass', $df));
// getInstance will save $returnedObject and return it again later when FooBarInterface is requested
$returnedObject = $injector->getInstance('FooBarInterface');
$childInjector = $injector->createChildInjector();
// add same binding again to child
$childInjector->addBinder('FooBarInterface', new Horde_Injector_Binder_Implementation('StdClass', $df));
$this->assertSame($returnedObject, $childInjector->getInstance('FooBarInterface'),
"Child should have returned object reference from parent because added binder was identical to the parent binder");
}
/**
* this test should test that when you override a binding in a child injector,
* that the child creates a new version of the object, and not the parent's cached version
* if the binding is changed
*/
public function testChildInjectorsDoNotAskParentForInstanceIfBindingIsSet()
{
$mockTopLevel = $this->getMock('Horde_Injector_TopLevel', array('getInstance'));
$mockTopLevel->expects($this->never())->method('getInstance');
$injector = new Horde_Injector($mockTopLevel);
$injector->addBinder('StdClass', new Horde_Injector_Binder_Mock());
$injector->getInstance('StdClass');
}
public function testChildInjectorAsksParentForInstance()
{
$topLevelMock = $this->getMock('Horde_Injector_TopLevel', array('getInstance'));
$topLevelMock->expects($this->once())
->method('getInstance')
->with('StdClass');
$injector = new Horde_Injector($topLevelMock);
$injector->getInstance('StdClass');
}
/**
* Would love to use PHPUnit's mock object here istead of Horde_Injector_Binder_Mock but you
* can't be sure the expected resulting object is the same object you told the mock to return.
* This is because Mock clone objects passed to mocked methods.
*
* http://www.phpunit.de/ticket/120
*
* @author Bob McKee <bmckee@bywires.com>
*/
public function testShouldCreateAndStoreSharedObjectIfOneDoesNotAlreadyExist()
{
$injector = new Horde_Injector(new Horde_Injector_TopLevel());
$injector->addBinder('BOUND_INTERFACE', new Horde_Injector_Binder_Mock());
// should call "createInstance" and then "setInstance" on the result
// normally you wouldn't get an injector back; the binder would create something and return
// it to you. here we are just confirming that the proper injector was passed to the
// binder's create method.
$this->assertSame($injector, $injector->getInstance('BOUND_INTERFACE'));
// should just return stored instance
// the injector sent to the "create" method noted above should also be returned here.
$this->assertSame($injector, $injector->getInstance('BOUND_INTERFACE'));
}
public function testShouldCreateAndStoreSharedObjectInstanceIfDefaultTopLevelBinderIsUsed()
{
$injector = new Horde_Injector(new Horde_Injector_TopLevel());
$class = $injector->getInstance('StdClass');
$class2 = $injector->getInstance('StdClass');
$this->assertSame($class, $class2, "Injector did not return same object on consecutive getInstance calls");
}
public function testCreateChildInjectorReturnsDifferentInjector()
{
$injector = new Horde_Injector($this->_getTopLevelNeverCalledMock());
$childInjector = $injector->createChildInjector();
$this->assertInstanceOf('Horde_Injector', $childInjector);
$this->assertNotSame($injector, $childInjector);
}
public function testShouldAllowChildInjectorsAccessToParentInjectorBindings()
{
$mockInjector = $this->getMock('Horde_Injector_TopLevel', array('getBinder'));
$mockInjector->expects($this->any()) // this gets called once in addBinder
->method('getBinder')
->with('BOUND_INTERFACE')
->will($this->returnValue(new Horde_Injector_Binder_Mock()));
$injector = new Horde_Injector($mockInjector);
$binder = new Horde_Injector_Binder_Mock();
$injector->addBinder('BOUND_INTERFACE', $binder);
$childInjector = $injector->createChildInjector();
$this->assertSame($binder, $childInjector->getBinder('BOUND_INTERFACE'));
}
private function _getTopLevelNeverCalledMock()
{
$topLevel = $this->getMock('Horde_Injector_TopLevel', array('getBinder', 'getInstance'));
$topLevel->expects($this->never())->method('getBinder');
return $topLevel;
}
}
/**
* Used by preceding tests
*/
class Horde_Injector_Binder_Mock implements Horde_Injector_Binder
{
private $_interface;
public function create(Horde_Injector $injector)
{
return $injector;
}
public function equals(Horde_Injector_Binder $otherBinder)
{
return $otherBinder === $this;
}
}
class Horde_Injector_Binder_MockWithDependencies implements Horde_Injector_Binder
{
private $_interface;
public function __construct($parameter1)
{
}
public function create(Horde_Injector $injector)
{
return $injector;
}
public function equals(Horde_Injector_Binder $otherBinder)
{
return $otherBinder === $this;
}
}
|