当前位置:网站首页>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);
}
}边栏推荐
- Sword finger offer 13 Robot range of motion (BFS)
- How to use the chart control of the b/s development tool devextreme - customize the axis position?
- Multi mode concurrent implementation of tortoise and rabbit race in go language
- What is the SRM system? How do I apply the SRM system?
- phpunit骚操作之静态类的部分mock
- Selenium file upload method
- The soft youth under the blessing of devcloud makes education "smart" in the cloud
- Custom handlerinterceptor interceptor for user authentication
- 从一个被应用商店坑了的BUG说起
- 基于STM32F103ZET6库函数串口实验
猜你喜欢

在线SQL转CSV工具

VB.Net读写NFC Ntag标签源码
Master slave replication of MySQL

Mysql database literacy, do you really know what a database is

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

linux中mysql 1045错误如何解决

What are the usage scenarios for locks in MySQL

LeetCode 每日一题——535. TinyURL 的加密与解密
![Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]](/img/33/bda0a898bfe3503197026d1f62e851.png)
Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]

DevCloud加持下的青软,让教育“智”上云端
随机推荐
The soft youth under the blessing of devcloud makes education "smart" in the cloud
测试dble split功能执行+导入耗时shell脚本参考
关于日期相加减问题
mysql视图能不能创建索引
R语言使用MASS包的glm.nb函数建立负二项广义线性模型(negative binomial)、summary函数获取负二项广义线性模型模型汇总统计信息
设置双击运行 jar 文件
reflex
Leetcode daily question - 535 Encryption and decryption of tinyurl
最受欢迎的30款开源软件
mysql. What is the concept of sock
R语言使用自定义函数编写深度学习Leaky ReLU激活函数、并可视化Leaky ReLU激活函数
Selenium upload file
Development of freedom free agreement pledge mining system
selenium上传文件
Mysql database literacy, do you really know what a database is
Visual studio plug-in coderush officially released v22.1 -- visual tool for optimizing debugging
0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram
Opencv+YOLO-V3实现目标跟踪
分割回文串[dp + dfs组合]
linux中mysql 1045错误如何解决