mardi 25 avril 2017

Adding a Mocked object to a typed list

I am trying to test this Box object (this is the simplified code):

class Box
{
    protected $boxTimes;

    protected $enabled;

    protected $number;

    public function __construct(int $number, bool $enabled)
    {
        $this->boxTimes = new ArrayCollection;
        $this->enabled = $enabled;
        $this->number = $number;
    }

    public function addBoxTime(BoxTime $boxTime): void
    {
        $this->boxTimes->add($boxTime);
    }

    public function estimatedWaitingTime(): int
    {
        $boxTimes = $this->boxTimes->map(function (BoxTime $boxTime) {
            return $boxTime->getTime();
        })->toArray();

        return array_sum($boxTimes) / count($boxTimes);
    }
}

As you can see it has a boxTimes list. This is the BoxTime object:

class BoxTime
{
    protected $dateTime;

    protected $time;

    public function __construct(Carbon $dateTime, int $time)
    {
        $this->dateTime = $dateTime;
        $this->time = $time;
    }

    public function getTime(): int
    {
        return $this->time;
    }
}

So when in my test I have this code:

class BoxTest extends TestCase
{
    public function it_has_an_estimated_waiting_time(): void
    {
        // Arrange
        $box = new Box(1, true);

        // Act
        $boxTime_20 = Mockery::mock(BoxTime::class)->shouldReceive('getTime')->andReturn(20)->getMock();
        $boxTime_50 = Mockery::mock(BoxTime::class)->shouldReceive('getTime')->andReturn(50)->getMock();
        $box->addBoxTime($boxTime_20);
        $box->addBoxTime($boxTime_50);

        // Assert
        $this->assertEquals(50, $box->estimatedWaitingTime());
    }
}

It returns this error when I try to add the mocked boxTimes with addBoxTime Expected BoxTime, got MockInterface

Aucun commentaire:

Enregistrer un commentaire