我正在编写一个我可以在一些项目中重用的简单库。目前,它只是将一些“哑”字符交换为智能等效字符。无论如何,我安装了一个新的laravel 4实例然后运行php artisan workbench jonnybarnes/dumbquotes
。然后我在DumbquotesParser.php
中写了一些代码。目前位于LARAVEL/workbench/jonnybarnes/dumbquotes/src/Jonnybarnes/Dumbquotes/DumbquotesParser.php
。所以我在提供的tests文件夹中为此创建了一个测试文件:
<?php
require __DIR__.'/../src/Jonnybarnes/Dumbquotes/DumbquotesParser.php';
class DumbquotesParserTest extends PHPUnit_Framework_TestCase {
public function testApostrophe()
{
$actual = DumbquotesParser::apostrophe('We\'re testing Jonny\'s apotrophes\' form');
$expected = 'We’re testing Jonny’s apostrophes’ form';
$this->assertEquals($actual, $excpected);
}
}
然而它不起作用。我不完全确定如何让phpunit加载解析器。我得到以下输出:
jonny@jonny-arch:~/dumbquotes-bench/workbench/jonnybarnes/dumbquotes|testing ⇒ ./vendor/bin/phpunit tests
PHPUnit 3.7.21-2-g268d9f1 by Sebastian Bergmann.
Configuration read from /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/phpunit.xml
PHP Fatal error: Class 'DumbquotesParser' not found in /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/tests/DumbquotesParserTest.php on line 8
PHP Stack trace:
PHP 1. {main}() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/composer/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/composer/bin/phpunit:63
PHP 3. PHPUnit_TextUI_Command->run() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:349
PHP 6. PHPUnit_Framework_TestSuite->run() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:705
PHP 7. PHPUnit_Framework_TestSuite->runTest() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745
PHP 8. PHPUnit_Framework_TestCase->run() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775
PHP 9. PHPUnit_Framework_TestResult->run() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:776
PHP 10. PHPUnit_Framework_TestCase->runBare() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:648
PHP 11. PHPUnit_Framework_TestCase->runTest() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:831
PHP 12. ReflectionMethod->invokeArgs() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:976
PHP 13. DumbquotesParserTest->testApostrophe() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:976
Fatal error: Class 'DumbquotesParser' not found in /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/tests/DumbquotesParserTest.php on line 8
Call Stack:
0.0002 231432 1. {main}() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/composer/bin/phpunit:0
0.0036 609112 2. PHPUnit_TextUI_Command::main() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/composer/bin/phpunit:63
0.0036 609344 3. PHPUnit_TextUI_Command->run() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:129
0.0220 2346712 4. PHPUnit_TextUI_TestRunner->doRun() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:176
0.0474 2638864 5. PHPUnit_Framework_TestSuite->run() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:349
0.0476 2640512 6. PHPUnit_Framework_TestSuite->run() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:705
0.0477 2641112 7. PHPUnit_Framework_TestSuite->runTest() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745
0.0477 2641144 8. PHPUnit_Framework_TestCase->run() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775
0.0478 2642352 9. PHPUnit_Framework_TestResult->run() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:776
0.0510 2700216 10. PHPUnit_Framework_TestCase->runBare() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:648
0.0515 2719488 11. PHPUnit_Framework_TestCase->runTest() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:831
0.0515 2720768 12. ReflectionMethod->invokeArgs() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:976
0.0515 2720800 13. DumbquotesParserTest->testApostrophe() /home/jonny/dumbquotes-bench/workbench/jonnybarnes/dumbquotes/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:976
那么如何实际加载解析器?
更改我的测试文件使测试工作:
<?php
require __DIR__.'/../src/Jonnybarnes/Dumbquotes/DumbquotesParser.php';
class DumbquotesParserTest extends PHPUnit_Framework_TestCase {
public function testApostrophe()
{
$parser = new \Jonnybarnes\Dumbquotes\DumbquotesParser();
$actual = $parser->apostrophe('We\'re testing Jonny\'s apostrophes\' form');
$expected = 'We’re testing Jonny’s apostrophes’ form';
$this->assertEquals($actual, $expected);
}
}
我是否需要在每次测试中加入$parser = new \...\...\...();
?
答案 0 :(得分:0)
您也可以将命名空间类导入到全局命名空间中(就在include之后):
use Jonnybarnes\Dumbquotes\DumbquotesParser;
然后您可以忘记setUp()
方法(因为您只是在这里测试一个静态方法)并保留您最初发布的实际测试代码:
$actual = DumbquotesParser::apostrophe('We\'re testing Jonny\'s apotrophes\' form');