diff --git a/composer.lock b/composer.lock index 4b68efd..8684f5c 100644 --- a/composer.lock +++ b/composer.lock @@ -11,12 +11,12 @@ "source": { "type": "git", "url": "https://github.com/stubbles/stubbles-core.git", - "reference": "d567a2b3e3fc11a6b4f09dfdb46e4b20543b5bc0" + "reference": "ba6d6963bc65faf0f18b161837641546ab54b895" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/stubbles/stubbles-core/zipball/d567a2b3e3fc11a6b4f09dfdb46e4b20543b5bc0", - "reference": "d567a2b3e3fc11a6b4f09dfdb46e4b20543b5bc0", + "url": "https://api.github.com/repos/stubbles/stubbles-core/zipball/ba6d6963bc65faf0f18b161837641546ab54b895", + "reference": "ba6d6963bc65faf0f18b161837641546ab54b895", "shasum": "" }, "require": { @@ -48,7 +48,7 @@ ], "description": "Provides base classes for Stubbles packages.", "homepage": "http://stubbles.net/", - "time": "2014-05-31 22:58:40" + "time": "2014-06-05 18:27:27" }, { "name": "stubbles/input", @@ -56,12 +56,12 @@ "source": { "type": "git", "url": "https://github.com/stubbles/stubbles-input.git", - "reference": "35cb92e75a2bfea3d80fc50d4afc1cc3d505fce9" + "reference": "afb2429e647306db8501a92f42188ad9f76d8e32" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/stubbles/stubbles-input/zipball/35cb92e75a2bfea3d80fc50d4afc1cc3d505fce9", - "reference": "35cb92e75a2bfea3d80fc50d4afc1cc3d505fce9", + "url": "https://api.github.com/repos/stubbles/stubbles-input/zipball/afb2429e647306db8501a92f42188ad9f76d8e32", + "reference": "afb2429e647306db8501a92f42188ad9f76d8e32", "shasum": "" }, "require": { @@ -93,7 +93,7 @@ ], "description": "Stubbles Input enables to handle input data via validation and filtering.", "homepage": "http://stubbles.net/", - "time": "2014-06-01 12:21:57" + "time": "2014-06-05 16:01:42" } ], "packages-dev": [ diff --git a/src/main/php/Console.php b/src/main/php/Console.php index 24045be..a9becc9 100644 --- a/src/main/php/Console.php +++ b/src/main/php/Console.php @@ -9,8 +9,8 @@ */ namespace stubbles\console; use stubbles\input\Param; -use stubbles\input\ParamErrors; use stubbles\input\ValueReader; +use stubbles\input\errors\ParamErrors; use stubbles\streams\InputStream; use stubbles\streams\OutputStream; /** diff --git a/src/main/php/creator/ClassFileCreator.php b/src/main/php/creator/ClassFileCreator.php index c3e1670..6411a90 100644 --- a/src/main/php/creator/ClassFileCreator.php +++ b/src/main/php/creator/ClassFileCreator.php @@ -21,7 +21,7 @@ class ClassFileCreator extends FileCreator public function create($className) { if (!class_exists($className)) { - $classFileName = $this->getClassFileName($className); + $classFileName = $this->fileNameforClass($className); $this->createFile($classFileName, $className, 'class.tmpl'); $this->console->writeLine('Class ' . $className . ' created at ' . $classFileName); } else { diff --git a/src/main/php/creator/ClassNameFilter.php b/src/main/php/creator/ClassNameFilter.php new file mode 100644 index 0000000..120657d --- /dev/null +++ b/src/main/php/creator/ClassNameFilter.php @@ -0,0 +1,57 @@ +isEmpty()) { + $param->addError('CLASSNAME_EMPTY'); + return null; + } + + $className = str_replace('\\\\', '\\', trim($param->value())); + if (! ((bool) preg_match('/^([a-zA-Z_]{1}[a-zA-Z0-9_\\\\]*)$/', $className))) { + $param->addError('CLASSNAME_INVALID'); + return null; + } + + if (! (bool) preg_match('/^([a-zA-Z_]{1}[a-zA-Z0-9_]*)$/', $this->nonQualifiedClassNameOf($className))) { + $param->addError('CLASSNAME_INVALID'); + return null; + } + + return $className; + } + + /** + * returns non qualified part of class name + * + * @param string $className + * @return string + */ + private function nonQualifiedClassNameOf($className) + { + return substr($className, strrpos($className, '\\') + 1); + } +} diff --git a/src/main/php/creator/ConsoleAppCreator.php b/src/main/php/creator/ConsoleAppCreator.php index 092401e..112b2af 100644 --- a/src/main/php/creator/ConsoleAppCreator.php +++ b/src/main/php/creator/ConsoleAppCreator.php @@ -1,6 +1,9 @@ console->writeLine('Stubbles ConsoleAppCreator') - ->writeLine(' (c) 2012-2014 Stubbles Development Group') - ->writeLine('') - ->writeLine('Please enter the full qualified class name for the console app: '); - $className = str_replace('\\\\', '\\', trim($this->console->readLine())); - if (!$this->isValid($className)) { - $this->console->writeLine('The class name ' . $className . ' is not a valid class name'); + $className = $this->console->writeLine('Stubbles ConsoleAppCreator') + ->writeLine(' (c) 2012-2014 Stubbles Development Group') + ->writeLine('') + ->prompt('Please enter the full qualified class name for the console app: ') + ->applyFilter(new ClassNameFilter()); + if (null === $className) { + $this->console->writeLine('The entered class name is not a valid class name'); return -10; } @@ -92,32 +95,4 @@ public function run() $this->testFile->create($className); return 0; } - - /** - * checks if given class name is valid - * - * @param string $className - * @return bool - */ - private function isValid($className) - { - if (! (bool) preg_match('/^([a-zA-Z_]{1}[a-zA-Z0-9_\\\\]*)$/', $className)) { - return false; - } - - return (bool) preg_match('/^([a-zA-Z_]{1}[a-zA-Z0-9_]*)$/', - $this->getNonQualifiedClassName($className) - ); - } - - /** - * returns non qualified part of class name - * - * @param string $className - * @return string - */ - private function getNonQualifiedClassName($className) - { - return substr($className, strrpos($className, '\\') + 1); - } } diff --git a/src/main/php/creator/FileCreator.php b/src/main/php/creator/FileCreator.php index 35c5900..33d602e 100644 --- a/src/main/php/creator/FileCreator.php +++ b/src/main/php/creator/FileCreator.php @@ -9,6 +9,9 @@ */ namespace stubbles\console\creator; use stubbles\console\Console; +use stubbles\lang\ResourceLoader; +use stubbles\lang\Rootpath; +use stubbles\lang\exception\FileNotFoundException; /** * Base class for file creation. */ @@ -23,22 +26,29 @@ abstract class FileCreator /** * path to project * - * @type string + * @type Rootpath */ - protected $projectPath; + protected $rootpath; + /** + * access to resources + * + * @type ResourceLoader + */ + private $resourceLoader; /** * constructor * - * @param Console $console - * @param string $projectPath path to project + * @param Console $console + * @param Rootpath $rootpath + * @param ResourceLoader $resourceLoader * @Inject - * @Named{projectPath}('stubbles.project.path') */ - public function __construct(Console $console, $projectPath) + public function __construct(Console $console, Rootpath $rootpath, ResourceLoader $resourceLoader) { - $this->console = $console; - $this->projectPath = $projectPath; + $this->console = $console; + $this->rootpath = $rootpath; + $this->resourceLoader = $resourceLoader; } /** @@ -55,14 +65,14 @@ public abstract function create($className); * @param string $type * @return string */ - protected function getClassFileName($className, $type = 'main') + protected function fileNameforClass($className, $type = 'main') { - return $this->projectPath - . '/src/' . $type . '/php/' - . str_replace('\\', DIRECTORY_SEPARATOR, $this->getNamespace($className)) - . DIRECTORY_SEPARATOR - . $this->getNonQualifiedClassName($className) - . '.php'; + return $this->rootpath->to( + 'src', + $type, + 'php', + str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php' + ); } /** @@ -83,21 +93,38 @@ protected function createFile($fileName, $className, $template) str_replace(['{NAMESPACE}', '{CLASS}' ], - [$this->getNamespace($className), - $this->getNonQualifiedClassName($className) + [$this->namespaceOf($className), + $this->nonQualifiedClassNameOf($className) ], - file_get_contents(__DIR__ . '/' . $template) + $this->resourceLoader->load($this->pathForTemplate($template)) ) ); } + /** + * finds absolute path for given template file + * + * @param string $template + * @return string + * @throws FileNotFoundException + */ + private function pathForTemplate($template) + { + $pathes = $this->resourceLoader->availableResourceUris('creator/' . $template); + if (!isset($pathes[0])) { + throw new FileNotFoundException($template); + } + + return $pathes[0]; + } + /** * returns namespace part of class name * * @param string $className * @return string */ - private function getNamespace($className) + private function namespaceOf($className) { return substr($className, 0, strrpos($className, '\\')); } @@ -108,7 +135,7 @@ private function getNamespace($className) * @param string $className * @return string */ - private function getNonQualifiedClassName($className) + private function nonQualifiedClassNameOf($className) { return substr($className, strrpos($className, '\\') + 1); } diff --git a/src/main/php/creator/ScriptFileCreator.php b/src/main/php/creator/ScriptFileCreator.php index 409f2b6..876d172 100644 --- a/src/main/php/creator/ScriptFileCreator.php +++ b/src/main/php/creator/ScriptFileCreator.php @@ -21,7 +21,7 @@ class ScriptFileCreator extends FileCreator public function create($className) { $this->console->writeLine('Please enter the script name for the console app: '); - $stubFileName = $this->projectPath . '/bin/' . $this->console->readLine(); + $stubFileName = $this->rootpath->to('bin', $this->console->readLine()); if (!file_exists($stubFileName)) { $this->createFile($stubFileName, $className, 'script.tmpl'); $this->console->writeLine('Script for ' . $className . ' created at ' . $stubFileName); diff --git a/src/main/php/creator/TestFileCreator.php b/src/main/php/creator/TestFileCreator.php index 50b89a9..2fac1bf 100644 --- a/src/main/php/creator/TestFileCreator.php +++ b/src/main/php/creator/TestFileCreator.php @@ -20,7 +20,7 @@ class TestFileCreator extends FileCreator */ public function create($className) { - $testClassFile = $this->getClassFileName($className . 'TestCase', 'test'); + $testClassFile = $this->fileNameforClass($className . 'Test', 'test'); if (!file_exists($testClassFile)) { $this->createFile($testClassFile, $className, 'test.tmpl'); $this->console->writeLine('Test for ' . $className . ' created at ' . $testClassFile); diff --git a/src/main/php/creator/class.tmpl b/src/main/resources/creator/class.tmpl similarity index 100% rename from src/main/php/creator/class.tmpl rename to src/main/resources/creator/class.tmpl diff --git a/src/main/php/creator/script.tmpl b/src/main/resources/creator/script.tmpl similarity index 100% rename from src/main/php/creator/script.tmpl rename to src/main/resources/creator/script.tmpl diff --git a/src/main/php/creator/test.tmpl b/src/main/resources/creator/test.tmpl similarity index 75% rename from src/main/php/creator/test.tmpl rename to src/main/resources/creator/test.tmpl index 4aa725d..a7d4290 100644 --- a/src/main/php/creator/test.tmpl +++ b/src/main/resources/creator/test.tmpl @@ -9,7 +9,7 @@ use stubbles\lang; /** * Test for {NAMESPACE}\{CLASS}. */ -class {CLASS}TestCase extends \PHPUnit_Framework_TestCase +class {CLASS}Test extends \PHPUnit_Framework_TestCase { /** * instance to test @@ -31,7 +31,9 @@ class {CLASS}TestCase extends \PHPUnit_Framework_TestCase */ public function annotationsPresentOnConstructor() { - $this->assertTrue(lang\reflectConstructor($this->instance)->hasAnnotation('Inject')); + $this->assertTrue( + lang\reflectConstructor($this->instance)->hasAnnotation('Inject') + ); } /** @@ -48,7 +50,7 @@ class {CLASS}TestCase extends \PHPUnit_Framework_TestCase public function canCreateInstance() { $this->assertInstanceOf('{NAMESPACE}\{CLASS}', - {CLASS}::create(\stubbles\lang\ResourceLoader::getRootPath()) + {CLASS}::create(new lang\Rootpath()) ); } } diff --git a/src/test/php/ConsoleTest.php b/src/test/php/ConsoleTest.php index caad169..dd22bb0 100644 --- a/src/test/php/ConsoleTest.php +++ b/src/test/php/ConsoleTest.php @@ -8,7 +8,7 @@ * @package stubbles\console */ namespace stubbles\console; -use stubbles\input\ParamErrors; +use stubbles\input\errors\ParamErrors; use stubbles\lang; /** * Test for stubbles\console\Console. diff --git a/src/test/php/creator/ClassFileCreatorTest.php b/src/test/php/creator/ClassFileCreatorTest.php index 5970a1b..6cc9f43 100644 --- a/src/test/php/creator/ClassFileCreatorTest.php +++ b/src/test/php/creator/ClassFileCreatorTest.php @@ -9,6 +9,8 @@ */ namespace stubbles\console\creator; use org\bovigo\vfs\vfsStream; +use stubbles\lang\ResourceLoader; +use stubbles\lang\Rootpath; /** * Test for stubbles\console\creator\ClassFileCreator. * @@ -31,20 +33,20 @@ class ClassFileCreatorTest extends \PHPUnit_Framework_TestCase /** * root directory * - * @type \org\bovigo\vfs\vfsStreamDirectory + * @type Rootpath */ - private $root; + private $rootpath; /** * set up test environment */ public function setUp() { - $this->root = vfsStream::setup(); + $this->rootpath = new Rootpath(vfsStream::setup()->url()); $this->mockConsole = $this->getMockBuilder('stubbles\console\Console') ->disableOriginalConstructor() ->getMock(); - $this->classFileCreator = new ClassFileCreator($this->mockConsole, vfsStream::url('root')); + $this->classFileCreator = new ClassFileCreator($this->mockConsole, $this->rootpath, new ResourceLoader()); } /** @@ -54,10 +56,11 @@ public function createsClassIfDoesNotExist() { $this->mockConsole->expects($this->once()) ->method('writeLine') - ->with($this->equalTo('Class example\console\ExampleConsoleApp created at ' . vfsStream::url('root/src/main/php/example/console/ExampleConsoleApp.php'))); + ->with($this->equalTo('Class example\console\ExampleConsoleApp created at ' . $this->rootpath->to('src/main/php/example/console/ExampleConsoleApp.php'))); $this->classFileCreator->create('example\console\ExampleConsoleApp'); - $this->assertTrue($this->root->hasChild('src/main/php/example/console/ExampleConsoleApp.php')); - $this->assertEquals('assertTrue(file_exists($this->rootpath->to('src/main/php/example/console/ExampleConsoleApp.php'))); + $this->assertEquals( + 'root->getChild('src/main/php/example/console/ExampleConsoleApp.php') - ->getContent() + file_get_contents($this->rootpath->to('src/main/php/example/console/ExampleConsoleApp.php')) ); } @@ -122,6 +124,6 @@ public function skipsClassCreationIfClassAlreadyExists() ->method('writeLine') ->with($this->equalTo('Class stubbles\console\creator\ClassFileCreator already exists, skipped creating the class')); $this->classFileCreator->create('stubbles\console\creator\ClassFileCreator'); - $this->assertFalse($this->root->hasChild('src/main/php/stubbles/console/creator/ClassFileCreator.php')); + $this->assertFalse(file_exists($this->rootpath->to('src/main/php/stubbles/console/creator/ClassFileCreator.php'))); } } diff --git a/src/test/php/creator/ClassNameFilterTest.php b/src/test/php/creator/ClassNameFilterTest.php new file mode 100644 index 0000000..8a40559 --- /dev/null +++ b/src/test/php/creator/ClassNameFilterTest.php @@ -0,0 +1,120 @@ +classNameFilter = new ClassNameFilter(); + } + + /** + * @return array + */ + public function emptyParamValues() + { + return [[null], ['']]; + } + + /** + * @param string $value + * @test + * @dataProvider emptyParamValues + */ + public function returnsNullWhenParamValueIsEmpty($value) + { + $this->assertNull( + $this->classNameFilter->apply(new Param('stdin', $value)) + ); + } + + /** + * @param string $value + * @test + * @dataProvider emptyParamValues + */ + public function addsErrorToParamWhenParamValueIsEmpty($value) + { + $param = new Param('stdin', $value); + $this->classNameFilter->apply($param); + $this->assertTrue($param->hasError('CLASSNAME_EMPTY')); + } + + /** + * @return array + */ + public function invalidParamValues() + { + return [['500'], ['foo\500']]; + } + + /** + * @param string $value + * @test + * @dataProvider invalidParamValues + */ + public function returnsNullWhenParamValueHasInvalidSyntax($value) + { + $this->assertNull( + $this->classNameFilter->apply(new Param('stdin', $value)) + ); + } + + /** + * @param string $value + * @test + * @dataProvider invalidParamValues + */ + public function addsErrorToParamWhenParamValueHasInvalidSyntax($value) + { + $param = new Param('stdin', $value); + $this->classNameFilter->apply($param); + $this->assertTrue($param->hasError('CLASSNAME_INVALID')); + } + + /** + * @test + */ + public function trimsInputValue() + { + $this->assertEquals( + 'foo\bar\Baz', + $this->classNameFilter->apply(new Param('stdin', ' foo\bar\Baz ')) + ); + } + + /** + * @test + */ + public function fixesQuotedNamespaceSeparator() + { + $this->assertEquals( + 'foo\bar\Baz', + $this->classNameFilter->apply(new Param('stdin', 'foo\\\\bar\\\\Baz')) + ); + } +} diff --git a/src/test/php/creator/ConsoleAppCreatorTest.php b/src/test/php/creator/ConsoleAppCreatorTest.php index 0ee2d13..ef86c09 100644 --- a/src/test/php/creator/ConsoleAppCreatorTest.php +++ b/src/test/php/creator/ConsoleAppCreatorTest.php @@ -5,6 +5,7 @@ * @package stubbles\console */ namespace stubbles\console\creator; +use stubbles\input\ValueReader; use stubbles\lang; /** * Test for stubbles\console\creator\ConsoleAppCreator. @@ -84,26 +85,9 @@ public function annotationsPresentOnConstructor() */ public function doesNotCreateClassWhenClassNameIsInvalid() { - $this->mockConsole->expects(($this->any())) - ->method('readLine') - ->will($this->returnValue('500')); - $this->mockClassFile->expects($this->never()) - ->method('create'); - $this->mockScriptFile->expects($this->never()) - ->method('create'); - $this->mockTestFile->expects($this->never()) - ->method('create'); - $this->assertEquals(-10, $this->consoleAppCreator->run()); - } - - /** - * @test - */ - public function doesNotCreateNonQualifiedPartOfClassWhenClassNameIsInvalid() - { - $this->mockConsole->expects(($this->any())) - ->method('readLine') - ->will($this->returnValue('foo\500')); + $this->mockConsole->expects(($this->once())) + ->method('prompt') + ->will($this->returnValue(ValueReader::forValue(null))); $this->mockClassFile->expects($this->never()) ->method('create'); $this->mockScriptFile->expects($this->never()) @@ -118,9 +102,9 @@ public function doesNotCreateNonQualifiedPartOfClassWhenClassNameIsInvalid() */ public function returnsExitCodeZeroOnSuccess() { - $this->mockConsole->expects(($this->any())) - ->method('readLine') - ->will($this->returnValue('foo\bar\Example')); + $this->mockConsole->expects(($this->once())) + ->method('prompt') + ->will($this->returnValue(ValueReader::forValue('foo\\bar\\Example'))); $this->mockClassFile->expects($this->once()) ->method('create') ->with($this->equalTo('foo\bar\Example')); @@ -133,53 +117,13 @@ public function returnsExitCodeZeroOnSuccess() $this->assertEquals(0, $this->consoleAppCreator->run()); } - /** - * @test - */ - public function trimsInputClassName() - { - $this->mockConsole->expects(($this->any())) - ->method('readLine') - ->will($this->returnValue(' foo\bar\Example ')); - $this->mockClassFile->expects($this->once()) - ->method('create') - ->with($this->equalTo('foo\bar\Example')); - $this->mockScriptFile->expects($this->once()) - ->method('create') - ->with($this->equalTo('foo\bar\Example')); - $this->mockTestFile->expects($this->once()) - ->method('create') - ->with($this->equalTo('foo\bar\Example')); - $this->assertEquals(0, $this->consoleAppCreator->run()); - } - - /** - * @test - */ - public function fixesQuotedNamespaceSeparator() - { - $this->mockConsole->expects(($this->any())) - ->method('readLine') - ->will($this->returnValue('foo\\\\bar\\\\Example')); - $this->mockClassFile->expects($this->once()) - ->method('create') - ->with($this->equalTo('foo\\bar\\Example')); - $this->mockScriptFile->expects($this->once()) - ->method('create') - ->with($this->equalTo('foo\\bar\\Example')); - $this->mockTestFile->expects($this->once()) - ->method('create') - ->with($this->equalTo('foo\\bar\\Example')); - $this->assertEquals(0, $this->consoleAppCreator->run()); - } - /** * @test */ public function canCreateInstance() { $this->assertInstanceOf('stubbles\console\creator\ConsoleAppCreator', - ConsoleAppCreator::create(\stubbles\lang\ResourceLoader::getRootPath()) + ConsoleAppCreator::create(new lang\Rootpath()) ); } } diff --git a/src/test/php/creator/ScriptFileCreatorTest.php b/src/test/php/creator/ScriptFileCreatorTest.php index 7cf95a1..74b4645 100644 --- a/src/test/php/creator/ScriptFileCreatorTest.php +++ b/src/test/php/creator/ScriptFileCreatorTest.php @@ -9,6 +9,8 @@ */ namespace stubbles\console\creator; use org\bovigo\vfs\vfsStream; +use stubbles\lang\ResourceLoader; +use stubbles\lang\Rootpath; /** * Test for stubbles\console\creator\ScriptFileCreator. * @@ -31,20 +33,20 @@ class ScriptFileCreatorTest extends \PHPUnit_Framework_TestCase /** * root directory * - * @type \org\bovigo\vfs\vfsStreamDirectory + * @type Rootpath */ - private $root; + private $rootpath; /** * set up test environment */ public function setUp() { - $this->root = vfsStream::setup(); + $this->rootpath = new Rootpath(vfsStream::setup()->url()); $this->mockConsole = $this->getMockBuilder('stubbles\console\Console') ->disableOriginalConstructor() ->getMock(); - $this->scriptFileCreator = new ScriptFileCreator($this->mockConsole, vfsStream::url('root')); + $this->scriptFileCreator = new ScriptFileCreator($this->mockConsole, $this->rootpath, new ResourceLoader()); } /** @@ -57,10 +59,11 @@ public function createsScriptIfDoesNotExist() ->will($this->returnValue('example')); $this->mockConsole->expects($this->at(2)) ->method('writeLine') - ->with($this->equalTo('Script for example\console\ExampleConsoleApp created at ' . vfsStream::url('root/bin/example'))); + ->with($this->equalTo('Script for example\console\ExampleConsoleApp created at ' . $this->rootpath->to('bin/example'))); $this->scriptFileCreator->create('example\console\ExampleConsoleApp'); - $this->assertTrue($this->root->hasChild('bin/example')); - $this->assertEquals('#!/usr/bin/php + $this->assertTrue(file_exists($this->rootpath->to('bin/example'))); + $this->assertEquals( + '#!/usr/bin/php root->getChild('bin/example') - ->getContent() + file_get_contents($this->rootpath->to('bin/example')) ); } @@ -92,18 +94,15 @@ public function createsScriptIfDoesNotExist() */ public function skipsScriptCreationIfTestAlreadyExists() { - vfsStream::newDirectory('bin') - ->at($this->root); - $testFile = vfsStream::newFile('example') - ->withContent('foo') - ->at($this->root->getChild('bin')); + mkdir($this->rootpath->to('bin')); + file_put_contents($this->rootpath->to('bin/example'), 'foo'); $this->mockConsole->expects($this->at(1)) ->method('readLine') ->will($this->returnValue('example')); $this->mockConsole->expects($this->at(2)) ->method('writeLine') - ->with($this->equalTo('Script for org\stubbles\console\scripts\creator\TestFileCreator already exists, skipped creating the script')); - $this->scriptFileCreator->create('org\stubbles\console\scripts\creator\TestFileCreator'); - $this->assertEquals('foo', $testFile->getContent()); + ->with($this->equalTo('Script for example\console\ExampleConsoleApp already exists, skipped creating the script')); + $this->scriptFileCreator->create('example\console\ExampleConsoleApp'); + $this->assertEquals('foo', file_get_contents($this->rootpath->to('bin/example'))); } } diff --git a/src/test/php/creator/TestFileCreatorTest.php b/src/test/php/creator/TestFileCreatorTest.php index e0de029..e0f796f 100644 --- a/src/test/php/creator/TestFileCreatorTest.php +++ b/src/test/php/creator/TestFileCreatorTest.php @@ -9,6 +9,8 @@ */ namespace stubbles\console\creator; use org\bovigo\vfs\vfsStream; +use stubbles\lang\ResourceLoader; +use stubbles\lang\Rootpath; /** * Test for stubbles\console\creator\TestFileCreator. * @@ -31,20 +33,20 @@ class TestFileCreatorTest extends \PHPUnit_Framework_TestCase /** * root directory * - * @type \org\bovigo\vfs\vfsStreamDirectory + * @type Rootpath */ - private $root; + private $rootpath; /** * set up test environment */ public function setUp() { - $this->root = vfsStream::setup(); + $this->rootpath = new Rootpath(vfsStream::setup()->url()); $this->mockConsole = $this->getMockBuilder('stubbles\console\Console') ->disableOriginalConstructor() ->getMock(); - $this->testFileCreator = new TestFileCreator($this->mockConsole, vfsStream::url('root')); + $this->testFileCreator = new TestFileCreator($this->mockConsole, $this->rootpath, new ResourceLoader()); } /** @@ -54,10 +56,11 @@ public function createsTestIfDoesNotExist() { $this->mockConsole->expects($this->once()) ->method('writeLine') - ->with($this->equalTo('Test for example\console\ExampleConsoleApp created at ' . vfsStream::url('root/src/test/php/example/console/ExampleConsoleAppTestCase.php'))); + ->with($this->equalTo('Test for example\console\ExampleConsoleApp created at ' . $this->rootpath->to('src/test/php/example/console/ExampleConsoleAppTest.php'))); $this->testFileCreator->create('example\console\ExampleConsoleApp'); - $this->assertTrue($this->root->hasChild('src/test/php/example/console/ExampleConsoleAppTestCase.php')); - $this->assertEquals('assertTrue(file_exists($this->rootpath->to('src/test/php/example/console/ExampleConsoleAppTest.php'))); + $this->assertEquals( + 'assertTrue(lang\reflectConstructor($this->instance)->hasAnnotation(\'Inject\')); + $this->assertTrue( + lang\reflectConstructor($this->instance)->hasAnnotation(\'Inject\') + ); } /** @@ -107,13 +112,12 @@ public function returnsExitCode0() public function canCreateInstance() { $this->assertInstanceOf(\'example\console\ExampleConsoleApp\', - ExampleConsoleApp::create(\stubbles\lang\ResourceLoader::getRootPath()) + ExampleConsoleApp::create(new lang\Rootpath()) ); } } ', - $this->root->getChild('src/test/php/example/console/ExampleConsoleAppTestCase.php') - ->getContent() + file_get_contents($this->rootpath->to('src/test/php/example/console/ExampleConsoleAppTest.php')) ); } @@ -122,15 +126,17 @@ public function canCreateInstance() */ public function skipsTestCreationIfTestAlreadyExists() { - vfsStream::newDirectory('src/test/php/org/stubbles/console/scripts/creator') - ->at($this->root); - $testFile = vfsStream::newFile('TestFileCreatorTestCase.php') - ->withContent('foo') - ->at($this->root->getChild('src/test/php/org/stubbles/console/scripts/creator')); + mkdir($this->rootpath->to('src/test/php/example/console'), 0755, true); + file_put_contents($this->rootpath->to('src/test/php/example/console/ExampleConsoleAppTest.php'), 'foo'); $this->mockConsole->expects($this->once()) ->method('writeLine') - ->with($this->equalTo('Test for org\stubbles\console\scripts\creator\TestFileCreator already exists, skipped creating the test')); - $this->testFileCreator->create('org\stubbles\console\scripts\creator\TestFileCreator'); - $this->assertEquals('foo', $testFile->getContent()); + ->with($this->equalTo('Test for example\console\ExampleConsoleApp already exists, skipped creating the test')); + $this->testFileCreator->create('example\console\ExampleConsoleApp'); + $this->assertEquals( + 'foo', + file_get_contents( + $this->rootpath->to('src/test/php/example/console/ExampleConsoleAppTest.php') + ) + ); } }