我使用 Symfony 的 KernelTestCase 编写单元测试,并且必须测试功能,该功能仅在当天的某个时间(特别早或特别晚)发生。
因此,当我让测试运行一个中午时,当然什么也没有发生。我如何伪造我的系统时间来假装它有不同的时间并且我的测试用例被触发。
我尝试使用 Symfony 的 ClockMock 类,但它不起作用。 https://symfony.com/doc/current/components/phpunit_bridge.html#clock-mocking
这是我的测试代码:
use Symfony\Bridge\PhpUnit\ClockMock;
use \DateTime;
/**
* testUserAchievedEarlyBirdTrophy
* @group time-sensitive
*/
public function testUserAchievedEarlyBirdTrophy()
{
ClockMock::withClockMock(strtotime('2018-11-05 01:00:00'));
echo (new DateTime())->format('Y-m-d H:m:s');
$user8Id = $this->user8->getId();
$progressSaveRequest = new ProgressSaveRequest($user8Id, $this->content_1_1->getId());
$this->progressService->saveProgress($progressSaveRequest);
$this->assertTrue($this->loggerCreator->hasDebugThatContains(
'Early Bird'
));
}
Run Code Online (Sandbox Code Playgroud)
回声给我今天的日期:2019-02-01 16:02:06
我也有一种感觉,ClockMock 更适合用来跳过时间,例如测试缓存而不是 sleep()。
我究竟做错了什么?
侦听器配置位于我的 phpunit.xml 中,调用 bin/simple-phpunit 会导致发生大量安装。
我不能使用普通的 phpunit 吗?
还有其他选项可以伪造一天中的时间吗?
我需要在我的 symfony5 项目中模拟 S3Client,以便能够引发异常并测试我的代码对这些异常的反应。我们使用 aws/aws-sdk-php 版本 3.*
\n7 年前,有人遇到了同样的问题,我尝试遵循这个解决方案 [ PHPUnit - Mock S3Client not running well ],但出现错误。
\n我现在所做的:
\n在我的服务中:
\nclass AwsS3Service implements AwsS3ServiceInterface\n{\n private S3Client $s3Client;\n ....\n\n public function __construct(ParameterBagInterface $params)\n {\n [ ... ]\n\n $this->s3Client = $this->getS3client();\n }\n\n public function getS3client(): S3Client\n {\n return (new Sdk($this->config))->createS3();\n }\n\nRun Code Online (Sandbox Code Playgroud)\n所以我有一个公共方法,我可以模拟它并使其返回一个模拟的 S3Client。
\n在我的测试中我执行以下操作:
\n $this->awsS3Service = $this->createMock(AwsS3ServiceInterface::class);\n\n $command = $this->createMock(Command::class);\n /** @var S3Client $s3Client */\n $s3Client = $this->createMock(S3Client::class);\n\n $s3Client->method(\'listObjectsV2\')\n ->willThrowException(new S3Exception(\'VALIDATION ERROR\', $command));\n $s3Client->method(\'putObject\')\n ->willThrowException(new …Run Code Online (Sandbox Code Playgroud) 我有一组太多的域,其常见文件,如我在中央服务器上的javascript文件.
现在我有这个设置:
在域A上的页面项上单击时,将在中央服务器上执行脚本,该服务器调用域A上的Web服务.
所以我想如果我的脚本将在域A上,一切都会正常工作(除了我必须在我的60个域上有大约60个我的脚本副本).但我希望我的网络服务永远不会被调用,因为我在这里遇到了CORS问题.
所以:
"A"从"Central"加载脚本执行它,"A"的Web服务由放置在"Central"中的脚本调用.
这是需要CORS的情况吗?或者我的错误可能在其他地方(或两者......但那是我的问题)
我正在安装新的kubuntu系统18.04,默认情况下使用的是php7.2
我当前软件项目的作曲家安装给我以下错误消息:
您的系统缺少所需的PHP扩展名ext-mcrypt *。安装或启用PHP的mcrypt扩展。
除了以前没有可用的php-mcrypt之外,其他类似http://aryo.lecture.ub.ac.id/easy-install-php-mcrypt-extension-on-ubuntu-linux/的文章也无济于事模拟php-mcrypt或php7-mcrypt不存在。
如何获得可以满足我的要求的设置?
sudo apt-get install php7.2-ext-mcrypt
Run Code Online (Sandbox Code Playgroud)
找不到安装候选对象。
php 7.1和7.2之间有区别吗?
毫不奇怪,这也不能解决问题:
$ sudo phpenmod mcrypt
WARNING: Module mcrypt ini file doesn't exist under /etc/php/7.2/mods-available
WARNING: Module mcrypt ini file doesn't exist under /etc/php/7.2/mods-available
Run Code Online (Sandbox Code Playgroud) 我尝试在 Synfony5 应用程序中配置两个缓存池以使用特定命名空间并为项目设置默认到期日期。在尝试了无数次第无数次变化之后,我感觉我的配置正在循环。
到目前为止我的理解是: 在RedisAdapter 的 构造函数中,您可以设置命名空间和默认过期时间。在 createConnection 方法中,您可以设置 Redis 服务器的 url。
然而RedisAdapter的构造函数似乎已经需要一个redis客户端(= redis连接?)RedisAdapter:
/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient The redis client
* @param string $namespace The default namespace
* @param int $defaultLifetime The default lifetime
*/
public function __construct($redisClient, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
{
$this->init($redisClient, $namespace, $defaultLifetime, $marshaller);
}
Run Code Online (Sandbox Code Playgroud)
如何将命名空间和 defaultLifetimes 注入 RedisAdapter?
到目前为止我尝试过的:cache.yaml:
framework:
cache:
pools:
cache.sap:
adapter: cache.adapter.redis
provider: app.service.puc_sap_redis_adapter
cache.pers:
adapter: cache.adapter.redis
provider: app.service.puc_pers_redis_adapter
Run Code Online (Sandbox Code Playgroud)
服务.yaml:
app.my_redis_adapter: …Run Code Online (Sandbox Code Playgroud)