Last active
October 16, 2018 01:45
-
-
Save TyrekeYang/a9161571f27d7a57da376b60539e6f3d to your computer and use it in GitHub Desktop.
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 | |
class BusinessLogicService | |
{ | |
/** | |
* @var Repository | |
*/ | |
protected $repository; | |
public function __construct(Repository $repository) { | |
$this->repository = $repository; | |
} | |
public function searchMemberByCode($ownerId, $code) | |
{ | |
$searchResponse = new stdClass(); | |
$searchResponse->result = false; | |
$searchResponse->member = new stdClass(); | |
$searchResponse->infoMsg = ''; | |
$member = $this->repository->getMemberByCode($code); | |
if (is_null($member)) { | |
log('member not exist'); | |
$searchResponse->infoMsg = 'member can create'; | |
} elseif ($member->created_user !== $ownerId) { | |
log('member not created by this owner'); | |
$searchResponse->infoMsg = 'member no permission'; | |
} else { | |
$searchResponse->member->id = $member->id; | |
$searchResponse->member->timezone = $member->timezone; | |
$searchResponse->member->email = $member->email; | |
$searchResponse->member->companyName = $member->company->name; | |
$searchResponse->result = true; | |
} | |
return $searchResponse; | |
} | |
} |
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 | |
class BusinessLogicServiceTest extends TestCase | |
{ | |
/** | |
* @var MockInterface | |
*/ | |
private $spyRepository; | |
/** | |
* @var Service | |
*/ | |
private $service; | |
public function setUp() | |
{ | |
parent::setUp(); // TODO: Change the autogenerated stub | |
$this->spyRepository = Mockery::spy(Repository::class); | |
App::instance(Repository::class, $this->spyRepository); | |
$this->service = App::make(Service::class); | |
} | |
/** | |
* Test search member by code. | |
*/ | |
public function testSearchMemberByCode() | |
{ | |
$ownerId = 1; | |
$memberData = $this->memberDataFormatter($ownerId); | |
$this->givenMember($memberData); | |
$this->shouldReceiveCorrectResponseFormat($ownerId, 'test-code', $memberData); | |
} | |
/** | |
* Setting member data. | |
* | |
* @param $memberData | |
*/ | |
private function givenMember($memberData) | |
{ | |
$fakeMember = new stdClass(); | |
$fakeMember->id = $memberData['id']; | |
$fakeMember->created_user = $memberData['create_user']; | |
$fakeMember->timezone = $memberData['timezone']; | |
$fakeMember->email = $memberData['email']; | |
$fakeMember->company = new stdClass(); | |
$fakeMember->company->name = $memberData['company_name']; | |
$this->spyRepository | |
->shouldReceive('getMemberByCode') | |
->with('test-code') | |
->andReturn($fakeMember); | |
} | |
/** | |
* Verify response format. | |
* | |
* @param $ownerId | |
* @param $code | |
* @param $fakeMember | |
*/ | |
private function shouldReceiveCorrectResponseFormat($ownerId, $code, $fakeMember) | |
{ | |
$response = $this->service->searchMemberByCode($ownerId, $code); | |
$this->assertTrue($response->result); | |
$this->assertEquals($fakeMember['id'], $response->member->id); | |
$this->assertEquals($fakeMember['timezone'], $response->member->timezone); | |
$this->assertEquals($fakeMember['email'], $response->member->email); | |
$this->assertEquals($fakeMember['company_name'], $response->member->companyName); | |
} | |
/** | |
* Format member data. | |
* | |
* @param $ownerId | |
* @return array | |
*/ | |
private function memberDataFormatter($ownerId): array | |
{ | |
$memberData = [ | |
'id' => 1, | |
'create_user' => $ownerId, | |
'timezone' => 'Asia/Taipei', | |
'email' => '[email protected]', | |
'company_name' => 'fake company', | |
]; | |
return $memberData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment