Last active
March 21, 2019 11:47
-
-
Save kanian/c230efc1031d47225f3d9ae4c464eb58 to your computer and use it in GitHub Desktop.
Sample Customer Service Unit Test with DI Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Assoa\Tests\Unit; | |
use PHPUnit\Framework\TestCase; | |
use Kanian\ContainerX\ContainerX; | |
use Assoa\DomainServices\Services\CustomerService; | |
use Assoa\Persistence\Repositories\Interfaces\CustomerRepositoryInterface; | |
class CustomerServiceTest extends TestCase | |
{ | |
private $customerRepository; | |
public function __construct(){ | |
$this->c = new ContainerX; | |
parent::__construct(); | |
} | |
protected function setUp() | |
{ | |
//some setup here | |
$this->c[CustomerRepositoryInterface::class] = $this->prophecy(CustomerRepositoryInterface::class); | |
$this->c['CustomerService'] = function ($c) { | |
return new CustomerService($c[CustomerRepositoryInterface::class]->reveal()); | |
}; | |
$this->customer = $this->objectMother->getUnSavedCustomer(); | |
$this->savedCustomer = $this->objectMother->getCustomer(); | |
} | |
public function testAddCustomerAdds() | |
{ | |
$this->c[CustomerRepositoryInterface::class]->save($this->customer)->shouldBeCalled() | |
->willReturn($this->savedCustomer->getId()); | |
$saved = $this->c['CustomerService']->create($this->customer->getName(), $this->customer->getEmail(), $this->customer->getPassword()); | |
$this->assertEquals($this->savedCustomer, $saved); | |
} | |
//... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment