Permalink
Browse files

Worked on code coverage - added Tests to cover optional @Service argu…

…ments.
  • Loading branch information...
joshiausdemwald committed Jun 20, 2011
1 parent 8fb3fe7 commit f1b32f097aeb49963da86f5ea2a9727505f348a0
@@ -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,16 +130,28 @@ 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;
}
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.
*
@@ -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')
+ );
+ }
}
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Copyright (c) 2011 Johannes Heinen <johannes.heinen@gmail.com>
+ *
+ * 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
+{
+
+}
@@ -0,0 +1,4 @@
+<?php
+namespace Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures;
+
+FullFledgedService::$TEST = true;

0 comments on commit f1b32f0

Please sign in to comment.