From dd6987ff029a294cb5ce8f5a9af07d4d090894ec Mon Sep 17 00:00:00 2001 From: Beanow Date: Sat, 11 Oct 2014 00:13:59 +0200 Subject: [PATCH 1/3] This is an example of autoloading classes and including unit tests. --- .gitignore | 3 +- Makefile | 2 ++ autoload.php | 41 ++++++++++++++++++++++++++++ index.php | 1 + src/Example/Hello.php | 10 +++++++ tests/phpunit.xml | 12 ++++++++ tests/unit/src/Example/HelloTest.php | 23 ++++++++++++++++ 7 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100644 autoload.php create mode 100644 src/Example/Hello.php create mode 100644 tests/phpunit.xml create mode 100644 tests/unit/src/Example/HelloTest.php diff --git a/.gitignore b/.gitignore index 19ce2922..11eaf823 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ .htconfig.php #* favicon.* - - +tests/coverage.html diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..3cf4a7c0 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +test: + cd tests && phpunit --coverage-html=coverage.html && x-www-browser ./coverage.html/index.html \ No newline at end of file diff --git a/autoload.php b/autoload.php new file mode 100644 index 00000000..0ac5f982 --- /dev/null +++ b/autoload.php @@ -0,0 +1,41 @@ + array( + __DIR__ . '/src', + __DIR__ . '/tests/unit/src', + ), + ); + + // go through the prefixes + foreach ($prefixes as $prefix => $dirs) { + + // does the requested class match the namespace prefix? + $prefix_len = strlen($prefix); + if (substr($class, 0, $prefix_len) !== $prefix) { + continue; + } + + // strip the prefix off the class + $class = substr($class, $prefix_len); + + // a partial filename + $part = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php'; + + // go through the directories to find classes + foreach ($dirs as $dir) { + $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir); + $file = $dir . DIRECTORY_SEPARATOR . $part; + if (is_readable($file)) { + require $file; + return; + } + } + } + +}); diff --git a/index.php b/index.php index e6fc910c..464afaed 100755 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@ + + + unit/src + + + + + ../src + + + diff --git a/tests/unit/src/Example/HelloTest.php b/tests/unit/src/Example/HelloTest.php new file mode 100644 index 00000000..3e6c06a0 --- /dev/null +++ b/tests/unit/src/Example/HelloTest.php @@ -0,0 +1,23 @@ +sayHello(); + + //Check that it returns the message we expect. + $this->assertEquals("Hello world!", $output); + + } + +} \ No newline at end of file From 9bafa316d4d63c7c65ea16d7282c18fefb01e7a8 Mon Sep 17 00:00:00 2001 From: Beanow Date: Sat, 11 Oct 2014 00:24:17 +0200 Subject: [PATCH 2/3] Add example code for psr4. --- example.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 example.php diff --git a/example.php b/example.php new file mode 100644 index 00000000..7f423fa5 --- /dev/null +++ b/example.php @@ -0,0 +1,8 @@ +sayHello(); From 8cc61e0c68e554b67b67403ecbfb3d9a9cb6e3ad Mon Sep 17 00:00:00 2001 From: Beanow Date: Sat, 11 Oct 2014 00:37:41 +0200 Subject: [PATCH 3/3] Add some code comments to the example. --- example.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/example.php b/example.php index 7f423fa5..6f37fe3f 100644 --- a/example.php +++ b/example.php @@ -1,8 +1,14 @@ sayHello();