我正在使用Phactory和PHPUnit为PHP Propel项目设置测试套件.我目前正在尝试对发出外部请求的函数进行单元测试,并且我希望在该请求的模拟响应中存根.
这是我试图测试的类的片段:
class Endpoint {
...
public function parseThirdPartyResponse() {
$response = $this->fetchUrl("www.example.com/api.xml");
// do stuff and return
...
}
public function fetchUrl($url) {
return file_get_contents($url);
}
...
Run Code Online (Sandbox Code Playgroud)
这是我试图写的测试功能.
// my factory, defined in a seperate file
Phactory::define('endpoint', array('identifier' => 'endpoint_$n');
// a test case in my endpoint_test file
public function testParseThirdPartyResponse() {
$phEndpoint = Phactory::create('endpoint', $options);
$endpoint = new EndpointQuery()::create()->findPK($phEndpoint->id);
$stub = $this->getMock('Endpoint');
$xml = "...<target>test_target</target>..."; // sample response from third party api …Run Code Online (Sandbox Code Playgroud) 部分测试科目:
class AddOptionsProviderArgumentPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if(!$container->hasDefinition('gremo_highcharts')) {
return;
}
if(!$container->hasParameter('gremo_highcharts.options_provider')) {
return;
}
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
我想断言:
hasDefinition() 使用参数'gremo_highcharts'调用将返回 falseprocess()返回,即不会调用其他方法一种解决方案是断言后续调用hasParameter():
public function testProcessWillReturnIfThereIsNoServiceDefinition()
{
$container = $this->getMockedContainerBuilder();
$pass = new AddOptionsProviderArgumentPass();
$container->expects($this->once())
->method('hasDefinition')
->with($this->equalTo('gremo_highcharts'))
->will($this->returnValue(false));
// Expects that hasParameter() is never invoked
$container->expects($this->never())
->method('hasParameter');
$pass->process($container);
}
Run Code Online (Sandbox Code Playgroud)
但它似乎不是一个优雅的解决方案.
首先,标题会暗示这是这个或这个的重复,但由于几个原因,这些答案对我不起作用,即使我最初的问题是相同的.我会解释原因.
我的问题是:我在我的代码中有几次我想发送标题和正文然后终止处理.与其他问题不同,我不能使用return或抛出异常(这些显然是为出于不同目的设计的不同函数而不是退出,这不是错误;它只是在某些特定情况下的早期运行时终止).
仍然,我想编写运行这些方法的单元测试,确保设置了适当的头文件(此处找到解决方案),输出主体是正确的($this->expectOutputString()在测试用例中使用-method 解决),然后继续测试.在这之间,exit意志会发生.
我已经@runInSeparateProcess在PHPUnit中尝试了-annotation,我也检查了test_helpers扩展,它有效,但我不想添加另一个扩展(建议测试也将在生产中运行)本机PHP代码打破了一切.必须有一种更简单的方法而不牺牲最佳实践.
有没有人有这个问题的好方法?
我正在使用phpunit和Symfony2.
我决定使用sqlite进行测试.
我遇到的问题是外键约束被忽略.
我知道我必须执行以下查询才能使用外键:) PRAGMA foreign_keys = ON.
我的问题是:有没有办法在使用sqlite创建数据库模式时始终使用外键?
谢谢 !
我想知道如何在我正在测试的模块的功能测试中导入配置同步文件。例如,我想测试一些自定义内容类型,并且 config/sync 中有许多文件与定义自定义内容类型的节点模块有关。
class ArticleControllerTest extends BrowserTestBase {
protected static $modules = ['node', 'dist_source'];
}
Run Code Online (Sandbox Code Playgroud)
在测试的顶部,我定义了成功导入的模块,但它不包括配置同步设置,因此我的自定义内容类型都不存在。如何将这些导入到我的测试环境中?
phpunit drupal functional-testing configuration-management drupal-8
我正在为 Laravel 应用程序编写测试,特别是针对在控制台上写入大量日志消息的进程。
例如
Log::info('Process starts', [
'process_name' => 'product_import',
'data' => // a huge text containing json_encode of the given message object
]
Run Code Online (Sandbox Code Playgroud)
当我运行 phpunit 时,我在控制台上看到所有这些烦人的日志消息。有没有办法禁用或以某种方式停止这些日志消息?
我正在尝试测试我的异常,或 PHP 单元中的任何其他异常。
<?php declare(strict_types=1);
namespace Tests\Exception;
use PHPUnit\Framework\TestCase;
class DrinkIsInvalidExceptionTest extends TestCase
{
public function testIsExceptionThrown(): void
{
$this->expectException(\Exception::class);
try {
throw new \Exception('Wrong exception');
} catch(\Exception $exception) {
echo $exception->getCode();
}
}
}
Run Code Online (Sandbox Code Playgroud)
仍然失败:
Failed asserting that exception of type "Exception" is thrown.
Run Code Online (Sandbox Code Playgroud)
可能是什么问题呢?
我刚刚升级到phpunit 7.5.20,phpunit 9.5.0遇到了很多错误(实际上是好的错误),但不能 100% 确定如何解决其中一些错误。只是寻找一些想法来修复以下错误:
Method setDummyStuff may not return value of type NULL, its return declaration is "void"
仅当您创建方法createConfiguredMock()并将null方法作为参数传递时,才会发生这种情况。
这是我的测试:
<?php
use Lib\IDummyCode;
class DummyTest extends PHPUnit\Framework\TestCase
{
public function setUp(): void
{
parent::setUp();
}
public function testDummyThatReturnsVoid()
{
$this->createConfiguredMock(IDummyCode::class, [
'setDummyStuff' => null
]);
}
}
Run Code Online (Sandbox Code Playgroud)
这是虚拟类:
<?php
namespace Lib;
interface IDummyCode
{
public function setDummyStuff(
int $testInt,
string $testString
): void;
}
Run Code Online (Sandbox Code Playgroud)
你们有关于如何改进这一点的想法吗?多谢!
我的 phpunit 容器有这个 dockerfile:
FROM php:8.1-fpm-alpine
WORKDIR /var/www/html
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/ --allow-untrusted gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php
ENV PHP_MEMORY_LIMIT=1G
ENV PHP_UPLOAD_MAX_FILESIZE: 512M
ENV PHP_POST_MAX_SIZE: 512M
RUN docker-php-ext-install pdo
RUN apk add --no-cache libpng libpng-dev && docker-php-ext-install gd && apk del libpng-dev
RUN apk update \
&& apk upgrade \
&& apk add --no-cache \
freetype \
libpng \
libjpeg-turbo \
freetype-dev \
libpng-dev \
jpeg-dev \
libwebp-dev \
libjpeg \
libjpeg-turbo-dev
RUN docker-php-ext-configure gd \
--with-freetype=/usr/lib/ \ …Run Code Online (Sandbox Code Playgroud)