Permalink
Browse files

Fixed bug in PHPParser, added some unit tests for php parser

  • Loading branch information...
1 parent e72f7e8 commit c828cb7aa6f7b20740582967f8f4e6a98434026c @joshiausdemwald committed Jun 2, 2011
Showing with 417 additions and 97 deletions.
  1. +2 −2 ...hleife/Bundle/AutowiringBundle/Autowiring/{ServiceBuilderException.php → AutowiringException.php}
  2. +1 −1 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/DefinitionNotFoundException.php
  3. +3 −3 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/{ → Injector}/ArgumentsAlreadyDefinedException.php
  4. +1 −1 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/Injector/ConstructorInjector.php
  5. +3 −1 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/Injector/InjectorException.php
  6. +2 −2 ...dle/Autowiring/{DuplicateServiceIdException.php → Injector/MethodCallAlreadyDefinedException.php}
  7. +2 −2 ...ringBundle/Autowiring/{DependencyResolverException.php → Injector/UnresolvedServiceException.php}
  8. +65 −40 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/Parser/PhpParser.php
  9. +2 −2 src/Ifschleife/Bundle/AutowiringBundle/Resources/config/autowiring.xml
  10. +0 −32 src/Ifschleife/Bundle/AutowiringBundle/Test/ParentTestclass.php
  11. +72 −0 src/Ifschleife/Bundle/AutowiringBundle/Tests/Autowiring/Parser/PhpParserTest.php
  12. +8 −5 ...le/AutowiringBundle/{Autowiring/MethodCallAlreadyDefinedException.php → Tests/AutowiringTest.php}
  13. +49 −0 src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/ParentTestclass.php
  14. +37 −0 src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/PhpParserTestclass0.php
  15. +6 −4 ...wiringBundle/{Autowiring/UnresolvedServiceException.php → Tests/Fixtures/PhpParserTestclass1.php}
  16. +49 −0 src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/PhpParserTestclass2.php
  17. +65 −0 src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/PhpParserTestclass3.php
  18. +48 −0 src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/PhpParserTestclass4.php
  19. +1 −1 src/Ifschleife/Bundle/AutowiringBundle/{Test → Tests/Fixtures}/Testclass.php
  20. +1 −1 src/Ifschleife/Bundle/AutowiringBundle/{Test → Tests/Fixtures}/Testservice.php
@@ -24,11 +24,11 @@
namespace Ifschleife\Bundle\AutowiringBundle\Autowiring;
/**
- * DuplicateServiceIdException
+ * AutowiringException
*
* @author joshi
*/
-class ServiceBuilderException extends Exception
+class AutowiringException extends \Exception
{
}
@@ -28,7 +28,7 @@
*
* @author joshi
*/
-class DefinitionNotFoundException extends DependencyResolverException
+class DefinitionNotFoundException extends AutowiringException
{
}
@@ -21,14 +21,14 @@
* SOFTWARE.
*/
-namespace Ifschleife\Bundle\AutowiringBundle\Autowiring;
+namespace Ifschleife\Bundle\AutowiringBundle\Autowiring\Injector;
/**
- * DefinitionNotFoundException
+ * ArgumentsAlreadyDefinedException
*
* @author joshi
*/
-class ArgumentsAlreadyDefinedException extends DependencyResolverException
+class ArgumentsAlreadyDefinedException extends InjectorException
{
}
@@ -24,7 +24,7 @@
namespace Ifschleife\Bundle\AutowiringBundle\Autowiring\Injector;
use Symfony\Component\DependencyInjection\Definition;
-use Ifschleife\Bundle\AutowiringBundle\Autowiring\ArgumentsAlreadyDefinedException;
+
/**
* Description of ConstuctorInjector
*
@@ -23,12 +23,14 @@
namespace Ifschleife\Bundle\AutowiringBundle\Autowiring\Injector;
+use Ifschleife\Bundle\AutowiringBundle\Autowiring\AutowiringException;
+
/**
* Generic InjectorException
*
* @author joshi
*/
-class InjectorException extends \Exception
+class InjectorException extends AutowiringException
{
}
@@ -24,11 +24,11 @@
namespace Ifschleife\Bundle\AutowiringBundle\Autowiring;
/**
- * DuplicateServiceIdException
+ * MethodCallAlreadyDefinedException
*
* @author joshi
*/
-class DuplicateServiceIdException extends ServiceBuilderException
+class MethodCallAlreadyDefinedException extends InjectorException
{
}
@@ -24,11 +24,11 @@
namespace Ifschleife\Bundle\AutowiringBundle\Autowiring;
/**
- * DefinitionNotFoundException
+ * UnresolvedServiceException
*
* @author joshi
*/
-class DependencyResolverException extends \Exception
+class UnresolvedServiceException extends InjectorException
{
}
@@ -20,57 +20,82 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-
-namespace Ifschleife\Bundle\AutowiringBundle\Autowiring\Parser;
-
-/**
- * Parses a file for namespaces/use/class declarations.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- * @author Johannes Heinen <johannes.heinen@gmail.com>
- */
-final class PhpParser
+namespace Ifschleife\Bundle\AutowiringBundle\Autowiring\Parser
{
- const SERVICE_ANNOTATION_TOKEN = 'Service';
+ use Ifschleife\Bundle\AutowiringBundle\Autowiring\AutowiringException;
/**
- * Parses all classes found in the given file. Must have a namespace
- * configured. Returns a list of \ReflectionClass instances.
- *
- * @todo provide a reliable shortcut for single-class files
- * @param string $filename
- * @return array $classes: An array of all found classes within the file as
- * instances of \ReflectionClass
+ * Parses a file for namespaces/use/class declarations.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Johannes Heinen <johannes.heinen@gmail.com>
*/
- public function parseFile($filename)
+ final class PhpParser
{
- $classes = array();
-
- //if(preg_match('#/\*(?:.*?)@(?:.*?)' . self::SERVICE_ANNOTATION_TOKEN . '\s*(?:\(.+?\))?(?:.*?)\*/\s*class\b#s', file_get_contents($filename), $matches))
- // {
- $src = php_strip_whitespace($filename);
+ const SERVICE_ANNOTATION_TOKEN = 'Service';
+
+ /**
+ * Parses all classes found in the given file. Must have a namespace
+ * configured. Returns a list of \ReflectionClass instances.
+ *
+ * @todo provide a reliable shortcut for single-class files
+ * @param string $filename
+ * @return array $classes: An array of all found classes within the file as
+ * instances of \ReflectionClass
+ */
+ public function parseFile($filename)
+ {
+ $classes = array();
- /**
- * Idea & Regex-Pattern derived from \Doctrine\Common\Annotations\PhpParser (@author Fabien Potencier <fabien@symfony.com>
- */
- if(preg_match_all('#\bnamespace\s+(.+?)\s*;.*?\bclass\s+(.+?)\b#s', $src, $matches))
- {
- $classes = array();
+ //if(preg_match('#/\*(?:.*?)@(?:.*?)' . self::SERVICE_ANNOTATION_TOKEN . '\s*(?:\(.+?\))?(?:.*?)\*/\s*class\b#s', file_get_contents($filename), $matches))
+ // {
+ $src = php_strip_whitespace($filename);
- for($i = 0; $i < count($matches[0]); $i++)
+ /**
+ * Idea & Regex-Pattern derived from \Doctrine\Common\Annotations\PhpParser (@author Fabien Potencier <fabien@symfony.com>
+ */
+ if(preg_match_all('#(?:(?:\bnamespace\s+(.+?)\s*(?:;|\\{).*?)?\bclass\s+(.+?)\b)+?#s', $src, $matches))
{
- $classname = $matches[1][$i] . '\\' . $matches[2][$i];
+ $classes = array();
- if( ! class_exists($classname))
+ $namespace = null;
+
+ for($i = 0; $i < count($matches[0]); $i++)
{
- require_once $filename;
- }
+ // REMEMBER PREVIOUS NAMESPACE
+ if($matches[1][$i])
+ {
+ $namespace = $matches[1][$i];
+ }
+
+ $classname = $namespace . '\\' . $matches[2][$i];
+
+ if( ! class_exists($classname))
+ {
+ require_once $filename;
+ }
- $classes[$classname] = new \ReflectionClass($classname);
+ try
+ {
+ $classes[$classname] = new \ReflectionClass($classname);
+ }
+ catch(\ReflectionException $e)
+ {
+ throw new PhpParserException(sprintf('PHP parser exception: Class "%s" in file "%s" could not be autoloaded.', $classname, realpath($filename)), null, $e);
+ }
+ }
}
- }
- // }
-
- return $classes;
+ // }
+
+ return $classes;
+ }
+ }
+
+ /**
+ * Parser Exception class
+ */
+ class PhpParserException extends AutowiringException
+ {
+
}
}
@@ -14,7 +14,7 @@
<argument type="service" id="ifschleife.autowiring.configurator" />
<tag name="kernel.listener" event="onCoreController" />
</service-->
- <service id="ifschleife.autowiring.testclass" class="Ifschleife\Bundle\AutowiringBundle\Test\Testclass"/>
- <service id="ifschleife.autowiring.testservice" class="Ifschleife\Bundle\AutowiringBundle\Test\Testservice"/>
+ <service id="ifschleife.autowiring.testclass" class="Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\Testclass"/>
+ <service id="ifschleife.autowiring.testservice" class="Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\Testservice"/>
</services>
</container>
@@ -1,32 +0,0 @@
-<?php
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-
-namespace Ifschleife\Bundle\AutowiringBundle\Test;
-
-use Ifschleife\Bundle\AutowiringBundle\Annotations\Inject;
-
-/**
- * Description of ParentTestclass
- *
- * @author joshi
- */
-class ParentTestclass
-{
- private $testservice;
-
- /**
- * @Inject
- */
- public function setTestservice(Testservice $testservice)
- {
- $this->testservice = $testservice;
- }
-
- public function getTestservice()
- {
- return $this->testservice;
- }
-}
@@ -0,0 +1,72 @@
+<?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\Autowiring\Parser;
+
+use Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures;
+use Ifschleife\Bundle\AutowiringBundle\Autowiring\Parser\PhpParser;
+
+class PhpParserTest extends \PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+
+ }
+
+ public function testParseFile()
+ {
+ $parser = new PhpParser();
+ $result = $parser->parseFile(__DIR__ . '/../../Fixtures/PhpParserTestclass0.php');
+ $this->assertInternalType('array', $result);
+ $this->assertEquals(0, count($result));
+
+ $result = $parser->parseFile(__DIR__ . '/../../Fixtures/PhpParserTestclass1.php');
+ $this->assertInternalType('array', $result);
+ $this->assertEquals(1, count($result));
+ $this->assertArrayHasKey('Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\PhpParserTestclass1', $result);
+
+ $result = $parser->parseFile(__DIR__ . '/../../Fixtures/PhpParserTestclass2.php');
+ $this->assertInternalType('array', $result);
+ $this->assertEquals(2, count($result));
+ $this->assertArrayHasKey('Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\PhpParserTestclass2', $result);
+ $this->assertArrayHasKey('Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\PhpParserTestclass2a', $result);
+
+
+ $result = $parser->parseFile(__DIR__ . '/../../Fixtures/PhpParserTestclass3.php');
+ $this->assertInternalType('array', $result);
+ $this->assertEquals(3, count($result));
+ $this->assertArrayHasKey('Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\PhpParserTestclass3', $result);
+ $this->assertArrayHasKey('Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\PhpParserTestclass3a', $result);
+ $this->assertArrayHasKey('Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\PhpParserTestclass3b', $result);
+
+ $result = $parser->parseFile(__DIR__ . '/../../Fixtures/PhpParserTestclass4.php');
+ $this->assertInternalType('array', $result);
+ $this->assertEquals(2, count($result));
+ $this->assertArrayHasKey('Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\PhpParserTestclass4', $result);
+ $this->assertArrayHasKey('Ifschleife\Bundle\AutowiringBundle\Tests\Fixtures\PhpParserTestclass4a', $result);
+ }
+
+ public function tearDown()
+ {
+
+ }
+}
@@ -21,14 +21,17 @@
* SOFTWARE.
*/
-namespace Ifschleife\Bundle\AutowiringBundle\Autowiring;
+namespace Ifschleife\Bundle\AutowiringBundle\Tests;
/**
- * DefinitionNotFoundException
+ * AutowiringTest
*
* @author joshi
*/
-class MethodCallAlreadyDefinedException extends DependencyResolverException
+class AutowiringTest extends \PHPUnit_Framework_TestCase
{
-
-}
+ function testTrue()
+ {
+ // $this->assertTrue(false, 'Not true!');
+ }
+}
Oops, something went wrong.

0 comments on commit c828cb7

Please sign in to comment.