Browse files

Tests and component-specific focus

- Focus on components for dependency list
- Added unit test suite
  • Loading branch information...
1 parent d6966fe commit 7dbd16d9f32b71a0c1b971ea4d8db480f77be927 @weierophinney committed Nov 30, 2010
View
54 zf-utils/library/Zf/Util/Dependencies.php
@@ -11,6 +11,7 @@
* - Iterate tokenizer results
* - For each T_USE, parse until ';' reached
* - Iterate tokens captured; all contiguous T_STRING + T_NS_SEPARATOR tokens are considered classes/namespaces
+ * - Remove any dependencies matching the namespace in the file
*/
namespace Zf\Util;
@@ -77,7 +78,7 @@ protected static function parse(array $tokens)
}
if ($insideUse && ($token === ';')) {
if ($insideAlias && !empty($currentAlias)) {
- $dependencies[] = $currentAlias;
+ $dependencies[] = trim($currentAlias, '\\');
}
$insideUse = false;
$insideAlias = false;
@@ -90,7 +91,7 @@ protected static function parse(array $tokens)
}
if ($insideAlias) {
if (!empty($currentAlias)) {
- $dependencies[] = $currentAlias;
+ $dependencies[] = trim($currentAlias, '\\');
}
$insideAlias = false;
$currentAlias = '';
@@ -126,35 +127,52 @@ protected static function parse(array $tokens)
default:
if ($insideAlias) {
if (!empty($currentAlias)) {
- $dependencies[] = $currentAlias;
+ $dependencies[] = trim($currentAlias, '\\');
}
$insideAlias = false;
$currentAlias = '';
}
}
}
+ // Normalize dependencies
+ // We only want:
+ // - non-empty dependencies
+ // - component-level dependencies
+ foreach ($dependencies as $k => $v) {
+ // Empty? remove
+ if (empty($v)) {
+ unset($dependencies[$k]);
+ }
+
+ $segments = explode('\\', $v);
+ // only 2-segments or less? done, as we have a component-level
+ // namespace
+ if (2 >= count($segments)) {
+ continue;
+ }
+
+ // Otherwise, reset by concatenating the first two segments
+ $dependencies[$k] = $segments[0] . '\\' . $segments[1];
+ }
+
+ // Return early if we don't have a namespace, or if we didn't find
+ // any dependencies
if (empty($namespace) || empty($dependencies)) {
return $dependencies;
}
// Remove dependencies that reference the same component
- $namespaceLength = strlen($namespace);
+ // First, get the component-level namespace
+ $namespaceSegments = explode('\\', $namespace);
+ if (2 < count($namespaceSegments)) {
+ $namespace = $namespaceSegments[0] . '\\' . $namespaceSegments[1];
+ }
+
+ // Next, loop through the dependencies to see if any match this
+ // component namespace
foreach ($dependencies as $index => $dep) {
- if (strlen($dep) < $namespaceLength) {
- // Check if it references a parent component.
- // The check for the NS separator is to ensure we're not
- // looking at a top-level NS.
- if (strstr($dep, '\\')) {
- if (substr($namespace, 0, strlen($dep)) == $dep) {
- // Matches; remove from index
- unset($dependencies[$index]);
- }
- }
- continue;
- }
- if (substr($dep, 0, $namespaceLength) == $namespace) {
- // Matches this component
+ if ($dep == $namespace) {
unset($dependencies[$index]);
}
}
View
56 zf-utils/tests/Bootstrap.php
@@ -0,0 +1,56 @@
+<?php
+/*
+ * E_STRICT compliance
+ */
+error_reporting( E_ALL | E_STRICT );
+
+/*
+ * Determine the root, library, and tests directories
+ */
+$root = realpath(dirname(__DIR__));
+$coreLibrary = "$root/library";
+$coreTests = "$root/tests";
+
+/*
+ * Prepend the library/ and tests/ directories to the include_path. This allows
+ * the tests to run out of the box and helps prevent loading other copies that
+ * might supersede this copy.
+ */
+$path = array(
+ $coreLibrary,
+ $coreTests,
+ get_include_path(),
+);
+set_include_path(implode(PATH_SEPARATOR, $path));
+
+/**
+ * Setup autoloading
+ */
+include __DIR__ . '/_autoload.php';
+
+if (defined('TESTS_GENERATE_REPORT') && TESTS_GENERATE_REPORT === true &&
+ version_compare(PHPUnit_Runner_Version::id(), '3.1.6', '>=')) {
+
+ /*
+ * Add library/ directory to the PHPUnit code coverage appear in the code
+ * coverage report and that all production code source whitelist. This has
+ * the effect that only production code source files files, even those that
+ * are not covered by a test yet, are processed.
+ */
+ PHPUnit_Util_Filter::addDirectoryToWhitelist($coreLibrary);
+
+ /*
+ * Omit from code coverage reports the contents of the tests directory
+ */
+ foreach (array('.php', '.phtml', '.csv', '.inc') as $suffix) {
+ PHPUnit_Util_Filter::addDirectoryToFilter($coreTests, $suffix);
+ }
+ PHPUnit_Util_Filter::addDirectoryToFilter(PEAR_INSTALL_DIR);
+ PHPUnit_Util_Filter::addDirectoryToFilter(PHP_LIBDIR);
+}
+
+
+/*
+ * Unset global variables that are no longer needed.
+ */
+unset($root, $coreLibrary, $coreTests, $path);
View
66 zf-utils/tests/Zf/Util/Dependencies.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace Zf\Util;
+
+class DependenciesTest extends \PHPUnit_Framework_TestCase
+{
+ public function testRaisesExceptionIfCannotFindFile()
+ {
+ $this->setExpectedException('InvalidArgumentException');
+ Dependencies::getForFile('/I/do/not/exist.php');
+ }
+
+ public function testCanParseDependenciesForFilesOnIncludePath()
+ {
+ set_include_path(implode(PATH_SEPARATOR, array(
+ '.',
+ __DIR__ . '/_files',
+ get_include_path(),
+ )));
+ $deps = Dependencies::getForFile('TestCase1.php');
+ $this->assertEquals(array('Foo\Bar'), $deps);
+ }
+
+ public function testDependenciesAreReturnedAsIsWhenNoNamespaceDeclared()
+ {
+ $deps = Dependencies::getForFile(__DIR__ . '/_files/TestCase2.php');
+ $expected = array(
+ 'Foo\Bar',
+ 'Bar\Baz',
+ 'Baz\Bat',
+ );
+ $this->assertEquals($expected, $deps);
+ }
+
+ public function testDependencyListOmitsThoseInSameComponentNamespace()
+ {
+ $deps = Dependencies::getForFile(__DIR__ . '/_files/TestCase3.php');
+ $expected = array(
+ 'Foo\Bar',
+ 'Baz\Bat',
+ );
+ $this->assertEquals(count($expected), count($deps));
+ foreach ($expected as $class) {
+ $this->assertContains($class, $deps);
+ }
+ }
+
+ public function testReturnsEmptyArrayIfNoDependenciesFound()
+ {
+ $deps = Dependencies::getForFile(__DIR__ . '/_files/TestCase4.php');
+ $this->assertEquals(array(), $deps);
+ }
+
+ public function testImportAliasesAreIgnored()
+ {
+ $deps = Dependencies::getForFile(__DIR__ . '/_files/TestCase5.php');
+ $expected = array(
+ 'Foo\Bar',
+ 'Baz\Bat',
+ );
+ $this->assertEquals(count($expected), count($deps));
+ foreach ($expected as $class) {
+ $this->assertContains($class, $deps);
+ }
+ }
+}
View
9 zf-utils/tests/Zf/Util/_files/TestCase1.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace Test\Case;
+
+use Foo\Bar\Baz;
+
+class TestCase1
+{
+}
View
5 zf-utils/tests/Zf/Util/_files/TestCase2.php
@@ -0,0 +1,5 @@
+<?php
+
+use Foo\Bar\Baz,
+ Bar\Baz\Bat;
+use Baz\Bat;
View
6 zf-utils/tests/Zf/Util/_files/TestCase3.php
@@ -0,0 +1,6 @@
+<?php
+namespace Bar\Baz\Beef;
+
+use Foo\Bar\Baz,
+ Bar\Baz\Bat;
+use Baz\Bat;
View
3 zf-utils/tests/Zf/Util/_files/TestCase4.php
@@ -0,0 +1,3 @@
+<?php
+namespace Bar\Baz\Beef;
+
View
7 zf-utils/tests/Zf/Util/_files/TestCase5.php
@@ -0,0 +1,7 @@
+<?php
+namespace Bar\Baz\Beef;
+
+use Foo\Bar\Baz as FooBar,
+ Bar\Baz\Bat as BazBat;
+use Baz\Bat as Bazzie;
+
View
8 zf-utils/tests/_autoload.php
@@ -0,0 +1,8 @@
+<?php
+/**
+ * Setup autoloading
+ */
+spl_autoload_register(function ($class) {
+ $class = ltrim($class, '\\');
+ return include str_replace(array('\\', '_'), DIRECTORY_SEPARATOR, $class) . '.php';
+}, true, true);
View
17 zf-utils/tests/phpunit.xml
@@ -0,0 +1,17 @@
+<phpunit bootstrap="./Bootstrap.php" colors="true">
+ <testsuite name="ZF-Utils Test Suite">
+ <directory>./</directory>
+ </testsuite>
+
+ <groups>
+ <exclude>
+ <group>disable</group>
+ </exclude>
+ </groups>
+
+ <filter>
+ <whitelist>
+ <directory suffix=".php">../library/</directory>
+ </whitelist>
+ </filter>
+</phpunit>

0 comments on commit 7dbd16d

Please sign in to comment.