当前位置:网站首页>Partial mock of static class of phpunit operation
Partial mock of static class of phpunit operation
2022-06-29 17:44:00 【fangdong88】
Know from official documents and practices , Classes with static methods, if needed mock, Use prefix alias: , However, there is a problem that the method of the class itself cannot be called later , So by reading the source code, you can use the following methods to achieve :
1. establish Helpers Class to encapsulate
<?php
use Mockery;
use Mockery\Generator\MockConfigurationBuilder;
class Helpers
{
/**
* Mock static class, you can call the original method and mock the method
* mock Static class , You can call the original method , Again mock Method
* @param string $class
* @param string $cloneClassNameSuffix
* @return Mockery\Mock
* @throws \Exception
*/
public static function mockAliasPartial($class, $classRealPath, $cloneClassNameSuffix = 'UnitOrigin') {
$sourceCode = file_get_contents($classRealPath);
preg_match('/class\s+([A-Z]\w+)/', $sourceCode, $match);
$className = $match[1] ?? '';
if (empty($className)) {
throw new \Exception('get class name failed!');
}
$cloneSourceCode = preg_replace('/class\s+([A-Z]\w+)/', 'class ' . $className . $cloneClassNameSuffix, $sourceCode, 1);
eval("?>" . $cloneSourceCode);
$builder = new MockConfigurationBuilder();
$builder->setInstanceMock(true);
$builder->setName($class);
$target = '\\' . $class . $cloneClassNameSuffix;
return Mockery::mock($target, $builder)->makePartial();
}
}2. Create static classes
class Test
{
public static function getName($name) {
if (empty($name)) {
return 'default_name';
}
return $name;
}
}3. test run
<?php
use PHPUnit\Framework\TestCase;
class UniTest extends TestCase
{
public function testAliasPartial() {
$mock = Helpers::mockAliasPartial(Test::class, './Test.php');
$actual1 = Test::getName('');
echo PHP_EOL;
$mock->shouldReceive('avatar_small')->andReturn('fake name');
$actual2 = Service::getName('');
$this->assertEquals('default_name', $actual1);
$this->assertEquals('fake name', $actual2);
}
}边栏推荐
猜你喜欢

Digital twin energy system, creating a "perspective" in the low-carbon era

关于日期相加减问题

mysql.sock的概念是什么

Visio标注、批注位置

位图的详细介绍及模拟实现

The soft youth under the blessing of devcloud makes education "smart" in the cloud

基于gis三维可视化的智慧城市行业运用

Industry application of smart city based on GIS 3D visualization

Maidong Internet won the bid of Dajia Insurance Group

MATLAB 最远点采样(FPS)
随机推荐
R语言使用自定义函数编写深度学习Leaky ReLU激活函数、并可视化Leaky ReLU激活函数
基于注解和拦截器防止表单重复提交
L'intercepteur handlerinterceptor personnalisé permet l'authentification de l'utilisateur
Master slave replication of MySQL
C language practice ---- pointer string and linked list
使用 SSH 方式拉取代码
mysql如何查询表的字符集编码
R语言使用自定义函数编写深度学习线性激活函数、并可视化线性激活函数
The aggregate function in the epidisplay package of R language divides numerical variables into different subsets based on factor variables, and calculates the summary statistics and aggregate data. W
[Oracle] basic knowledge interview questions
分布式 | 几步快速拥有读写分离
剑指 Offer 13. 机器人的运动范围 (BFS)
力扣今日题-535. TinyURL 的加密与解密
软件快速交付真的需要以安全为代价吗?
自定義HandlerInterceptor攔截器實現用戶鑒權
Selenium key combination operation
R language uses GLM of mass package The Nb function establishes the negative binomial generalized linear model, and the summary function obtains the summary statistical information of the negative bin
Visual studio plug-in coderush officially released v22.1 -- visual tool for optimizing debugging
Does MySQL support foreign keys
Digital twin energy system, creating a "perspective" in the low-carbon era