I'm trying to spec value object with static constructor in phpspec and the problem it says that shouldBeLike
is undefined method. The object returned from times
method is new Money, so I don't know what is wrong...
<?php
namespace spec;
use Money;
use PhpSpec\ObjectBehavior;
class MoneySpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(Money::class);
}
function it_multiplies()
{
$five = Money::dollar(5);
$five->times(2)->shouldBeLike(Money::dollar(5));
$five->times(3)->shouldBeLike(Money::dollar(6));
}
}
<?php
class Money
{
private $amount;
private function __construct($amount)
{
$this->amount = $amount;
}
public static function dollar($amount)
{
return new Money($amount);
}
public function times($multiplier)
{
return new Money($this->amount * $multiplier);
}
public function getAmount()
{
return $this->amount;
}
}
For named static constructors you need use beConstructedThrough
(or one of the shorter syntax described). For example, with your case:
function it_multiplies()
{
$this->beConstructedThrough('dollar', [5]);
$this->times(2)->shouldBeLike(Money::dollar(5));
$this->times(3)->shouldBeLike(Money::dollar(6));
}