当前位置:网站首页>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
- 剖析下零拷贝机制的实现原理,适用场景和代码实现
- 基于注解和拦截器防止表单重复提交
- R语言使用自定义函数编写深度学习线性激活函数、并可视化线性激活函数
- What is the function of MySQL cursors
- 布隆过滤器:
- R语言使用MASS包的glm.nb函数建立负二项广义线性模型(negative binomial)、summary函数获取负二项广义线性模型模型汇总统计信息
- R语言dplyr包filter函数通过组合逻辑(与逻辑)过滤dataframe数据中的数据、其中一个字段的内容等于指定向量中的其中一个,并且另外一个字段值大于某一阈值
- mongoTemplate - distinct 使用
- 育润多维发力慈善领域,勇抗企业公益大旗
猜你喜欢
随机推荐
What are the usage scenarios for locks in MySQL
Open source warehouse contribution - submit pr
What is the SRM system? How do I apply the SRM system?
2022春夏系列 KOREANO ESSENTIAL重塑时装生命力
How to solve the 2003 error of MySQL in Linux
phpunit骚操作之静态类的部分mock
mac安装php7.2
mongoTemplate - distinct 使用
R language uses user-defined functions to write deep learning linear activation functions and visualize linear activation functions
R语言使用MASS包的glm.nb函数建立负二项广义线性模型(negative binomial)、summary函数获取负二项广义线性模型模型汇总统计信息
分布式 | 几步快速拥有读写分离
分割回文串[dp + dfs组合]
Custom handlerinterceptor interceptor for user authentication
L'intercepteur handlerinterceptor personnalisé permet l'authentification de l'utilisateur
linux中mysql 1045错误如何解决
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
ISO 32000-2 国际标准7.7
自定義HandlerInterceptor攔截器實現用戶鑒權
How MySQL queries character set codes of tables
The R language uses the KAP function (kap.2.raters function) of epidisplay package to calculate the value of kappa statistics (total consistency, expected consistency), analyze the consistency of the





![填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]](/img/33/bda0a898bfe3503197026d1f62e851.png)



