是否有可以与我的Web应用程序捆绑的PHPUnit的可移植版本?我希望能够在任何服务器上使用phpunit,同时避免使用PEAR(版本冲突,破坏其他托管应用程序等)的问题.
当我phpunit在命令行上运行时,我得到了'phpunit' is not recognized as an internal or external command, operable program or batch file.
php已被添加到我的系统路径中.我的php.ini包括include_path=".;C:\PHP\pear".
如何通过命令行识别命令phpunit?
我正在设计一个有两个依赖项的类.其中一个依赖类已经编写和测试.另一个还没写.
它发生在我身上,因为剩余的依赖关系将被编写以方便将要使用它的类,我应该首先编写后者,然后设计前者的接口,学习它应该做什么.
在我看来,这是制作代码的好方法.毕竟,只要主类在其构造函数中得到一个mock,我就可以编写它并在不知道它的依赖性不存在的情况下测试它,然后我可以创建依赖项,一旦我确定我知道我需要什么.
那么:我该怎么做?创建一个我修改的骨架类.也许是这样的:
class NonExistantSkeleton
{
public function requiredMethod1()
{
}
public function newlyDiscoveredRequirement()
{
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用PHPUnit模拟它,并设置存根等,以保持我的课程开发愉快?
这是要走的路吗?
这似乎是开发代码的好方法 - 在我看来比开发依赖更有意义,而不确定它将如何被使用.
我目前正在尝试获取基于Yii框架的PHP应用程序的代码覆盖率报告.
代码覆盖率由PHPUnit 3.6生成,我使用白名单方法来源文件过滤.
问题是,当我设置选项时addUncoveredFilesFromWhitelist="true",代码覆盖中断时出现以下错误:
Generating code coverage report, this may take a moment.PHP Warning: include(CButtonColumn.php): failed to open stream: No such file or directory in /home/hijarian/systems/yii/framework/YiiBase.php on line 418
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:125
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:187
PHP 5. PHP_CodeCoverage_Report_HTML->process() /usr/share/php/PHPUnit/TextUI/TestRunner.php:373
PHP 6. PHP_CodeCoverage->getReport() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:133
PHP 7. PHP_CodeCoverage_Report_Factory->create() /usr/share/php/PHP/CodeCoverage.php:141
PHP 8. PHP_CodeCoverage->getData() /usr/share/php/PHP/CodeCoverage/Report/Factory.php:65
PHP 9. PHP_CodeCoverage->processUncoveredFilesFromWhitelist() /usr/share/php/PHP/CodeCoverage.php:173
PHP 10. include_once() /usr/share/php/PHP/CodeCoverage.php:516 …Run Code Online (Sandbox Code Playgroud) 我正在为使用Symfony2(2.1)完成的应用程序做一些功能测试,我遇到了问题.
当用户单击链接或其他元素时,我会加载网站的某些部分,但这些操作是使用jQuery和$ .post调用执行的.如何让Symfony2抓取工具进行这些调用?
当我做这样的事情时:
$link = $crawler->filter('ul.line_menu a')->eq(1)->link();
$crawler = $client->click($link);
Run Code Online (Sandbox Code Playgroud)
爬虫获取"a"元素的"href"并启动它,但"href"为空,并且"click()"函数与此元素相关联,从而阻止了使用"preventDefault()"的单击操作.
谢谢大家!!:)
我的其中一条路线中有以下代码:
return Response::download('cv.pdf');
知道如何测试这个吗?我试过使用 shouldReceive() 但这似乎不起作用('shouldReceive() 未定义函数......')。
我刚刚烘焙了一些Fixtures和一些TestCases,每当我运行时,vendor/bin/phpunit我得到以下错误的版本:
$ vendor/bin/phpunit
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
IException: Unable to insert fixtures for "App\Test\TestCase\Controller\ScreensControllerTest" test case. SQLSTATE[HY000] [2002] No such file or directory in [/Applications/MAMP/htdocs/myapp/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureManager.php, line 254]
Run Code Online (Sandbox Code Playgroud)
使用烘焙工具自动烘烤固定装置和测试.我检查了$fixtures属性中的每个项目对应于一个文件tests/Fixture/.如果我$fixtures在任何测试类中注释掉声明,则错误只会移到下一个类.因此,在任何特定的夹具/测试中,它的语法都不错.
PHP 7.4 和 PHPUnit 9
使用 PHPUnit 主页示例 ( https://phpunit.de/getting-started/phpunit-9.html ):
private function ensureIsValidEmail(string $email): void
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException(
sprintf(
'"%s" is not a valid email address',
$email
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
主页还向我们展示了如何使用expectException()方法来测试抛出的异常:
public function testCannotBeCreatedFromInvalidEmailAddress(): void
{
$this->expectException(InvalidArgumentException::class);
Email::fromString('invalid');
}
Run Code Online (Sandbox Code Playgroud)
那太棒了。但是,如果我想测试在给定有效输入的情况下是否抛出异常怎么办?
查看文档(https://phpunit.readthedocs.io/en/9.3/writing-tests-for-phpunit.html#testing-exceptions)似乎没有提到逆向方法expectException() ?
我应该如何处理这个问题?
编辑添加:
为了完全清楚地表明,我正在测试一个场景,即不会Email::fromString('valid.email@example.com');引发异常。
我的类中有一个方法(getUsers()),我想模拟它,但我的类中有一个构造函数。在模拟我的类时如何将值传递给构造函数?
class MyNotifications {
/**
* Date time.
*
* @var DateTime|mixed|null
*/
public $date;
public function __construct($date = NULL)
{
if (!$date) {
$date = new \DateTime();
}
$this->date = $date;
}
/**
* Get users.
*
* @param int $node_id
* Node id.
*
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getUsers($node_id)
{
// API code goes here.
}
/**
* Get day.
*
* @return false|int|string
*/
public function getDay()
{
return $this->date->format('d'); …Run Code Online (Sandbox Code Playgroud) 使用 Symfony 5.2 和 PhpUnit 最新版本,我的功能测试出现此错误
LogicException: You cannot create the client used in functional tests if the "framework.test" config is not set to true.
Run Code Online (Sandbox Code Playgroud)
内容config/packages/test/framework.yaml:
framework:
test: true
session:
storage_id: session.storage.mock_file
Run Code Online (Sandbox Code Playgroud)
我的 PHP 部分phpunit.xml.dist:
<php>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
<server name="SHELL_VERBOSITY" value="-1"/>
<env name="BOOTSTRAP_CLEAR_CACHE_ENV" value="test"/>
</php>
Run Code Online (Sandbox Code Playgroud)
这是我的测试内容:
class JWTTokenAuthenticatorTest extends WebTestCase
{
public function test_authentication_token()
{
$client = static::createClient();
$client->request(
'POST',
'/authentication_token',
[],
[],
['CONTENT_TYPE' => 'application/json'],
'{"email":"api@api.com","password":"api"}'
);
$this->assertResponseIsSuccessful();
$this->assertMatchesRegularExpression('/{"token":"(.*)"}/i', $client->getResponse()->getContent()); …Run Code Online (Sandbox Code Playgroud) phpunit ×10
php ×7
laravel ×2
symfony ×2
unit-testing ×2
cakephp ×1
cakephp-3.0 ×1
class-design ×1
jquery ×1
laravel-4 ×1
mockery ×1
web-crawler ×1
windows ×1
windows-7 ×1
yii ×1