当前位置:网站首页>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);
}
}边栏推荐
猜你喜欢
Master slave replication of MySQL

mysql如何查询表的字符集编码

mysql支持外键吗

Industry application of smart city based on GIS 3D visualization
![Split palindrome string [dp + DFS combination]](/img/7b/221b000984977508f849e19802c2c2.png)
Split palindrome string [dp + DFS combination]

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

小程序容器是什么技术?能助力物联网企业红海突围?
![分割回文串[dp + dfs组合]](/img/7b/221b000984977508f849e19802c2c2.png)
分割回文串[dp + dfs组合]

Professor of Cambridge University: eating breakfast often is harmful and dangerous. - you know what

阿里云不同账号新旧服务器镜像迁移数据迁移同步
随机推荐
The dplyr package filter function of R language filters the data in dataframe data through combinatorial logic (and logic). The content of one field is equal to one of the specified vectors, and the v
What are the usage scenarios for locks in MySQL
ISO 32000-2 国际标准7.7
Opencv+yolo-v3 for target tracking
L'intercepteur handlerinterceptor personnalisé permet l'authentification de l'utilisateur
reflex
How MySQL queries character set codes of tables
力扣今日题-535. TinyURL 的加密与解密
人脸识别4-百度商用方案调研
SCM系统是什么?供应链管理系统有哪些优势?
Selenium file upload method
基于STM32F103ZET6库函数定时器中断实验
Bottom level internal skill cultivation
Development of freedom free agreement pledge mining system
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用exp函数和coef函数获取模型所有变量的事件密度比(Incidence Density Ratio,IDR)并解读
位图的详细介绍及模拟实现
从一个被应用商店坑了的BUG说起
自定義HandlerInterceptor攔截器實現用戶鑒權
selenium 文件上传方法
3h精通OpenCV(九)-最简单的人脸检测