定制 genesis/phpunitassister 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

genesis/phpunitassister

Composer 安装命令:

composer require genesis/phpunitassister

包简介

Extends the phpunit WebTestCase class to provide easier mocking and assertions (chain mocking and chain assertions)

README 文档

README

This README would normally document whatever steps are necessary to get your application up and running.

What is this repository for?

  • Quick summary PHPUnit assister's main focus is to minimize the amount of duplicate code written for testing. Inital version focuses on mocking and asserting methods.

How do I get set up?

Install/Cloning the repo 

With composer

{
	"require": {
		"genesis/phpunitassister": "1.0.*"
	}
}
  • PHPUnit assister is a simple class library which is to be placed as a parent class of the actual test class.

  • Configuration There isnt much to configure, the mockProvider.php is a custom file where you can define your applications mock classes. Currently it works for a symfony2 app.

  • Dependencies PHPUnit
  • Database configuration No Database calls.
  • How to run tests Simply extend your test class with the objectHandler class and run the app, it should work as is even if written with plain PHPUnit methods. Once its working, use the methods provided.
// YourProject/Src/Bundle/Test/YourTestClass.Test.php

use PHPUnitAssister\Core\TestObjectHandler;

class YourTestClass extends TestObjectHandler {

    public function testYourMethod()
    {
        ...
    }
}

You can also choose to extend the assister with your own custom methods, a base symfony2 mock provider is already provided as an extended class.

// PHPUnitAssister/Src/Extensions/Symfony2MockProvider.Class.php

namespace PHPUnitAssister\Extensions;


use PHPUnitAssister\Core\TestObjectHandler;

/*
 * This class provides standard symfony2.3 mock objects
 */
class Symfony2MockProvider extends Mocker{
	...
}
// YourClass/ExtendedMockProvider.Class.php

class ExtendedMockProvider extends Mocker{ // or Symfony2MockProvider

}

Contribution guidelines

  • Feel free to contribute to this small project.

Who do I talk to?

  • Repo owner or admin

  • phpunitAssister abbreviated calls

    // YourProject/Src/Bundle/Test/YourTestClass.Test.php
    

// Setting the test object $this->setTestObject($yourClassToTest, array $arguments);

// Mocking $this->setmo(object $mocked) // set mock object

->mm($method, array $args ...) // mock method
->mmx(array $methods, array $options) // Mock multiple methods at once, stops chaining
->then($method, array $args ...) // Chain next mock object with previous will clause object
->getmo() // get mock object
->getmos() // get mock objects
->setbm() // Set base mock

// Assertions $this->tm($method) // test method

->with($param1, $param2) // params for the test method

// Basic assertions
->assert('true') // Assert that result is true
->assert('true', 'your result') // Assert that result equals the string 'your result'
->assert('false')
->assert('equals', 'something-that-equals-result') // Perform equal assertion
->assert('empty') // assert if result is empty
->assert('notempty') // assert if result in non-empty

// String position
->assert('contains', 'This-would-be-something-you-expect-the-result-to-contain')
->assert('true', '!==something-not-equal-to-result') // can be used with true or false

// Regular expression assert
->assert('regexp', '/some-regular-expression/i')

// Array assertions
->assert('isarray') // Assert that result is an array
->assert('isarray', '[]==5') // Assert that array has a count of 5, can be used with true or false
->assert('isarray', '[3]==example') // Assert that array index 3 is equal to example, can be used with true or false
->assert('arrayhaskey', 'some-key-index')
->setIndexToTest('someIndex') // Explicitly set the index to test

// Object assertions
->assert('isobject') // Assert that resultant is an object
->assert('isobject', $classType) // Assert that the resultant object is of type $classType
->setPropertyToTest('property') // Sets the property to be tested
->assert('true', '->property==something') // Shortcut for the setPropertyToTest method, can be used with true or false
->callMethodToTest('isLoggedIn') // Call method 'isLoggedIn' on resultant object and set its value as the test subject
->assert('true') // Assert that the result of isLoggedIn is true

// Generic calls
->getTestResult(); // Returns the test result at any point

// Repeat call - can be used with specific assertion calls only

->assert('isarray', '[3]==example') // Assert that array index 3 is equal to example
->repeat() // Will repeat isarray with no params
->repeat('[1]==repeated') // will repeat assert isarray with index 1 equal to repeated
->repeat('[]==10') // will repeat assert is array with param array count equal to 10

// Resetting the initial result back to test

->resetResultToTest();

* Examples

Setting your test class

// YourProject/Src/Bundle/Test/YourTestClass.Test.php

$this->SymfMockProvider = $this->getMockProvider('Symfony2');

$this->setTestObject('Bundles\CampaignsBundle\Service\CampaignsService', array(

        'entityManager' => $this->SymfMockProvider->getEntityManagerMock(),
        'router' => $this->SymfMockProvider->getRouterMock(),
        'translator' => $this->SymfMockProvider->getTranslatorMock()
    ));

Altering your test class's dependency

$entityManager = $this->SymfMockProvider->getEntityManagerMock();

$this->resetTestObjectArgument('entityManager', $entityManager);


A cleaner and more contained way to mock (chain-mock)

PHPUnit standard

// YourProject/Src/Bundle/Test/YourTestClass.Test.php

// User mock to be injected in assertRepoMock $userMock = $this->getUserMock(); $userMock->expects($this->any())

->method('getEmail')
->will($this->returnValue('example@phpunitAssister.com'));

// assertRepoMock to be injected in entityManager $assertRepoMock = $this->SymfMockProvider->getRepositoryMock('AssetBundle:Image'); $assertRepoMock->expects($this->exactly(1))

    ->method('find')
    ->with(12)
    ->will($this->returnValue($userMock));

// Final object that contains the mocked objects $entityManager = $this->SymfMockProvider->getEntityManagerMock(); $entityManager->expects($this->exactly(2))

    ->method('getRepository')
    ->with('AssetBundle:Image')
    ->will($this->returnValue($assertRepoMock));

PHPUnit assister - chain mocking the entity manager in symfony2

// YourProject/Src/Bundle/Test/YourTestClass.Test.php

// Starts at the entityManager $entityManager = $this->setmo($this->SymfMockProvider->getEntityManagerMock())

->mm('getRepository', [
	'expects' => $this->exactly(2),
	'with' => 'AssetBundle:Image',
	// Inject the mock directly, doesnt need to stored in another variable
	'will' => $this->returnValue($this->SymfMockProvider->getRepositoryMock('AssetBundle:Image'))
])
// Use the then call to further mock methods of the previously set object in the will clause
->then('find', [
	'expects' => $this->exactly(1),
	'with' => 12,
	'will' => $this->returnValue($this->getUserMock())
])
// This will mock a method on the $this->getUserMock() resultant object
->then('getEmail', [
	//Omit expects as its default value is $this->any()
	'will' => $this->returnValue('example@phpunitAssister.com')
])
// Finally get the mocked object to inject into the test object
->getmo();

To use the userMock and AssetRepoMock from the previous example you can do

list($entityManager, $assetRepoMock, $userMock) = $this->setmo($this->SymfMockProvider->getEntityManagerMock())

->mm('getRepository', [
	'expects' => $this->exactly(2),
	'with' => 'AssetBundle:Image',
	// Inject the mock directly, doesnt need to stored in another variable
	'will' => $this->returnValue($this->SymfMockProvider->getRepositoryMock('AssetBundle:Image'))
])
// Use the then call to further mock methods of the previously set object in the will clause
->then('find', [
	'expects' => $this->exactly(1),
	'with' => 12,
	'will' => $this->returnValue($this->getUserMock())
])
// This will mock a method on the $this->getUserMock() resultant object
->then('getEmail', [
	//Omit expects as its default value is $this->any()
	'will' => $this->returnValue('example@phpunitAssister.com')
])
// Getmos call gives you call mocks in sequential order
->getmos();

Calling test methods and chained Assertions

// YourProject/Src/Bundle/Test/YourTestClass.Test.php

... // Mocking

// Set method to test $this->tm('exampleMethod')

// Specify params of method if any
->with($param1, $param2)
// assert if its an object, can also provide a specific object to test against
->assert('isobject', $optionalSpecificObject)
// call a method on the resultant object to further test its values
->callMethodToTest('isLoggedIn')
// check if the result of the isLoggedIn call is true
->assert('true');

Calling a private/protected method to test

$this->setTestObjectMethodAccessible() // or $this->setma()

->tm('privateMethod')->with()
->assert...

The `->setTestObjectMethodAccessible` will set the next tested method using the `->tm` call to public accessibility. Must be called before the `->with` call in order to work.

To get the mock provider you can use `$this->provider` which essentially is the same as `$this->getMockProvider()`. Note that this will provide you with basic mocking capabilities. To create a new mock object you can use

// Args are optional in this case, this will provide a new mock with the constructor disabled. $this->provider->getNewMock('\Your\Class', $args);


Creating extensions
-------------------

To create an extension place your extension mock provider in the extensions folder (Src/Extensions) with the namespace PHPUnitAssister\Extensions, the filenaming convention is `nameOfYourExtension.Class.php`

Once you have placed it in extensions folder, you can access your extension methods using the getMockProvider call

// YourProject/Src/Bundle/Test/YourTestClass.Test.php ... // Any code

$this->getMockProvider('name-of-your-extension-without-mockProvider-extension');


genesis/phpunitassister 适用场景与选型建议

genesis/phpunitassister 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 135 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 11 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 genesis/phpunitassister 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2014-11-05