serendipity_hq / phpunit_helper
PHPUnit 辅助工具,用于管理测试资源、模拟对象和测试值。
Requires
- php: ^7.4|^8.0
- symfony/property-access: ^4.4|^5.4
Requires (Dev)
- phpunit/phpunit: ^8
README
SHQ_PHPUnit_Helper
! 文档尚未更新 !
此助手允许您轻松地拆分测试。
它还可以在拆分前后打印有关内存使用情况的一些信息。
如何安装 PHPUnit_Helper
要安装 PHPUnit_Helper,请使用 Composer
composer require serendipity_hq/phpunit_helper
快速使用
要快速使用 PHPUnit_Helper,创建一个 TestCase.php 文件,并从该文件扩展所有测试。
此助手构建为 trait,因此您可以在其他测试用例(例如,由 LiipFunctionalTestBundle 或 Symfony 的 WebTestCase 提供的 WebTestCase)中使用它。
<?php
/**
* @package PHPUnit_Helper
*
* @author Adamo Crespi <hello@aerendir.me>
* @copyright Copyright (C) 2016.
* @license MIT
*/
namespace AppBundle\Tests;
use Liip\FunctionalTestBundle\Test\WebTestCase as BaseWebTestCase;
use SerendipityHQ\Library\PHPUnit_Helper\PHPUnit_Helper;
/**
* Class WebTestCase
*
* @package AppBundle\Tests
*/
class WebTestCase extends BaseWebTestCase
{
/** Here you include the trait in the test case */
use PHPUnit_Helper;
public function tearDown()
{
/** Measure the memory usage before tear down */
$this->measureMemoryBeforeTearDown();
/** Call the parent tear down method */
parent::tearDown();
/** CALL THIS AFTER the parent tear down method */
$this->helpTearDown();
/** Print in the console the information about memory usage */
$this->printMemoryUsageInfo();
}
}
使用助手
在单元测试期间,您需要
- 实例化要测试的类;
- 创建一些模拟来模拟由测试类使用的原始类;
- 设置一些预期值。
所有这些操作都可以由助手以简单而优雅的方式处理。
以下是一个测试假设的 Order 实体的示例代码
/**
* Tests all get and set methods
*/
public function testOrder()
{
$this->setResourceToTest(new Order());
$this->addHelpMocksCollection('purchase',
$this->generateMocksCollection($this->getMock('\AppBundle\Entity\Purchase'), 3),
true
)
->addExpectedValue('id', 1)
->addHelpMock('channel', $this->getMock('\AppBundle\Entity\Store'), true)
->addHelpMock('placedBy', $this->getMock('\AppBundle\Entity\Customer'), true)
->addExpectedValue('placededOn', new \DateTime('2015-04-12 00:08'))
->addExpectedValue('modifiedOn', new \DateTime('2015-04-12 00:08'))
->addExpectedValue('completedOn', new \DateTime('2015-04-12 00:08'))
->bindExpectedValuesToResource();
$this->assertEquals($this->getExpectedValue('id'), $this->getTestingResource()->getId());
$this->assertEquals($this->getExpectedValue('channel'), $this->getTestingResource()->getChannel());
$this->assertEquals($this->getExpectedValue('placedBy'), $this->getTestingResource()->getPlacedBy());
$this->assertEquals($this->getExpectedValue('placedOn'), $this->getTestingResource()->getPlacedOn());
$this->assertEquals($this->getExpectedValue('modifiedOn'), $this->getTestingResource()->getModifiedOn());
$this->assertEquals($this->getExpectedValue('completedOn'), $this->getTestingResource()->getCompletedOn());
$this->assertEquals($this->getExpectedValue('id'), $this->getTestingResource()->__toString());
$this->assertEquals($this->getExpectedCount('purchase'), count($this->getTestingResource()->getPurchases()));
$this->getTestingResource()->removePurchase($this->removeMockFromMocksCollection(1, 'purchase'));
$this->assertEquals($this->getExpectedCount('purchase'), count($this->getTestingResource()->getPurchases()));
}
使用 $this->setResourceToTest()
使用 $this->setResourceToTest(),您可以告诉助手测试资源是什么。了解这一点后,助手可以帮助您在添加预期值后填充它。
使用 $this->addHelpMocksCollection() 和 $this->generateMocksCollection()
要测试 add*() 和 remove*() 方法,您必须创建许多相同类型对象的模拟。
也许您的 add*() 方法看起来像这样
/**
* Add a Product
*
* @param \AppBundle\Entity\Purchase $purchase
* @return $this
*/
public function addPurchase(Purchase $purchase)
{
$this->purchases[] = $purchase;
$purchase->setInOrder($this);
return $this;
}
测试此方法的旧方法可能如下所示
$resource = new Order();
$testPurchases = [
$this->getMock('\AppBundle\Entity\Purchase'),
$this->getMock('\AppBundle\Entity\Purchase'),
$this->getMock('\AppBundle\Entity\Purchase')
];
foreach ($testPurchases as $purchase)
$resource->addPurchase($purchase);
使用 PHPUnit_Helper::generateMocksCollection() 方法,您只需写一行代码
$this->addHelpMocksCollection('purchase',
$this->generateMocksCollection($this->getMock('\AppBundle\Entity\Purchase'), 3),
true
)
使用集合名称,使用不带 add 部分的 add*() 方法名称(在示例 Order 实体中,添加 Purchase 的方法称为 Order::addPurchase())。
使用集合可以使您能够使用 PHPUnit_Helper::getExpectedCount({name_of_the_collection}) 和 PHPUnit_Helper::removeMockFromMocksCollection({key}, {name_of_the_collection}) 方法。
默认情况下,PHPUnit_Helper::removeMockFromMocksCollection() 还会从预期值中删除模拟,但您可以通过传递可选的第三个参数并将其设置为 false 来禁用此行为。
使用 PHPUnit_Helper::bindExpectedValuesToResource()
添加预期值并作为您类的属性调用它们,使您能够使用 PHPUnit_Helper::bindExpectedValuesToResource() 方法。这可以自动用预期值填充您通过 PHPUnit_Helper::setResource() 设置的测试资源,因此您不需要手动填充它。
使用反射来拆分测试
PHPUnit_Helper::helpTearDown() 方法默认将内部属性设置为 null,而不关心测试的属性。
如果您使用具有现有测试类的此方法,则可能需要使用 PHPUnit_Helper::useReflectionToTearDown() 方法。
使用此方法,您可以设置一个内部标志为true,该标志告诉PHPUnit_Helper使用反射将测试类的所有找到的属性设置为null。
这在实现过程中非常有用,可以充分利用拆卸功能,即使您尚未使用所有辅助方法来管理测试。
测试期间测量内存使用情况
您可以使用方法$this->measureMemoryBeforeTearDown()和$this->printMemoryUsageInfo()在拆卸前后测量内存使用情况。最后一个方法如果尚未调用,将自动调用$this->measureMemoryAfterTearDown()。
因此,您可以在您最适合的时候显式调用它来测量内存使用情况。