phptddrepository-patternbehatphpspec

PhpSpec testing service layers correctly


The application has several services and some of the functionality overlaps multiple services. Since PhpSpec wraps objects I am only able to test one service at a time.

Background:

The group service can create groups like "Red cars", "Blue cars". The car service can assign cars to groups.

Group Service:

function createGroup($name);

Car Service:

function assignCarToGroup($car, $group);

Problem:

When I'm describing the car service within PhpSpec I want to make sure that it can check if a car has been successfully assigned to a group.

To do that, the group needs to exist prior to assigning the car to it.

Questions:

I need to create the group before I run the file. This is where I face the problem.

Would I rely on the previous examples (of the GroupServiceSpec) to create the groups, I would make the test very brittle.

How can I test if the car has been assigned successfully just within the car service?

GroupService.php

<?php

namespace App;

class GroupService
{
    private $group_repository;

    public function __construct($group_repository)
    {
        $this->group_repository = $group_repository;
    }

    public function CreateGroupServiceCall($name)
    {
        return $this->group_repository->createGroup($name);
    }
}

GroupRepository.php

<?php

namespace App;

class GroupRepository
{
    public function createGroup($name)
    {
        file_put_contents($name . '.db', '');

        return true;
    }
}

CarService.php

<?php

namespace App;

class CarService
{
    private $car_repository;

    public function __construct($car_repository)
    {
        $this->car_repository = $car_repository;
    }

    public function AssignCarToGroupServiceCall($car, $group_name)
    {
        return $this->car_repository->assignCarToGroup($car, $group_name);
    }
}

CarRepository.php

<?php

namespace app;

class CarRepository
{
    public function assignCarToGroup($car, $name)
    {
        $file_path = $name . '.db';

        if (file_exists($file_path) == true) {
            file_put_contents($file_path, $car);

            return true;
        } else {
            return false;
        }
    }
}

And here are the tests:

GroupServiceSpec.php

<?php

namespace spec\App;

use App\GroupRepository;
use App\GroupService;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class GroupServiceSpec extends ObjectBehavior
{

    function let()
    {
        $group_repository = new GroupRepository();
        $this->beConstructedWith($group_repository);
    }

    function letGo()
    {
        # Clean up and remove all DB files
        array_map('unlink', glob(__DIR__ . "/../../*.db"));
    }

    function it_is_initializable()
    {
        $this->shouldHaveType(GroupService::class);
    }

    function it_can_create_a_group()
    {
        $this->CreateGroupServiceCall('New Group')->shouldBe(true);
    }

}

GroupRepositorySpec.php

<?php

namespace spec\App;

use App\GroupRepository;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class GroupRepositorySpec extends ObjectBehavior
{

    function letGo()
    {
        # Clean up and remove all DB files
        array_map('unlink', glob(__DIR__ . "/../../*.db"));
    }

    function it_is_initializable()
    {
        $this->shouldHaveType(GroupRepository::class);
    }

    function it_can_create_group()
    {
        $status = $this->createGroup('test');
        $status->shouldBe(true);
    }
}

CarServiceSpec.php

<?php

namespace spec\App;

use App\CarRepository;
use App\CarService;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class CarServiceSpec extends ObjectBehavior
{

    function let()
    {
        $car_repository = new CarRepository();
        $this->beConstructedWith($car_repository);
    }

    function it_is_initializable()
    {
        $this->shouldHaveType(CarService::class);
    }

    function it_can_assign_car_to_group()
    {
        $car = 'Car I';
        $group_name = 'Group1';

        $this->AssignCarToGroupServiceCall($car, $group_name)->shouldBe(true);
    }
}

CarRepositorySpec.php

<?php

namespace spec\App;

use app\CarRepository;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class CarRepositorySpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType(CarRepository::class);
    }

    function it_can_assign_car_to_group()
    {
        # how can I test this part, as the group is not existing now.
        # If I'm not to create the group, how can I test if this code works at all?
        # Do I have a design error?

        $car = 'Car1';
        $group_name = 'Company';

        $this->assignCarToGroup($car, $group_name)->shouldBe(true);
    }
}

Solution

  • Your car service spec should look like:

    class CarServiceSpec extends ObjectBehavior
    {
    
      function let(CarRepository $carRepository)
      {
        $this->beConstructedWith($carRepository);
      }
    
      function it_is_initializable()
      {
        $this->shouldHaveType(CarService::class);
      }
    
      function it_can_assign_car_to_group(CarRepository $carRepository)
      {
        $car = 'Car I';
        $group_name = 'Group1';
    
        $carRepository->assignCarToGroup($car, $group_name)->shouldBeCalled()->willReturn(true);
    
        $this->AssignCarToGroupServiceCall($car, $group_name)->shouldReturn(true);
      }
    }