Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mathiasverraes/7039404 to your computer and use it in GitHub Desktop.
Save mathiasverraes/7039404 to your computer and use it in GitHub Desktop.
PHPUnit + Prophecy vs PHPUnit + PHPUnit MockObject
<?php
namespace Model\WeekPlanning\Tests\Activity;
use Dsp\CoreBundle\Entity\FosUser;
use Dsp\CoreBundle\Entity\School;
use Dsp\CoreBundle\Tests\DspTest;
use Dsp\MaterialsBundle\Entity\Material;
use Dsp\MaterialsBundle\Entity\MaterialRepository;
use Dsp\MaterialsBundle\Entity\OwnedMaterial;
use Model\Curriculum\ClusteredLearningGoal\ClusteredLearningGoal;
use Model\Curriculum\ClusteredLearningGoal\ClusteredLearningGoalId;
use Model\Curriculum\Domain\Domain;
use Model\Curriculum\HeadStructure\HeadStructure;
use Model\Curriculum\Subject\Subject;
use Model\Taxonomy\Category\Category;
use Model\WeekPlanning\Activity\AvailableActivity;
use Model\WeekPlanning\Activity\AvailableActivityRepository;
use Model\WeekPlanning\Activity\CoupleMaterialToAvailableActivity;
use Model\WeekPlanning\Activity\CoupleMaterialToAvailableActivityCommandHandler;
use PHPUnit_Framework_MockObject_MockObject;
use Prophecy\Prophecy\ObjectProphecy;
final class CoupleMaterialToAvailableActivityCommandHandlerTest extends DspTest
{
const AVAILABLE_ACTIVITY_ID = 123;
const MATERIAL_ID = 456;
/**
* @var AvailableActivity
*/
private $availableActivity;
/**
* @var Material
*/
private $material;
/**
* @var MaterialRepository | PHPUnit_Framework_MockObject_MockObject
*/
private $materialRepository;
/**
* @var AvailableActivityRepository | PHPUnit_Framework_MockObject_MockObject
*/
private $availableActivityRepository;
/**
* @var CoupleMaterialToAvailableActivityCommandHandler
*/
private $commandHandler;
public function setUp()
{
parent::setUp();
$this->availableActivityRepository = $this->getMock('Model\WeekPlanning\Activity\AvailableActivityRepository');
$this->materialRepository = $this->getMock('Dsp\MaterialsBundle\Entity\MaterialRepository');
$this->commandHandler = new CoupleMaterialToAvailableActivityCommandHandler($this->availableActivityRepository, $this->materialRepository);
$clusteredLearningGoal1 = new ClusteredLearningGoal(new ClusteredLearningGoalId('cluster123'), 'De bomen leren herkennen', new Subject('Boomchirurgie'), new Domain('Anatomie'), new HeadStructure('111'));
$clusteredLearningGoal2 = new ClusteredLearningGoal(new ClusteredLearningGoalId('cluster456'), 'De bomen leren omhakken', new Subject('Boomchirurgie'), new Domain('Anatomie'), new HeadStructure('111'));
$school = $this->getMock(School::fqcn(), [], [], '', false);
$this->availableActivity = AvailableActivity::define($school, new FosUser(), 'Boswandeling', new Category('Uitstap'), [$clusteredLearningGoal1, $clusteredLearningGoal2]);
$this->material = new OwnedMaterial;
}
/**
* @test
*/
public function it_should_couple_a_material()
{
$this->availableActivityRepository
->expects(once())
->method('find')
->with(self::AVAILABLE_ACTIVITY_ID)
->will(returnValue($this->availableActivity));
$this->materialRepository
->expects(once())
->method('find')
->with(self::MATERIAL_ID)
->will(returnValue($this->material));
$this->commandHandler->handle(new CoupleMaterialToAvailableActivity(self::AVAILABLE_ACTIVITY_ID, self::MATERIAL_ID));
assertThat(
$this->availableActivity->getCoupledMaterials(),
isEqualTo([$this->material])
);
}
}
<?php
namespace Model\WeekPlanning\Tests\Activity;
use Dsp\CoreBundle\Entity\FosUser;
use Dsp\CoreBundle\Entity\School;
use Dsp\CoreBundle\Tests\DspTest;
use Dsp\MaterialsBundle\Entity\Material;
use Dsp\MaterialsBundle\Entity\MaterialRepository;
use Dsp\MaterialsBundle\Entity\OwnedMaterial;
use Model\Curriculum\ClusteredLearningGoal\ClusteredLearningGoal;
use Model\Curriculum\ClusteredLearningGoal\ClusteredLearningGoalId;
use Model\Curriculum\Domain\Domain;
use Model\Curriculum\HeadStructure\HeadStructure;
use Model\Curriculum\Subject\Subject;
use Model\Taxonomy\Category\Category;
use Model\WeekPlanning\Activity\AvailableActivity;
use Model\WeekPlanning\Activity\AvailableActivityRepository;
use Model\WeekPlanning\Activity\CoupleMaterialToAvailableActivity;
use Model\WeekPlanning\Activity\CoupleMaterialToAvailableActivityCommandHandler;
use Prophecy\Prophecy\ObjectProphecy;
final class CoupleMaterialToAvailableActivityCommandHandlerWithProphecyTest extends DspTest
{
const AVAILABLE_ACTIVITY_ID = 123;
const MATERIAL_ID = 456;
/**
* @var AvailableActivity
*/
private $availableActivity;
/**
* @var Material
*/
private $material;
/**
* @var MaterialRepository | ObjectProphecy
*/
private $materialRepository;
/**
* @var AvailableActivityRepository | ObjectProphecy
*/
private $availableActivityRepository;
/**
* @var CoupleMaterialToAvailableActivityCommandHandler
*/
private $commandHandler;
public function setUp()
{
parent::setUp();
$this->availableActivityRepository = $this->prophet->prophesize('Model\WeekPlanning\Activity\AvailableActivityRepository');
$this->materialRepository = $this->prophet->prophesize('Dsp\MaterialsBundle\Entity\MaterialRepository');
$this->commandHandler = new CoupleMaterialToAvailableActivityCommandHandler($this->availableActivityRepository->reveal(), $this->materialRepository->reveal());
$clusteredLearningGoal1 = new ClusteredLearningGoal(new ClusteredLearningGoalId('cluster123'), 'De bomen leren herkennen', new Subject('Boomchirurgie'), new Domain('Anatomie'), new HeadStructure('111'));
$clusteredLearningGoal2 = new ClusteredLearningGoal(new ClusteredLearningGoalId('cluster456'), 'De bomen leren omhakken', new Subject('Boomchirurgie'), new Domain('Anatomie'), new HeadStructure('111'));
$school = $this->prophet->prophesize(School::fqcn());
$this->availableActivity = AvailableActivity::define($school->reveal(), new FosUser(), 'Boswandeling', new Category('Uitstap'), [$clusteredLearningGoal1, $clusteredLearningGoal2]);
$this->material = new OwnedMaterial;
}
/**
* @test
*/
public function it_should_couple_a_material()
{
$this->availableActivityRepository
->find(self::AVAILABLE_ACTIVITY_ID)
->shouldBeCalled()
->willReturn($this->availableActivity);
$this->materialRepository
->find(self::MATERIAL_ID)
->shouldBeCalled()
->willReturn($this->material);
$this->commandHandler->handle(new CoupleMaterialToAvailableActivity(self::AVAILABLE_ACTIVITY_ID, self::MATERIAL_ID));
assertThat(
$this->availableActivity->getCoupledMaterials(),
isEqualTo([$this->material])
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment