The process of restructuring existing code without changing external behavior.
$this->assertEquals(0, $result); // PHPUnit
$result->shoulBe(0); // PHPSpec
$ phpunit CommandTest.php
PHPUnit 3.7.28 by Sebastian Bergmann.
PHP Fatal error: Class 'Command' not found
in Tests/TestCommand.php on line 8
public function testCanAbortUpload()
{
$transfer = $this->getMockedTransfer();
$model = $this->getMockBuilder('Guzzle\Service\Resource\Model')
->disableOriginalConstructor()
->getMock();
$command = $this->getMockBuilder('Guzzle\Service\Command\OperationCommand')
->disableOriginalConstructor()
->getMock();
$command->expects($this->any())
->method('getResult')
->will($this->returnValue($model));
$transfer->expects($this->any())
->method('getAbortCommand')
->will($this->returnValue($command));
$transfer->abort();
$this->assertTrue($this->readAttribute($transfer, 'stopped'));
}
PHPSpec is a development tool, designed to help you achieve clean and working PHP code by using a technique derived from test-first development called (spec) behaviour driven development, or SpecBDD.
phpspec.net
$this->getRating()->shouldBe(5);
$this->getTitle()->shouldBeEqualTo("Star Wars");
$this->getReleaseDate()->shouldReturn(233366400);
$this->getRating()->shouldBeLike('5');
$this->shouldThrow('\InvalidArgumentException')->duringSetRating(-3);
$this->shouldThrow('\InvalidArgumentException')->during('setRating', array(-3));
$this->shouldThrow(new \InvalidArgumentException("Invalid rating"))->during('setRating', array(-3));
$this->shouldHaveType('Movie');
$this->shouldReturnAnInstanceOf('Movie');
$this->shouldBeAnInstanceOf('Movie');
$this->shouldImplement('Movie');
$this->shouldBeAvailableOnCinemas();
$this->shouldHaveSoundtrack();
$this->getItems()->shouldHaveCount(3);
$this->getTitle()->shouldBeString();
$this->getPrice()->shouldBeFloat();
$this->getItems()->shouldBeArray();
class MovieSpec extends ObjectBehavior
{
function it_should_have_some_specific_options_by_default()
{
$this->getOptions()->shouldHaveKey('username');
$this->getOptions()->shouldHaveValue('diegoholiveira');
}
public function getMatchers()
{
return [
'haveKey' => function($subject, $key) {
return array_key_exists($key, $subject);
},
'haveValue' => function($subject, $value) {
return in_array($value, $subject);
},
];
}
}
$converter
->convert(80, 'USD')
->willReturn(106.54)
;
$converter
->convert(80, 'USD')
->shouldBeCalled()
;
class ConverterSpec extends ObjectBehavior
{
function let(ExchangeRate $exchangeRate)
{
$this->beConstructedWith($exchangeRate);
}
function letgo()
{
// release any resource
}
}
<?php
/**
* This file is part of the T-Day talk.
*
* @author PHPSpec developers
*/
namespace %namespace%;
class %name%
{
}
.phpspec/class.tpl