PHPUnit
Справочная страница https://phpunit.readthedocs.io/ru/latest/index.html
1. Зависимости @depends testEmpty
<?php
use PHPUnit\Framework\TestCase;
class StackTest extends TestCase
{
public function testEmpty()
{
$stack = [];
$this->assertEmpty($stack);
return $stack;
}
/**
* @depends testEmpty
*/
public function testPush(array $stack)
{
array_push($stack, 'foo');
$this->assertSame('foo', $stack[count($stack)-1]);
$this->assertNotEmpty($stack);
return $stack;
}
/**
* @depends testPush
*/
public function testPop(array $stack)
{
$this->assertSame('foo', array_pop($stack));
$this->assertEmpty($stack);
}
}
2. Провайдеры данных @dataProvider additionProvider
<?php
use PHPUnit\Framework\TestCase;
class DataTest extends TestCase
{
/**
* @dataProvider additionProvider
*/
public function testAdd($a, $b, $expected)
{
$this->assertSame($expected, $a + $b);
}
public function additionProvider()
{
return [
'adding zeros' => [0, 0, 0],
'zero plus one' => [0, 1, 1],
'one plus zero' => [1, 0, 1],
'one plus one' => [1, 1, 3]
];
}
}
3. Тестовые двойники
class SportsMetricTest extends TestCase
{
/**
* @var SportsMetric
*/
protected $sportsMetric;
public function setUp(): void
{
$mailer = new FakeMailer();
$httpClientStub = $this->createStub(Client::class);
$httpClientStub->method('getData')->willReturn('Сообщение по умолчанию');
$this->sportsMetric = new SportsMetric($mailer, $httpClientStub);
}
public function testThatFiveStepsIsBadResult()
{
$resultMessage = $this->sportsMetric->analyzeDailyFootsteps(5);
$this->assertEquals('Ленивая задница', $resultMessage);
}
public function testTwoHundredsStepsIsNormalResult()
{
$resultMessage = $this->sportsMetric->analyzeDailyFootsteps(200);
$this->assertEquals('Неплохо', $resultMessage);
}
public function testThatEmailNotificationIsSent()
{
$mailer = $this->createMock(Standard::class);
$mailer->expects($this->once())->method('send');
$client = new Client();
$sportMetric = new SportsMetric($mailer, $client);
$sportMetric->analyzeDailyFootsteps(200);
}
}