unit-testing/class-spy 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

unit-testing/class-spy

Composer 安装命令:

composer require --dev unit-testing/class-spy

包简介

a simple trait that aids in unit testing protected class methods

README 文档

README

In some cases, you may want to mock a class's methods without using testing facility like https://github.com/padraic/mockery. This trait will let you do that.

Installation

  • composer require --dev unit-testing/class-spy:dev-master
  • or in require-dev block of composer.json, add "unit-testing/class-spy": "dev-master" and then run composer update

Usage

In your phpunit test, you'll create a stub of your class under test. In that stub definition, add use \UnitTesting\ClassSpy\WatchableTrait;. Your stub will be extended with the following methods:

  • trackMethodCall: For any method you want to test on the stub, you can create (or override its parent) method by calling return $this->trackMethodCall(); from within the method.
  • getAllMethodCalls: Returns an array of all the arguments passed to all tracked methods. The array has a list of method names as keys and an array of arguments passed to the method as the value.
  • getMethodCalls($method, $index = null): Pass the method name to get the arguments passed to it. Optionally add an index (where 1 = the first call) to get the arguments for a specific call if the method was called more than once. If the argument was never called, or was not called less times than the index provided, it will return null.
  • getLastMethodCall($method): Get the last set of arguments for a particular method.
  • setMethodResult($method, $result): Fake a specific result to be passed back from your tracked method. If the $result parameter is an instance Closure, the Closure will be executed with the actual parameters and its result will be returned.

All of the above have their equivalents for static class methods: trackStaticMethodCall, getAllStaticMethodCalls, getStaticMethodCalls, getLastStaticMethodCall, setStaticMethodResult.

Example

<?php namespace UnitTesting\ClassSpy;

class WatchableTraitIntegrationTest extends \PHPUnit_Framework_TestCase {

	function test_WatchableTrait_TracksCalls()
	{
		$instance = new SomeTestClassStub;

		// let's make some calls to this method
		$instance->doSomething('foo');
		$instance->doSomething();
		$instance->doSomethingElse('zee');
		$instance->doSomething('baz', 'boo');

		// all the tracked method and their arguments can be retrieved with getAllMethodCalls()
		$this->assertEquals(array(
			'doSomething' => array(
				array('foo'),
				array(),
				array('baz', 'boo'),
			),
			'doSomethingElse' => array(
				array('zee'),
			),
		), $instance->getAllMethodCalls());

		// get a specific method's argument calls
		$this->assertEquals(array(
			array('foo'),
			array(),
			array('baz', 'boo'),
		), $instance->getMethodCalls('doSomething'));

		// get a specific method's argument calls with an index, remember 2 = second call
		$this->assertEquals(array(), $instance->getMethodCalls('doSomething', 2));

		// get the last argument call
		$this->assertEquals(array('baz', 'boo'), $instance->getMethodCalls('doSomething', 'last'));

		// another way to do the above
		$this->assertEquals(array('baz', 'boo'), $instance->getLastMethodCall('doSomething'));

		// trying to access a call on a tracked method with an index beyond the actual number of calls yields null
		$this->assertNull($instance->getMethodCalls('doSomething', 100));

		// something that is never called will yield null
		$this->assertNull($instance->getMethodCalls('doSomethingThatIsNotTracked'));
	}

	function test_WatchableTraitOnStatic_TracksCalls()
	{
		// let's make some calls to this method
		SomeTestClassStub::doSomethingStatic('foo');
		SomeTestClassStub::doSomethingStatic();
		SomeTestClassStub::doSomethingStatic('baz', 'boo');

		// all the tracked method and their arguments can be retrieved with getAllMethodCalls()
		$this->assertEquals(array(
			'doSomethingStatic' => array(
				array('foo'),
				array(),
				array('baz', 'boo'),
			),
		), SomeTestClassStub::getAllStaticMethodCalls());

		// don't forget to reset calls on a static for subsequent tests
		SomeTestClassStub::flushStatic();
	}

	function test_WatchableTrait_CanSetResponses()
	{
		$instance = new SomeTestClassStub;
		// let's set up a simple response
		$instance->setMethodResult('doSomething', 'same result every time');

		// it will always return same thing
		$this->assertEquals('same result every time', $instance->doSomething());
		$this->assertEquals('same result every time', $instance->doSomething('foo'));
		$this->assertEquals('same result every time', $instance->doSomething('foo', 'bar'));

		// return something based upon the parameter
		$instance->setMethodResult('doSomething', function($param)
		{
			return $param . ' result';
		});

		// it will now return value based upon our closure
		$this->assertEquals('foo result', $instance->doSomething('foo'));
		$this->assertEquals('bar result', $instance->doSomething('bar'));
	}
}

class SomeTestClassStub {
	// add this trait to set this class up for testing
	use \UnitTesting\ClassSpy\WatchableTrait;

	public function doSomething()
	{
		// this will actuall track the method and its arguments.
		// Be sure to return its value if you want to mock some return values.
		return $this->trackMethodCall();
	}

	public function doSomethingElse()
	{
		// we'll just set up another one the same as above for illustration purposes.
		return $this->trackMethodCall();
	}

	public static function doSomethingStatic()
	{
		// we'll do the same for a static method
		return self::trackStaticMethodCall();
	}
}

unit-testing/class-spy 适用场景与选型建议

unit-testing/class-spy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.89k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「phpunit」 「trait」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 unit-testing/class-spy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 unit-testing/class-spy 我们能提供哪些服务?
定制开发 / 二次开发

基于 unit-testing/class-spy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.89k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-27