当前位置:网站首页>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);
}
}
边栏推荐
- 填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]
- R语言ggplot2可视化:使用patchwork包(直接使用加号+)将两个ggplot2可视化结果横向组合、接着再和第三个图像横向组合起来(三幅图各占比例为50%、25%、25%)
- MySQL highly available cluster – MHA
- What is the MySQL query view command
- PCB frame drawing - ad19
- 软件快速交付真的需要以安全为代价吗?
- C语言练习----指针字符串、链表
- [the sixth operation of modern signal processing]
- Kali installation tutorial 2020
- [Oracle] basic knowledge interview questions
猜你喜欢
Collaborative development of epidemic home outsourcing project 𞓜 community essay solicitation
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]
底层内功修养
基于gis三维可视化的智慧城市行业运用
序列检测器
Mysql database literacy, do you really know what a database is
Does MySQL support foreign keys
Openfeign use step polling strategy and weight log4j configuration of openfeign interceptor
Tencent cloud released orbit, an automated delivery and operation and maintenance product, to promote enterprise applications to be fully cloud native
OpenFeign使用步骤 轮询策略与权重 log4j使用 openFeign拦截器的配置
随机推荐
DevCloud加持下的青软,让教育“智”上云端
MySQL highly available cluster – MHA
Self taught structure (small turtle C language)
关于日期相加减问题
R language uses user-defined functions to write deep learning leaky relu activation functions and visualize leaky relu activation functions
It is the same that robots can win gold medals without maintenance and out of bounds
Segment tree and tree array template (copy and paste are really easy to use)
0 basic self-study STM32 (wildfire) - register lit LED
Tencent cloud released orbit, an automated delivery and operation and maintenance product, to promote enterprise applications to be fully cloud native
Multi mode concurrent implementation of tortoise and rabbit race in go language
Collaborative development of epidemic home outsourcing project 𞓜 community essay solicitation
What are the advantages of SaaS services
3h精通OpenCV(九)-最简单的人脸检测
How to create and delete MySQL triggers
External automatic (PLC start robot)
reflex
基于STM32F103ZET6库函数PWM输出实验
基于STM32F103ZET6库函数串口实验
Error:Connection refused: connect
基于gis三维可视化的智慧城市行业运用