Permalink
Browse files

tests

  • Loading branch information...
1 parent 86733fb commit d861ebc96019d539307388ff2471383fa95d4982 @fprochazka fprochazka committed Aug 18, 2012
@@ -1,5 +1,7 @@
<?php
+
+
/**
* Converts #attr(foo) into getter and setter method
*
@@ -1,5 +1,9 @@
<?php
+namespace GettersExample;
+
+
+
/**
* Movie model class
*
@@ -0,0 +1,40 @@
+<?php
+
+namespace GettersExample;
+
+
+
+/**
+ * @author Filip Procházka <filip.prochazka@kdyby.org>
+ */
+class RunTest extends \Tests\TestCase
+{
+
+ public function setUp()
+ {
+ require_once __DIR__ . '/GettersEnhancer.php';
+ \Enhancer\EnhancerStream::$enhancer = new \GettersEnhancer();
+ $this->enhancerAutoload(__DIR__, 'GettersExample');
+ }
+
+
+
+ public function test()
+ {
+ $movie = new Movie("The Dark Night Returns", 10);
+
+ // methods generated by enhancer
+ $this->assertSame("The Dark Night Returns", $movie->getName());
+ $this->assertSame(10, $movie->getRating());
+
+ // methods are real
+ $this->assertSame(array(
+ 'getName',
+ 'setName',
+ 'getRating',
+ 'setRating',
+ '__construct'
+ ), get_class_methods($movie));
+ }
+
+}
@@ -1,22 +0,0 @@
-<?php
-require_once __DIR__ . '/../bootstrap.php';
-
-// Setup enhancer
-require_once __DIR__ . '/GettersEnhancer.php';
-\Enhancer\EnhancerStream::$enhancer = new GettersEnhancer;
-
-
-// Register autoloader
-$loader = new \Enhancer\Loaders\ClassLoader;
-$loader->register(true);
-$loader->add('', __DIR__ . '/classes');
-
-
-/***************** tests *****************j*d*/
-
-
-$movie = new Movie("The Dark Night Returns", 10);
-dump($movie->getName(), $movie->getRating()); // methods generated by enhancer
-
-$refl = new ReflectionClass('Movie'); // the methods are real
-dump($refl->getMethods());
@@ -17,8 +17,11 @@ public function enhance($code)
while (($token = $parser->fetch()) !== FALSE) {
if ($parser->isCurrent(T_NAMESPACE)) {
$namespace = (string) $parser->fetchAll(T_STRING, T_NS_SEPARATOR);
+ $s .= $token . ' ' . $namespace;
if ($parser->fetch(';', '{') === '{') {
$s .= '{';
+ } else {
+ $s .= ';';
}
} elseif ($parser->isCurrent(T_USE)) {
@@ -68,3 +71,28 @@ public function enhance($code)
}
}
+
+
+
+/**
+ * @author Jan Dolecek <juzna.cz@gmail.com>
+ */
+class ClassHookHelper
+{
+
+ public static function newInstance($className /*, $args */)
+ {
+ $args = func_get_args();
+ array_shift($args);
+ $cnt = count($args);
+
+ echo "LOG: Creating instance of '$className' with $cnt arguments\n";
+
+ $refl = new ReflectionClass($className);
+ $ret = $refl->newInstanceArgs($args);
+
+ echo "LOG: ... done\n";
+
+ return $ret;
+ }
+}
@@ -1,5 +1,9 @@
<?php
+namespace ClassHookExample;
+
+
+
class Movie
{
public $name;
@@ -8,7 +12,6 @@ class Movie
public function __construct($name, $rating)
{
- echo "in Movie constructor\n";
$this->name = $name;
$this->rating = $rating;
}
@@ -1,5 +1,8 @@
<?php
+namespace ClassHookExample;
+
+
class MovieFactory
{
@@ -0,0 +1,39 @@
+<?php
+
+namespace ClassHookExample;
+
+
+
+/**
+ * @author Filip Procházka <filip.prochazka@kdyby.org>
+ */
+class RunTest extends \Tests\TestCase
+{
+
+ public function setUp()
+ {
+ require_once __DIR__ . '/ClassHookEnhancer.php';
+ \Enhancer\EnhancerStream::$enhancer = new \ClassHookEnhancer();
+ $this->enhancerAutoload(__DIR__ , 'ClassHookExample');
+ }
+
+
+
+ public function test()
+ {
+ ob_start();
+ $movie = MovieFactory::createMovie("The Dark Night Returns", 10);
+ $log = ob_get_clean();
+
+ $this->assertInstanceOf('ClassHookExample\Movie', $movie);
+ $this->assertSame(
+ "LOG: Creating instance of 'ClassHookExample\\Movie' with 2 arguments\n" .
+ "LOG: ... done\n",
+ $log
+ );
+
+ $this->assertSame("The Dark Night Returns", $movie->name);
+ $this->assertSame(10, $movie->rating);
+ }
+
+}
@@ -1,44 +0,0 @@
-<?php
-require_once __DIR__ . '/../bootstrap.php';
-
-// Setup enhancer
-require_once __DIR__ . '/ClassHookEnhancer.php';
-\Enhancer\EnhancerStream::$enhancer = new ClassHookEnhancer;
-
-
-// Register autoloader
-$loader = new \Enhancer\Loaders\ClassLoader;
-$loader->register(true);
-$loader->add('', __DIR__ . '/classes');
-
-// echo file_get_contents("enhance://" . __DIR__ . '/classes/MovieFactory.php');
-
-
-/***************** hooks *****************j*d*/
-
-
-class ClassHookHelper
-{
- public static function newInstance($className /*, $args */)
- {
- $args = func_get_args();
- array_shift($args);
- $cnt = count($args);
-
- echo "LOG: Creating instance of '$className' with $cnt arguments\n";
-
- $refl = new ReflectionClass($className);
- $ret = $refl->newInstanceArgs($args);
-
- echo "LOG: ... done\n";
-
- return $ret;
- }
-}
-
-
-/***************** tests *****************j*d*/
-
-
-$movie = MovieFactory::createMovie("The Dark Night Returns", 10);
-dump($movie);
@@ -0,0 +1,28 @@
+<?php
+
+namespace GenericsExample;
+
+
+
+/**
+ * @author Filip Procházka <filip.prochazka@kdyby.org>
+ */
+class RunTest extends \Tests\TestCase
+{
+
+ public function setUp()
+ {
+ require_once __DIR__ . '/GenericsEnhancer.php';
+ \Enhancer\EnhancerStream::$enhancer = new \GenericsEnhancer();
+ $this->enhancerAutoload(__DIR__ . '/classes', 'GenericsExample');
+ }
+
+
+
+ /***/
+ public function test()
+ {
+ $this->fail();
+ }
+
+}
View
@@ -3,6 +3,7 @@
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = require_once __DIR__ . '/../vendor/autoload.php';
$loader->add('Enhancer', __DIR__ . '/../src/');
+$loader->add('Tests', __DIR__ . '/../src/');
// allow dump()
Nette\Diagnostics\Debugger::$strictMode = TRUE;
View
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<phpunit
+ stopOnError="false"
+ stopOnFailure="false"
+ stopOnIncomplete="false"
+ stopOnSkipped="false"
+
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+
+ bootstrap="examples/bootstrap.php"
+ colors="true"
+ >
+ <testsuites>
+ <testsuite name="PHP Enhancer Test Suite">
+ <directory suffix="Test.php">./examples</directory>
+ </testsuite>
+ </testsuites>
+
+ <filter>
+ <blacklist>
+ <directory suffix=".php">./vendor</directory>
+ </blacklist>
+ <whitelist addUncoveredFilesFromWhitelist="true">
+ <directory suffix=".php">./examples</directory>
+ <directory suffix=".php">./src</directory>
+ </whitelist>
+ </filter>
+</phpunit>
View
@@ -0,0 +1,43 @@
+<?php
+
+namespace Tests;
+
+use Nette;
+
+
+
+/**
+ * @author Filip Procházka <filip.prochazka@kdyby.org>
+ */
+class TestCase extends \PHPUnit_Framework_TestCase
+{
+
+ /**
+ * @var \Enhancer\Loaders\ClassLoader
+ */
+ private $loader;
+
+
+
+ /**
+ * @param $dir
+ * @param string $namespace
+ */
+ protected function enhancerAutoload($dir, $namespace = '')
+ {
+ // Register autoloader
+ $this->loader = new \Enhancer\Loaders\ClassLoader;
+ $this->loader->register(true);
+ $this->loader->add($namespace, $dir);
+ }
+
+
+
+ public function tearDown()
+ {
+ if ($this->loader) {
+ $this->loader->unregister();
+ }
+ }
+
+}

0 comments on commit d861ebc

Please sign in to comment.