Example below shows some basics of PhpSpec. Also please don't take the example classes too seriously because they are just used for demonstration purposes. For more information about PhpSpec and it's features see this and this documentations.


Player class


namespace Application\BackendBundle\Util;

class Player
{
const GENDER = 'male';
const LOVESFOOTBALL = true;
public $name;

public function setName($name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}

public function getGender()
{
return self::GENDER;
}

public function getLovesFootball()
{
return self::LOVESFOOTBALL;
}
}

Team class


namespace Application\BackendBundle\Util;

use InvalidArgumentException;

class Team
{
private $player;
private $argOne;
private $argTwo;
private $argThree;
private $argFour;
private $argSix;

public function __construct(
Player $player,
array $argOne,
$argTwo,
array $argThree,
array $argFour,
$argSix
) {
$this->player = $player;
$this->argOne = $argOne;
$this->argTwo = $argTwo;
$this->argThree = $argThree;
$this->argFour = $argFour;
$this->argSix = $argSix;

$this->callMeFromConstruct($player);
}

public function callMeFromConstruct($arg)
{
return $arg;
}

public function isActive()
{
return true;
}

public function hasStadium()
{
return false;
}

public function getPlayerInstance()
{
return $this->player;
}

public function getArgOne()
{
return $this->argOne;
}

public function getArgOneType()
{
return gettype($this->argOne);
}

public function getArgOneCount()
{
return count($this->argOne);
}

public function getArgTwo()
{
return $this->argTwo;
}

public function getArgThree()
{
return $this->argThree;
}

public function getArgFour()
{
return $this->argFour;
}

public function getArgSix()
{
return $this->argSix;
}

public function getSum($argOne, $argTwo)
{
if (!is_numeric($argOne) || !is_numeric($argTwo)) {
throw new InvalidArgumentException('Arguments must be numeric only.');
}

return ($argOne + $argTwo);
}

public function createPlayer(Player $player, $name)
{
$player->setName($name);

return $player->getName();
}

public function getPlayerInfo(Player $player, $name)
{
$player->setName($name);

return $player;
}

public function methodOne($arg)
{
return $this->methodTwo($arg);
}

public function methodTwo($arg)
{
return 'Received: ' . $arg;
}
}

TeamSpec


namespace spec\Application\BackendBundle\Util;

use Application\BackendBundle\Util\Player;
use InvalidArgumentException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class TeamSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith(
new Player(),
array(),
'plain string value',
array('inanzzz'),
array('key' => 'value'),
11.00
);
}

function it_is_initializable()
{
$this->shouldHaveType('Application\BackendBundle\Util\Team');
}

function it_is_called_by_construct()
{
$this->callMeFromConstruct(new Player())->shouldBeAnInstanceOf('Application\BackendBundle\Util\Player');
}

function it_is_should_be_active()
{
$this->shouldBeActive();
}

function it_should_not_have_stadium()
{
$this->shouldNotHaveStadium();
}

function it_should_return_instance_of_player_object()
{
$this->getPlayerInstance()->shouldHaveType('Application\BackendBundle\Util\Player');
$this->getPlayerInstance()->shouldReturnAnInstanceOf('Application\BackendBundle\Util\Player');
$this->getPlayerInstance()->shouldBeAnInstanceOf('Application\BackendBundle\Util\Player');
$this->getPlayerInstance()->shouldImplement('Application\BackendBundle\Util\Player');
$this->getPlayerInstance()->shouldBeObject();
}

function it_should_return_expected_result()
{
$this->getArgOne()->shouldBeArray();
$this->getArgOne()->shouldReturn(array());
$this->getArgOne()->shouldBeLike(array());
$this->getArgTwo()->shouldBeString();
$this->getArgTwo()->shouldReturn('plain string value');
$this->getArgTwo()->shouldBeString();
$this->getArgTwo()->shouldStartWith('plain');
$this->getArgTwo()->shouldEndWith('value');
$this->getArgTwo()->shouldMatch('/ring/i');
}

function it_should_return_expected_result_type()
{
$this->getArgOneType()->shouldBe('array');
$this->getArgOneType()->shouldBeEqualTo('array');
$this->getArgOneType()->shouldEqual('array');
}

function it_should_not_return_unexpected_type()
{
$this->getArgOne()->shouldNotBeInteger();
$this->getArgOne()->shouldNotBeBool();
$this->getArgOne()->shouldNotBeString();
}

function it_should_be_decimal()
{
$this->getArgSix()->shouldBeDecimalOrDouble();
}

function it_should_return_expected_result_count()
{
$this->getArgOneCount()->shouldBe(0);
}

function it_should_contain_certain_element()
{
$this->getArgThree()->shouldContain('inanzzz');
}

function it_should_have_certain_array_key()
{
$this->getArgFour()->shouldHaveKey('key');
}

function it_should_has_correct_amount_of_elements()
{
$this->getArgOne()->shouldHaveCount(0);
}

function it_should_return_correct_sum()
{
$this->getSum(1, 2)->shouldBe(3);
}

function it_should_throw_exception_on_sum()
{
$this->shouldThrow('\InvalidArgumentException')->during('getSum', array(1, 'A'));
$this->shouldThrow('\InvalidArgumentException')->duringGetSum(1, 'A');
$this->shouldThrow(new InvalidArgumentException('Arguments must be numeric only.'))->during('getSum', array(1, 'A'));
}

function it_should_create_a_player()
{
$this->createPlayer(new Player(), 'inanzzz')->shouldReturn('inanzzz');
}

function it_should_return_player_info()
{
$player = new Player();
$name = 'inanzzz';
$player->setName($name);

$this->getPlayerInfo($player, $name)->shouldReturnAnInstanceOf('Application\BackendBundle\Util\Player');
}

function it_should_call_another_method_to_get_result()
{
$this->methodOne('inanzzz')->shouldReturn('Received: inanzzz');
}

function it_should_get_concatenated_result()
{
$this->methodTwo('inanzzz')->shouldReturn('Received: inanzzz');
}

public function getMatchers()
{
return [
'beDecimalOrDouble' => function ($value) {
return is_double($value);
}
];
}
}