From f1b32f097aeb49963da86f5ea2a9727505f348a0 Mon Sep 17 00:00:00 2001 From: Johannes Heinen Date: Mon, 20 Jun 2011 21:55:15 +0200 Subject: [PATCH] Worked on code coverage - added Tests to cover optional @Service arguments. --- .../Loader/AnnotatedFileLoader.php | 25 ++++++++-- .../Loader/AnnotatedFileLoaderTest.php | 43 +++++++++++++++++ .../Tests/Fixtures/FullFledgedService.php | 48 +++++++++++++++++++ .../AutowiringBundle/Tests/Fixtures/init.php | 4 ++ 4 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/FullFledgedService.php create mode 100644 src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/init.php diff --git a/src/Ifschleife/Bundle/AutowiringBundle/DependencyInjection/Loader/AnnotatedFileLoader.php b/src/Ifschleife/Bundle/AutowiringBundle/DependencyInjection/Loader/AnnotatedFileLoader.php index 61adb2c..7f899a0 100644 --- a/src/Ifschleife/Bundle/AutowiringBundle/DependencyInjection/Loader/AnnotatedFileLoader.php +++ b/src/Ifschleife/Bundle/AutowiringBundle/DependencyInjection/Loader/AnnotatedFileLoader.php @@ -59,7 +59,7 @@ class AnnotatedFileLoader extends FileLoader /** * @var string $path: The absolute path to the php class file (given by the locator). */ - protected $classFilePath; + protected $locatedClasspath; /** * @var FileLocatorInterface @@ -106,8 +106,13 @@ public function getContainer() */ public function load($resource, $type = null) { + if(null === $this->locatedClasspath || null === $this->classes) + { + throw new \BadMethodCallException('You tried to load a resource with an uninitialized loader, or attempted to load an unsupported resource. Call supports($resource) before loading.'); + } + // FILE RESOURCE ENABLES THE CONTAINER TO CHECK IF IT IS UP-TO-DATE - $this->container->addResource(new FileResource($this->classFilePath)); + $this->container->addResource(new FileResource($this->locatedClasspath)); // services $this->injectServices(); @@ -125,9 +130,9 @@ public function supports($resource, $type = null) { if(is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION)) { - $this->classFilePath = $this->locator->locate($resource); + $this->locatedClasspath = $this->locator->locate($resource); - $this->classes = $this->phpParser->parseFile($this->classFilePath); + $this->classes = $this->detectContainedClasses($this->locatedClasspath); return count($this->classes) > 0; } @@ -135,6 +140,18 @@ public function supports($resource, $type = null) return false; } + /** + * Parsed the sourcecode of the given $resource and stores any found + * php classes in $this->classes. + * + * @param type $resource + * @return array $foundClasses: An array of found classes. + */ + protected function detectContainedClasses($resource) + { + return $this->phpParser->parseFile($resource); + } + /** * Creates Services from the given \Reflection classes. * diff --git a/src/Ifschleife/Bundle/AutowiringBundle/Tests/DependencyInjection/Loader/AnnotatedFileLoaderTest.php b/src/Ifschleife/Bundle/AutowiringBundle/Tests/DependencyInjection/Loader/AnnotatedFileLoaderTest.php index b7ce3bb..25dcf0e 100644 --- a/src/Ifschleife/Bundle/AutowiringBundle/Tests/DependencyInjection/Loader/AnnotatedFileLoaderTest.php +++ b/src/Ifschleife/Bundle/AutowiringBundle/Tests/DependencyInjection/Loader/AnnotatedFileLoaderTest.php @@ -77,6 +77,42 @@ function testMalformedFiles($file) $this->assertFalse($loader->supports($file)); } + /** + * Tests optional arguments like "scope", "tags", "File", "Public" + * @dataProvider filesWithOptionalArguments + */ + function testOptionalArguments($file) + { + $container = new ContainerBuilder(); + + $container->setParameter('test_path', __DIR__ . '/../../Fixtures/init.php'); + + $parser = new PhpParser(); + + $reader = new AnnotationReaderDecorator(); + + $locator = new FileLocator(); + + $containerInjector = new \Ifschleife\Bundle\AutowiringBundle\Autowiring\Injector\ContainerInjector($container, $reader); + + $loader = new AnnotatedFileLoader($container, $containerInjector, $locator, $parser); + + $this->assertTrue($loader->supports($file)); + + $loader->load($file); + + $this->assertInstanceOf('Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\FullFledgedService', $service = $container->get('autowiring.full_fledged_service')); + + // SET BY PRE-REQUIRED FILE + $this->assertTrue(\Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\FullFledgedService::$TEST); + + $this->assertTrue($container->getDefinition('autowiring.full_fledged_service')->isPublic()); + + $this->assertContains('my.tag', $container->getDefinition('autowiring.full_fledged_service')->getTags()); + + $this->assertFalse($container->getDefinition('autowiring.full_fledged_service2')->isPublic()); + } + function files() { return array( @@ -91,4 +127,11 @@ function malformedFiles() array(__DIR__ . '/../../Fixtures/CreateServiceMalformed1.xml') ); } + + function filesWithOptionalArguments() + { + return array( + array(__DIR__ . '/../../Fixtures/FullFledgedService.php') + ); + } } diff --git a/src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/FullFledgedService.php b/src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/FullFledgedService.php new file mode 100644 index 0000000..86878d7 --- /dev/null +++ b/src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/FullFledgedService.php @@ -0,0 +1,48 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures; + +use Ifschleife\Bundle\AutowiringBundle\Annotations\Service; + +/** + * FullFledgedService + * + * @author joshi + * @Service(Id="autowiring.full_fledged_service", Tags={"my.tag"}, Public=true, File="%test_path%") + */ +class FullFledgedService +{ + public static $TEST=false; +} + +/** + * FullFledgedService2 + * + * @author joshi + * @Service(Id="autowiring.full_fledged_service2", Tags={"my.tag2"}, Public=false) + */ +class FullFledgedService2 +{ + +} \ No newline at end of file diff --git a/src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/init.php b/src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/init.php new file mode 100644 index 0000000..baabfc1 --- /dev/null +++ b/src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/init.php @@ -0,0 +1,4 @@ +