当前位置:网站首页>phpunit骚操作之静态类的部分mock
phpunit骚操作之静态类的部分mock
2022-06-29 17:39:00 【fangdong88】
通过官方文档以及实践得知,含有静态方法的类如果需要mock,就要使用前缀 alias: ,但是随之而来有个问题是后面无法调用这个类本身的方法,所以通过阅读源码可以使用以下方法来实现:
1.创建 Helpers 类进行封装
<?php
use Mockery;
use Mockery\Generator\MockConfigurationBuilder;
class Helpers
{
/**
* Mock static class, you can call the original method and mock the method
* mock 静态类,既可以调用原来的方法,又可以 mock 方法
* @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.创建静态类
class Test
{
public static function getName($name) {
if (empty($name)) {
return 'default_name';
}
return $name;
}
}3.测试运行
<?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);
}
}边栏推荐
- selenium 组合键操作
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用exp函数和coef函数获取模型所有变量的事件密度比(Incidence Density Ratio,IDR)并解读
- 育润多维发力慈善领域,勇抗企业公益大旗
- Yurun multidimensional makes efforts in the charity field and bravely resists the corporate public welfare banner
- Bottom level internal skill cultivation
- mysql. What is the concept of sock
- 0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram
- MySQL highly available cluster – MHA
- 剖析下零拷贝机制的实现原理,适用场景和代码实现
- 序列检测器
猜你喜欢
![Split palindrome string [dp + DFS combination]](/img/7b/221b000984977508f849e19802c2c2.png)
Split palindrome string [dp + DFS combination]

Mysql中锁的使用场景是什么

传承中华美德,关注中老年大健康,育润奶粉敬老情浓

力扣每日一题 06.29 两数相加

Redis 原理 - Sorted Set (ZSet)

2022 spring summer collection koreano essential reshapes the vitality of fashion

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

mysql. What is the concept of sock
![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]
Help MySQL data analysis with databend
随机推荐
Issue 42: is it necessary for MySQL to have multiple column partitions
Li Kou today's question -535 Encryption and decryption of tinyurl
PCB frame drawing - ad19
在线SQL转CSV工具
Open source warehouse contribution - submit pr
Master slave replication of MySQL
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
R语言使用MASS包的glm.nb函数建立负二项广义线性模型(negative binomial)、summary函数获取负二项广义线性模型模型汇总统计信息
Scenario solution for two modules calling each other in go
0 basic self-study STM32 (wildfire) - register lit LED
R language ggplot2 visualization: use the patchwork package (directly use the plus sign +) to horizontally combine the two ggplot2 visualization results, and then horizontally combine them with the th
Multi mode concurrent implementation of tortoise and rabbit race in go language
Use SSH to pull codes
SCM系统是什么?供应链管理系统有哪些优势?
mysql查询视图命令是哪个
Mysql中锁的使用场景是什么
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses exp function and coef function to ob
从一个被应用商店坑了的BUG说起
The soft youth under the blessing of devcloud makes education "smart" in the cloud
正则表达式