Forked from TyrekeYang/BusinessLogicService.php
Last active
October 16, 2018 01:46
-
-
Save hatelove/34693d15bf21f9f832a93cf20ddbe638 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 | |
/** | |
* Created by PhpStorm. | |
* User: joeychen | |
* Date: 2018/10/15 | |
* Time: 上午 11:47 | |
*/ | |
namespace App; | |
use stdClass; | |
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'); | |
echo('member not exist'); | |
$searchResponse->infoMsg = 'member can create'; | |
} | |
elseif ($member->created_user !== $ownerId) { | |
// log('member not created by this owner'); | |
echo('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 | |
/** | |
* Created by PhpStorm. | |
* User: joeychen | |
* Date: 2018/10/15 | |
* Time: 上午 11:49 | |
*/ | |
namespace Tests; | |
use App\BusinessLogicService; | |
use App\Repository; | |
use PHPUnit\Framework\TestCase; | |
use Mockery as m; | |
use stdClass; | |
class BusinessLogicServiceTest extends TestCase | |
{ | |
private $spyRepository; | |
/** | |
* @var BusinessLogicService | |
*/ | |
private $service; | |
private $AnyCode = 'test-code'; | |
private $AnyOwnerId = 91; | |
private $OtherOwnerId = -1; | |
public function setUp() | |
{ | |
parent::setUp(); | |
$this->spyRepository = m::spy(Repository::class); | |
// App::instance(Repository::class, $this->spyRepository); | |
$this->service = new BusinessLogicService($this->spyRepository); | |
// $this->service = App::make(Service::class); | |
} | |
/** | |
* Test search member by code. | |
*/ | |
public function test_get_member_from_response() | |
{ | |
$this->givenMember(1, $this->AnyOwnerId, 'Asia/Taipei', '[email protected]'); | |
$response = $this->service->searchMemberByCode($this->AnyOwnerId, $this->AnyCode); | |
$expectedMember = [ | |
'id' => 1, | |
'create_user' => $this->AnyOwnerId, | |
'timezone' => 'Asia/Taipei', | |
'email' => '[email protected]', | |
'company_name' => 'fake company', | |
]; | |
$this->shouldHasResult($response); | |
$this->responseMemberShouldBe($expectedMember, $response); | |
} | |
/** | |
* member not exist | |
*/ | |
public function test_member_not_exist() | |
{ | |
$this->givenNotExistMember(); | |
$response = $this->service->searchMemberByCode($this->AnyOwnerId, $this->AnyCode); | |
$this->messageShouldBe('member can create', $response); | |
} | |
/** | |
* without permission | |
*/ | |
public function test_without_permission() | |
{ | |
$this->givenMember(1, $this->OtherOwnerId, 'Asia/Taipei', '[email protected]'); | |
$response = $this->service->searchMemberByCode($this->AnyOwnerId, $this->AnyCode); | |
$this->messageShouldBe('member no permission', $response); | |
} | |
/** | |
* Setting member data. | |
* | |
* @param $id | |
* @param $ownId | |
* @param $timezone | |
* @param $email | |
*/ | |
private function givenMember($id, $ownId, $timezone, $email) | |
{ | |
$fakeMember = new stdClass(); | |
$fakeMember->id = $id; | |
$fakeMember->created_user = $ownId; | |
$fakeMember->timezone = $timezone; | |
$fakeMember->email = $email; | |
$fakeMember->company = new stdClass(); | |
$fakeMember->company->name = 'fake company'; | |
$this->spyRepository | |
->shouldReceive('getMemberByCode') | |
->andReturn($fakeMember); | |
} | |
/** | |
* @param $response | |
*/ | |
private function shouldHasResult($response): void | |
{ | |
$this->assertTrue($response->result); | |
} | |
/** | |
* @param $expectedMember | |
* @param $response | |
*/ | |
private function responseMemberShouldBe($expectedMember, $response): void | |
{ | |
$this->assertEquals($expectedMember['id'], $response->member->id); | |
$this->assertEquals($expectedMember['timezone'], $response->member->timezone); | |
$this->assertEquals($expectedMember['email'], $response->member->email); | |
$this->assertEquals($expectedMember['company_name'], $response->member->companyName); | |
} | |
private function givenNotExistMember() | |
{ | |
return null; | |
} | |
/** | |
* @param $expectedMessage | |
* @param $response | |
*/ | |
private function messageShouldBe($expectedMessage, $response): void | |
{ | |
$this->assertEquals($expectedMessage, $response->infoMsg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment