Skip to content

Instantly share code, notes, and snippets.

@kanian
Last active March 21, 2019 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanian/c230efc1031d47225f3d9ae4c464eb58 to your computer and use it in GitHub Desktop.
Save kanian/c230efc1031d47225f3d9ae4c464eb58 to your computer and use it in GitHub Desktop.
Sample Customer Service Unit Test with DI Container
<?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