Changeset 307
- Timestamp:
- 02/28/07 17:26:33 (2 years ago)
- Files:
-
- trunk/src/main/php/net/stubbles/ioc/injection/stubInjectAnnotation.php (moved) (moved from trunk/src/main/php/net/stubbles/ioc/injection/Inject.php) (5 diffs)
- trunk/src/main/php/net/stubbles/ioc/injection/stubInjectionException.php (moved) (moved from trunk/src/main/php/net/stubbles/ioc/injection/InjectionException.php) (1 diff)
- trunk/src/main/php/net/stubbles/ioc/injection/stubInjectionMap.php (moved) (moved from trunk/src/main/php/net/stubbles/ioc/injection/InjectionMap.php) (6 diffs)
- trunk/src/test/php/net/stubbles/ioc/IOCTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/ioc/injection/stubInjectTestCase.php (moved) (moved from trunk/src/test/php/net/stubbles/ioc/injection/InjectTestCase.php) (9 diffs)
- trunk/src/test/php/net/stubbles/ioc/injection/stubInjectionMapTestCase.php (moved) (moved from trunk/src/test/php/net/stubbles/ioc/injection/InjectionMapTestCase.php) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ioc/injection/stubInjectAnnotation.php
r251 r307 5 5 * @author Frank Kleine <mikey@stubbles.net> 6 6 * @package stubbles 7 * @subpackage ioc 7 * @subpackage ioc_injection 8 8 */ 9 9 stubClassLoader::load('net.stubbles.reflection.reflection', 10 'net.stubbles.ioc.injection. InjectionException',11 'net.stubbles.ioc.injection. InjectionMap'10 'net.stubbles.ioc.injection.stubInjectionException', 11 'net.stubbles.ioc.injection.stubInjectionMap' 12 12 ); 13 13 /** … … 15 15 * 16 16 * @package stubbles 17 * @subpackage ioc 17 * @subpackage ioc_injection 18 18 */ 19 class Injectextends stubAbstractAnnotation19 class stubInjectAnnotation extends stubAbstractAnnotation 20 20 { 21 21 /** … … 60 60 * 61 61 * @param stubReflectionClass $refClass the reflection class for $injectible 62 * @param InjectionMap$injectionMap map of values to inject62 * @param stubInjectionMap $injectionMap map of values to inject 63 63 * @param object $injectible the class to inject into 64 64 * @throws InjectionException 65 65 */ 66 protected function handleInjections(stubReflectionClass $refClass, InjectionMap $injectionMap, $injectible)66 protected function handleInjections(stubReflectionClass $refClass, stubInjectionMap $injectionMap, $injectible) 67 67 { 68 68 foreach ($this->injections as $injection) { 69 if ( isset($injectionMap[$injection]) == false) {70 throw new InjectionException('Injectible needs ' . $injection . ' but it was not given in list of injected classes.');69 if ($injectionMap->hasInjection($injection) == false) { 70 throw new stubInjectionException('Injectible needs ' . $injection . ' but it was not given in list of injected classes.'); 71 71 } 72 72 … … 74 74 if ($refClass->hasMethod($methodName) == true && $refClass->getMethod($methodName)->isPublic() == true) { 75 75 try { 76 $refClass->getMethod($methodName)->invoke($injectible, $injectionMap [$injection]);76 $refClass->getMethod($methodName)->invoke($injectible, $injectionMap->getInjection($injection)); 77 77 } catch (ReflectionException $re) { 78 throw new InjectionException('Could not inject ' . $injection . ': ' . $re->getMessage());78 throw new stubInjectionException('Could not inject ' . $injection . ': ' . $re->getMessage()); 79 79 } 80 80 } elseif ($refClass->hasProperty($injection) == true && $refClass->getProperty($injection)->isPublic() == true) { 81 $refClass->getProperty($injection)->setValue($injectible, $injectionMap [$injection]);81 $refClass->getProperty($injection)->setValue($injectible, $injectionMap->getInjection($injection)); 82 82 } elseif ($refClass->hasMethod('__set') == true && $refClass->getMethod('__set')->isPublic() == true) { 83 $refClass->getMethod('__set')->invoke($injectible, $injection, $injectionMap [$injection]);83 $refClass->getMethod('__set')->invoke($injectible, $injection, $injectionMap->getInjection($injection)); 84 84 } else { 85 throw new InjectionException('Given injectible has no method or property that accepts injection ' . $injection);85 throw new stubInjectionException('Given injectible has no method or property that accepts injection ' . $injection); 86 86 } 87 87 } … … 95 95 * @throws InjectionException 96 96 */ 97 public static function factory( InjectionMap $injectionMap, $injectible)97 public static function factory(stubInjectionMap $injectionMap, $injectible) 98 98 { 99 99 if (is_object($injectible) == false) { 100 throw new InjectionException('Given injectible is not an object. Can only inject into objects.');100 throw new stubInjectionException('Given injectible is not an object. Can only inject into objects.'); 101 101 } 102 102 trunk/src/main/php/net/stubbles/ioc/injection/stubInjectionException.php
r66 r307 13 13 * @subpackage ioc 14 14 */ 15 class InjectionException extends stubException15 class stubInjectionException extends stubException 16 16 { 17 17 trunk/src/main/php/net/stubbles/ioc/injection/stubInjectionMap.php
r65 r307 5 5 * @author Frank Kleine <mikey@stubbles.net> 6 6 * @package stubbles 7 * @subpackage ioc 7 * @subpackage ioc_injection 8 8 */ 9 9 /** … … 11 11 * 12 12 * @package stubbles 13 * @subpackage ioc 13 * @subpackage ioc_injection 14 14 */ 15 class InjectionMap extends stubBaseObject implements ArrayAccess, Countable, IteratorAggregate15 class stubInjectionMap extends stubBaseObject 16 16 { 17 17 /** 18 18 * list of injections with their keywords 19 19 * 20 * @var array<string, mixed>20 * @var array<string,stubObject> 21 21 */ 22 22 protected $map = array(); … … 25 25 * Add an injection 26 26 * 27 * @param string $offsetkeyword for injection28 * @param mixed $value valueto inject27 * @param string $key keyword for injection 28 * @param stubObject $class class to inject 29 29 */ 30 public function offsetSet($offset, $value)30 public function addInjection($key, stubObject $class) 31 31 { 32 $this->map[$ offset] = $value;32 $this->map[$key] = $class; 33 33 } 34 34 … … 36 36 * Remove the injection 37 37 * 38 * @param string $ offsetkeyword under which injection is stored38 * @param string $key keyword under which injection is stored 39 39 */ 40 public function offsetUnset($offset)40 public function removeInjection($key) 41 41 { 42 if (isset($this->map[$ offset]) == true) {43 unset($this->map[$ offset]);42 if (isset($this->map[$key]) == true) { 43 unset($this->map[$key]); 44 44 } 45 45 } … … 48 48 * Returns the injection for the given keyword 49 49 * 50 * @param string $offsetkeyword under which injection is stored51 * @return mixed50 * @param string $key keyword under which injection is stored 51 * @return stubObject 52 52 */ 53 public function offsetGet($offset)53 public function getInjection($key) 54 54 { 55 if (isset($this->map[$ offset]) == true) {56 return $this->map[$ offset];55 if (isset($this->map[$key]) == true) { 56 return $this->map[$key]; 57 57 } 58 58 … … 63 63 * Check whether an injection for the given keyword exists 64 64 * 65 * @param string $ offsetkeyword under which injection is stored65 * @param string $key keyword under which injection is stored 66 66 * @return bool true if an injection for given keyword exists 67 67 */ 68 public function offsetExists ($offset)68 public function hasInjection($key) 69 69 { 70 return isset($this->map[$offset]); 71 } 72 73 /** 74 * count the amount of injections 75 * 76 * @return int 77 */ 78 public function count() 79 { 80 return count($this->map); 81 } 82 83 /** 84 * Returns an iterator for the map 85 * 86 * @return ArrayIterator 87 */ 88 public function getIterator() 89 { 90 return new ArrayIterator($this->map); 70 return isset($this->map[$key]); 91 71 } 92 72 } trunk/src/test/php/net/stubbles/ioc/IOCTestSuite.php
r30 r307 21 21 { 22 22 $this->TestSuite('All ioc tests'); 23 $this->addTestFile(dirname(__FILE__) . '/injection/ InjectTestCase.php');24 $this->addTestFile(dirname(__FILE__) . '/injection/ InjectionMapTestCase.php');23 $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectTestCase.php'); 24 $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectionMapTestCase.php'); 25 25 } 26 26 } trunk/src/test/php/net/stubbles/ioc/injection/stubInjectTestCase.php
r98 r307 1 1 <?php 2 2 /** 3 * Test for ioc.injection.Inject3 * Test for net.stubbles.ioc.injection.stubInjectAnnotation 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> 6 6 * @package stubbles 7 * @subpackage reflection_test7 * @subpackage ioc_injection_test 8 8 */ 9 stubClassLoader::load('net.stubbles.ioc.injection. Inject');10 class TestInjection1 {}11 class TestInjection2 {}12 class TestInjection3 {}9 stubClassLoader::load('net.stubbles.ioc.injection.stubInjectAnnotation'); 10 class TestInjection1 extends stubBaseObject {} 11 class TestInjection2 extends stubBaseObject {} 12 class TestInjection3 extends stubBaseObject {} 13 13 /** 14 14 * class for testing injections … … 45 45 } 46 46 /** 47 * Test for ioc.injection.Inject47 * Test for net.stubbles.ioc.injection.stubInjectAnnotation 48 48 * 49 49 * @package stubbles 50 * @subpackage reflection_test50 * @subpackage ioc_injection_test 51 51 */ 52 class InjectTestCase extends UnitTestCase52 class stubInjectTestCase extends UnitTestCase 53 53 { 54 54 /** 55 55 * injection map to use for tests 56 56 * 57 * @var InjectionMap57 * @var stubInjectionMap 58 58 */ 59 59 protected $injectionMap; … … 64 64 public function setUp() 65 65 { 66 $this->injectionMap = new InjectionMap();67 $this->injectionMap ['TestInjection1'] = new TestInjection1();68 $this->injectionMap ['TestInjection2'] = new TestInjection2();69 $this->injectionMap ['TestInjection3'] = new TestInjection3();66 $this->injectionMap = new stubInjectionMap(); 67 $this->injectionMap->addInjection('TestInjection1', new TestInjection1()); 68 $this->injectionMap->addInjection('TestInjection2', new TestInjection2()); 69 $this->injectionMap->addInjection('TestInjection3', new TestInjection3()); 70 70 } 71 71 /** … … 74 74 public function testInjections() 75 75 { 76 $inject = new Inject();76 $inject = new stubInjectAnnotation(); 77 77 $inject->setValue('TestInjection1:TestInjection2:TestInjection3'); 78 78 $this->assertEqual($inject->getInjections(), array('TestInjection1', 'TestInjection2', 'TestInjection3')); … … 85 85 { 86 86 $testInjectible = new TestInjectible1(); 87 Inject::factory($this->injectionMap, $testInjectible);88 $t his->assertReference($this->injectionMap['TestInjection1'], $testInjectible->getTestInjection1());89 $t his->assertReference($this->injectionMap['TestInjection2'], $testInjectible->TestInjection2);90 $this->assertReference($t his->injectionMap['TestInjection3'], $testInjectible->TestInjection3);87 stubInjectAnnotation::factory($this->injectionMap, $testInjectible); 88 $test1a = $this->injectionMap->getInjection('TestInjection1'); 89 $test1b = $testInjectible->getTestInjection1(); 90 $this->assertReference($test1a, $test1b); 91 91 } 92 92 … … 97 97 { 98 98 $testInjectible = new TestInjectible1(); 99 $this->expectException(' InjectionException');100 Inject::factory(newInjectionMap(), $testInjectible);99 $this->expectException('stubInjectionException'); 100 stubInjectAnnotation::factory(new stubInjectionMap(), $testInjectible); 101 101 } 102 102 … … 107 107 { 108 108 $testInjectible = new TestInjectible2(); 109 $this->expectException(' InjectionException');110 Inject::factory($this->injectionMap, $testInjectible);109 $this->expectException('stubInjectionException'); 110 stubInjectAnnotation::factory($this->injectionMap, $testInjectible); 111 111 } 112 112 … … 120 120 $testInjectible->expectNever('setTestInjection1'); 121 121 $testInjectible->expectNever('__set'); 122 Inject::factory($this->injectionMap, $testInjectible);122 stubInjectAnnotation::factory($this->injectionMap, $testInjectible); 123 123 } 124 124 … … 128 128 public function testHandlingWrongInjectible() 129 129 { 130 $this->expectException(' InjectionException');131 Inject::factory($this->injectionMap, 'foo');130 $this->expectException('stubInjectionException'); 131 stubInjectAnnotation::factory($this->injectionMap, 'foo'); 132 132 } 133 133 } trunk/src/test/php/net/stubbles/ioc/injection/stubInjectionMapTestCase.php
r98 r307 1 1 <?php 2 2 /** 3 * Test for ioc.injection.InjectionMap3 * Test for net.stubbles.ioc.injection.stubInjectionMap 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> 6 6 * @package stubbles 7 * @subpackage reflection_test7 * @subpackage ioc_injection_test 8 8 */ 9 stubClassLoader::load('net.stubbles.ioc.injection. InjectionMap');9 stubClassLoader::load('net.stubbles.ioc.injection.stubInjectionMap'); 10 10 /** 11 * Test for ioc.injection.InjectionMap11 * Test for net.stubbles.ioc.injection.stubInjectionMap 12 12 * 13 13 * @package stubbles 14 * @subpackage reflection_test14 * @subpackage ioc_injection_test 15 15 */ 16 class InjectionMapTestCase extends UnitTestCase16 class stubInjectionMapTestCase extends UnitTestCase 17 17 { 18 18 /** 19 19 * instance to test 20 20 * 21 * @var InjectionMap21 * @var stubInjectionMap 22 22 */ 23 23 protected $injectionMap; 24 /** 25 * the base object used for putting into the map 26 * 27 * @var stubBaseObject 28 */ 29 protected $baseObject; 24 30 25 31 /** … … 28 34 public function setUp() 29 35 { 30 $this->injectionMap = new InjectionMap(); 31 $this->injectionMap['foo'] = 'baz'; 36 $this->injectionMap = new stubInjectionMap(); 37 $this->baseObject = new stubBaseObject(); 38 $this->injectionMap->addInjection('foo', $this->baseObject); 32 39 } 33 40 … … 37 44 public function testSetUnset() 38 45 { 39 $this->assertEqual($this->injectionMap['foo'], 'baz'); 40 $this->assertTrue(isset($this->injectionMap['foo'])); 41 $this->assertEqual(count($this->injectionMap['foo']), 1); 42 unset($this->injectionMap['foo']); 43 $this->assertNull($this->injectionMap['foo']); 44 $this->assertFalse(isset($this->injectionMap['foo'])); 45 $this->assertEqual(count($this->injectionMap['foo']), 0); 46 } 47 48 /** 49 * test that you can iterator over the map 50 */ 51 public function testIterator() 52 { 53 foreach ($this->injectionMap as $key => $value) { 54 $this->assertEqual($key, 'foo'); 55 $this->assertEqual($value, 'baz'); 56 } 46 $this->assertNull($this->injectionMap->getInjection('bar')); 47 $this->assertFalse($this->injectionMap->hasInjection('bar')); 48 $this->assertEqual($this->injectionMap->getInjection('foo'), $this->baseObject); 49 $this->assertTrue($this->injectionMap->hasInjection('foo')); 50 $this->injectionMap->removeInjection('foo'); 51 $this->assertNull($this->injectionMap->getInjection('foo')); 52 $this->assertFalse($this->injectionMap->hasInjection('foo')); 57 53 } 58 54 }
