Skip to content
Browse files

intial testsuite commit

  • Loading branch information...
1 parent ca9a218 commit 7fba4223fe7c8d35e6c330261b88f085f311bfa9 @lapistano committed
View
3 .gitignore
@@ -1,3 +1,4 @@
.buildpath
.project
-.settings/
+.settings/
+vendor/
View
76 accepted/tests/Psr0_CompatibilityTest.php
@@ -0,0 +1,76 @@
+<?php
+/**
+ * Test case containing test to verify PSR-0 copatibility of a projext.
+ *
+ * @author Bastian Feder <lapistano@php.net>
+ * @copyright 2011 by Bastian Feder
+ *
+ */
+namespace figStandards\PSR0\Compatibility;
+
+require __DIR__ . '/Psr0_Scanner.php';
+
+class psr0_CompatibilityTest extends \PHPUnit_Framework_TestCase
+{
+
+ /**
+ * Translates a classname to a filename according to the rules of PSR-0.
+ *
+ * This method is a clone of the autoload() function accepted to be the reference
+ * implementation to autoload classes following the PSR-0 FIG standard.
+ *
+ * @param string $className
+ * @return string
+ * @link https://github.com/php-fig/fig-standards
+ */
+ protected function translateClassToFilename($className)
+ {
+ $className = ltrim($className, '\\');
+ $fileName = '';
+ $namespace = '';
+ if ($lastNsPos = strripos($className, '\\')) {
+ $namespace = substr($className, 0, $lastNsPos);
+ $className = substr($className, $lastNsPos + 1);
+ $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
+ }
+ $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
+ return $fileName;
+ }
+
+
+ /**
+ * @dataProvider psr0CompatibilityDataprovider
+ */
+ public function testPsr0Compatability($filename, $classname)
+ {
+ $pattern = "(^.*?(" . Psr0_ProjectRootNamespace . ".*)$)";
+ $filename = preg_replace($pattern, '$1', $filename, 1);
+
+ $this->assertEquals(
+ $filename,
+ $this->translateClassToFilename($classname),
+ 'The classname does not translate correctly into a PSR-0 compatible filename.'
+ );
+ }
+
+
+/*************************************************************************/
+/* Dataprovider */
+/*************************************************************************/
+
+ /**
+ * This data provider gathers all filenames, classnames, and namespaces within the configured directory.
+ *
+ * @return array
+ */
+ public static function psr0CompatibilityDataprovider()
+ {
+ // preparations
+ $psr0 = new Psr0_Scanner(
+ Psr0_ScannerInclude,
+ Psr0_ScannerExclude
+ );
+
+ return $psr0->scan(Psr0_ScannerStartDir);
+ }
+}
View
139 accepted/tests/Psr0_Scanner.php
@@ -0,0 +1,139 @@
+<?php
+/**
+ *
+ *
+ * @author Bastian Feder <lapistano@php.net>
+ * @copyright 2011 by Bastian Feder
+ *
+ */
+
+namespace figStandards\PSR0\Compatibility;
+
+class Psr0_Scanner
+{
+ protected $includes = '';
+ protected $excludes = '';
+ protected $registry = array();
+ protected $errors = array();
+
+ public function __construct($includes, $excludes)
+ {
+ $this->includes = $includes;
+ $this->excludes = $excludes;
+ }
+
+ /**
+ *
+ *
+ * @param unknown_type $dir
+ */
+ public function scan($dir)
+ {
+ $scanner = $this->initScanner();
+ $files = $scanner($dir);
+ $classes = array();
+
+ foreach ($files as $file) {
+ $this->parseFile($file);
+ }
+
+ // $this->registry now has all information about used NS and classnames
+ foreach ($this->registry as $namespace => $classInfo) {
+ foreach ($classInfo as $class) {
+ $classname = $namespace . '\\' . $class['classname'];
+ $classes[$classname] = array(
+ 'filename' => $class['filename'],
+ 'classname' => $classname
+ );
+ }
+ }
+
+ return $classes;
+
+ }
+
+ /**
+ * Provides the list of errors accord during the parsing.
+ *
+ * @return array
+ */
+ public function getErrors()
+ {
+ return $this->errors;
+ }
+
+
+ /**
+ * Greps namespaces and classnames from the given file
+ *
+ * @param SplFileInfo $file
+ */
+ protected function parseFile($file)
+ {
+ if (!$file->isReadable()) {
+ $this->errors['NotReadable'][] = $file->getPathname();
+ return;
+ }
+ $content = file($file->getPathname());
+
+ foreach ($content as $line) {
+ // find namespace declarations
+ preg_match('(^\s*?namespace\s*([^;{]*))', $line, $matches);
+ if (isset($matches[1])) {
+ $namespace = $matches[1];
+ if (!isset($this->registry[$namespace])) {
+ $this->registry[$namespace] = array();
+ }
+ }
+
+ // find class declarations
+ preg_match('(^\s*?class\s+([^\s]+)(?:extends|implements)?\s+[^{]*)m', $line, $matches);
+ if (isset($matches[1])) {
+ $this->registry[$namespace][] = array(
+ 'classname' => $matches[1],
+ 'filename' => $file->getPathname()
+ );
+ }
+ }
+ }
+
+ /**
+ * Initializes the directory scanner.
+ *
+ * @return DirectoryScanner
+ */
+ protected function initScanner()
+ {
+ require __DIR__ . '/vendor/TheSeer/DirectoryScanner/autoload.php';
+
+ $scanner = new \TheSeer\DirectoryScanner\DirectoryScanner;
+ $this->registerExclusions($scanner);
+ $this->registerInclusions($scanner);
+
+ return $scanner;
+ }
+
+ /**
+ * Registers preset files/directories to be ignored when scanning the directory structure.
+ *
+ * @param DirectoryScanner $scanner
+ */
+ protected function registerExclusions(\TheSeer\DirectoryScanner\DirectoryScanner $scanner) {
+ if (!empty($this->excludes)) {
+ $exclusions = explode(":", $this->excludes);
+ $scanner->setExcludes($exclusions);
+ }
+ }
+
+ /**
+ * Registers preset files/directories to be recognized when scanning the directory structure.
+ *
+ * @param DirectoryScanner $scanner
+ */
+ protected function registerInclusions(\TheSeer\DirectoryScanner\DirectoryScanner $scanner) {
+ if (!empty($this->includes)) {
+ $inclusions = explode(":", $this->includes);
+ $scanner->setIncludes($inclusions);
+ }
+ }
+}
View
0 accepted/tests/README.md
No changes.
View
4 accepted/tests/install.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# used to crawl the dir structure
+git clone git://github.com/theseer/DirectoryScanner.git vendor/TheSeer/DirectoryScanner
View
35 accepted/tests/phpunit.xml.dist
@@ -0,0 +1,35 @@
+<phpunit strict="true"
+ verbose="true"
+>
+
+ <!-- PSR-0 test configuration
+
+ * Psr0_ProjectRootNamespace
+ Root namespace of your project (e.g. Liip)..
+
+ * Psr0_ScannerStartDir
+ Absolute or relative path to your source files (e.g. ../src/).
+ See https://github.com/theseer/DirectoryScanner for further information.
+
+ * Psr0_ScannerInclude
+ Colon separated set of patterns which directories/files are to be recognized when scanning.
+ See https://github.com/theseer/DirectoryScanner for further information.
+
+ * Psr0_ScannerExclude
+ Colon separated set of patterns which directories/files are to be ignored when scanning.
+ You usually want to exclude the directories containing the tests and 3rd party source probably located in a
+ vendor directory. This combines into the string shown in the floowing example:
+
+ */vendor/*:*/Test/*
+
+ See https://github.com/theseer/DirectoryScanner for further information.
+
+ -->
+ <php>
+ <const name="Psr0_ProjectRootNamespace" value="PROJECT_ROOT_NAMESPACE"/>
+ <const name="Psr0_ScannerStartDir" value="../src"/>
+ <const name="Psr0_ScannerInclude" value="*.php"/>
+ <const name="Psr0_ScannerExclude" value="*/vendor/*:*/Test/*"/>
+ </php>
+
+</phpunit>

0 comments on commit 7fba422

Please sign in to comment.
Something went wrong with that request. Please try again.