Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SWIMMING IN HELL

nrs
March 30, 2019

SWIMMING IN HELL

レガシーあるあるに対しての簡単な処方箋のような内容です。
2019/03/30 PHPer茶会における LT のスライドです。

HomePage: https://nrslib.com
Twitter: https://twitter.com/nrslib

nrs

March 30, 2019
Tweet

More Decks by nrs

Other Decks in Programming

Transcript

  1. <?php echo '-------------------------'; echo ' Introduction '; echo '-------------------------'; $name

    = 'Masanobu Naruse'; $job = ['Programmer', 'Developer Evangelist']; $company = 'GMO Internet, Inc. '; $team = ['UX UI', 'Developer Relations Team']; $twitter = '@nrslib'; $url = 'https://nrslib.com';
  2. public function confirm(Data $data) { var_dump($data); /* * array(3) *

    { * [0]=> string(9) "111|201|1" * [1]=> string(9) "112|201|3" * [2]=> string(9) "100|011|1" * } */ /* * なんかごっつい処理 */ }
  3. public function confirm(Data $data) { var_dump($data); /* * array(3) *

    { * [0]=> string(9) "111|201|1" * [1]=> string(9) "112|201|3" * [2]=> string(9) "100|011|1" * } */ /* * なんかごっつい処理 */ } 111|201|1 112|201|3 100|011|1
  4. public function confirm(Data $data) { var_dump($data); /* * array(3) *

    { * [0]=> string(9) "111|201|1" * [1]=> string(9) "112|201|3" * [2]=> string(9) "100|011|1" * } */ /* * なんかごっつい処理 */ } 111|201|1 112|201|3 100|011|1 Q. どういう意味 でしょうか
  5. A.

  6. クラス作ろう class Data { /** * @var string */ public

    $productCode; /** * @var string */ public $branchNumber; /** * @var string */ public $orderNum; }
  7. class HumbleController { public function orderConfirm(string $id){ $order = DB::table('orders')->where('id',

    $id)->first(); $response = $order; return view('order', compact('response')); } }
  8. class HumbleController { public function orderConfirm(string $id){ $order = DB::table('orders')->where('id',

    $id)->first(); $response = $order; return view('order', compact('response')); } } 金額計算は いずこに?
  9. 注文確認画面 <?php $price = floor((($response->unit_price * $response->num) * 1.08) -

    $response->discount); ?> <h1>注文確認</h1> <p> 注文番号 {{$response->id}} の合計金額は {{$price}} 円です </p>
  10. 注文履歴確認画面 <?php $price = floor((($response->unit_price * $response->num) * 1.08) -

    $response->discount); ?> <h1>注文履歴</h1> <p> 注文番号 {{$response->id}} の合計金額は {{$price}} 円です </p>
  11. フロントくん <ul> @foreach ($response->data as $order) <li> Name: {{$order->productName}} Status:

    <span style="color: {{$order->status}}"> @switch($order->status) @case('yellow') 入金待ち @break @case('green') 設定中 @break @case('blue') 稼働中 @break @case('red') エラー @break @endswitch </span> </li> @endforeach </ul>
  12. <ul> @foreach ($response->data as $order) <li> Name: {{$order->productName}} Status: <span

    style="color: {{$order->status}}"> @switch($order->status) @case('yellow') 入金待ち @break @case('green') 設定中 @break @case('blue') 稼働中 @break @case('red') エラー @break @endswitch </span> </li> @endforeach </ul> フロントくん サーバーから 受け取ったデータが 色に・・・?
  13. class MeddleController extends BaseController { public function orderList(string $userId) {

    $orders = DB::table('orders')->where('owner_id', $userId)->get()->toArray(); $response = new ProductOrderResponse(); $response->data = array_map(function($order) { $data = new ProductOrderData(); $data->productName = $order->productName; switch ($order->status){ case '1' : $data->status = 'yellow'; break; case '2' : $data->status = 'green'; break; case '3' : $data->status = 'blue'; break; case '-1' : $data->status = 'red'; break; default: $data->status = 'gray'; break; } $data->status = $order->status; return $data; }, $orders); return view('meddle.orders', compact('response')); } }
  14. class MeddleController extends BaseController { public function orderList(string $userId) {

    $orders = DB::table('orders')->where('owner_id', $userId)->get()->toArray(); $response = new ProductOrderResponse(); $response->data = array_map(function($order) { $data = new ProductOrderData(); $data->productName = $order->productName; switch ($order->status){ case '1' : $data->status = 'yellow'; break; case '2' : $data->status = 'green'; break; case '3' : $data->status = 'blue'; break; case '-1' : $data->status = 'red'; break; default: $data->status = 'gray'; break; } $data->status = $order->status; return $data; }, $orders); return view('meddle.orders', compact('response')); } }
  15. class MeddleController extends BaseController { public function orderList(string $userId) {

    $orders = DB::table('orders')->where('owner_id', $userId)->get()->toArray(); $response = new ProductOrderResponse(); $response->data = array_map(function($order) { $data = new ProductOrderData(); $data->productName = $order->productName; switch ($order->status){ case '1' : $data->status = 'yellow'; break; case '2' : $data->status = 'green'; break; case '3' : $data->status = 'blue'; break; case '-1' : $data->status = 'red'; break; default: $data->status = 'gray'; break; } return $data; }, $orders); return view('meddle.orders', compact('response')); } }
  16. class MeddleController extends BaseController { public function orderList(string $userId) {

    $orders = DB::table('orders')->where('owner_id', $userId)->get()->toArray(); $response = new ProductOrderResponse(); $response->data = array_map(function($order) { $data = new ProductOrderData(); $data->productName = $order->productName; switch ($order->status){ case '1' : $data->status = 'wait'; break; case '2' : $data->status = 'setting'; break; case '3' : $data->status = 'running'; break; case '-1' : $data->status = 'error'; break; default: $data->status = 'undefined'; break; } return $data; }, $orders); return view('meddle.orders', compact('response')); } }
  17. class MeddleController extends BaseController { public function orderList(string $userId) {

    $orders = DB::table('orders')->where('owner_id', $userId)->get()->toArray(); $response = new ProductOrderResponse(); $response->data = array_map(function($order) { $data = new ProductOrderData(); $data->productName = $order->productName; switch ($order->status){ case '1' : $data->status = 'wait'; break; case '2' : $data->status = 'setting'; break; case '3' : $data->status = 'running'; break; case '-1' : $data->status = 'error'; break; default: $data->status = 'undefined'; break; } return $data; }, $orders); return view('meddle.orders', compact('response')); } } フロントエンドの都合は バックエンドに書かないように するのがおすすめ
  18. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  19. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); } class User { public $id; public $name; public $dbError; public $error; public function loadByName(string $name){ $this->error = false; try{ $user = DB::table('users')->where('name', $name)->first(); if($user !== null){ $this->id = $user->id; $this->name = $user->name; return true; }else{ return false; } }catch (¥Exception $e){ $this->dbError = $e; $this->error = true; return false; }
  20. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  21. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  22. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  23. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); } class User { public $id; public $name; public $dbError; public $error; public function add() { $this->error = false; try { DB::table('users')->insert( [ 'id' => $this->id, 'name' => $this->name ] ); return true; } catch (¥Exception $e) { $this->error = true; $this->dbError = $e; return false; } } }
  24. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  25. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  26. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  27. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  28. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  29. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  30. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  31. public function create( Request $request, UserRepositoryInterface $userRepository ) { $name

    = $request->input('name'); $duplicated = $userRepository->findByName($name); if($duplicated !== null) { $viewModel = UserCreateResponse::fail(); return view('user.create', compact('viewModel')); } $userId = uniqid(); $user = new User($userId, $name); $userRepository->save($user); $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  32. public function create( Request $request, UserRepositoryInterface $userRepository ) { $name

    = $request->input('name'); $duplicated = $userRepository->findByName($name); if($duplicated !== null) { $viewModel = UserCreateResponse::fail(); return view('user.create', compact('viewModel')); } $userId = uniqid(); $user = new User($userId, $name); $userRepository->save($user); $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); } データの永続化を 担うオブジェクト を DI
  33. public function create( Request $request, UserRepositoryInterface $userRepository ) { $name

    = $request->input('name'); $duplicated = $userRepository->findByName($name); if($duplicated !== null) { $viewModel = UserCreateResponse::fail(); return view('user.create', compact('viewModel')); } $userId = uniqid(); $user = new User($userId, $name); $userRepository->save($user); $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); } リポジトリに 再構築・永続化を 依頼する
  34. DB のエラーは リポジトリで 例外投げて終わり (ここでは取り扱わない) public function create( Request $request,

    UserRepositoryInterface $userRepository ) { $name = $request->input('name'); $duplicated = $userRepository->findByName($name); if($duplicated !== null) { $viewModel = UserCreateResponse::fail(); return view('user.create', compact('viewModel')); } $userId = uniqid(); $user = new User($userId, $name); $userRepository->save($user); $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  35. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  36. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  37. public function create(Request $request) { $name = $request->input('name'); $user =

    new User(); $result = $user->loadByName($name); if ($user->error) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } // ユーザ名重複 if ($result) { $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $user->id = uniqid(); $user->name = $name; $result = $user->add(); if (!$result) { Logger::Log('error occurred. error:' . $user->dbError); $viewModel = new UserCreateResponse('', ''); return view('user.create', compact('viewModel')); } $viewModel = new UserCreateResponse($user->id, $user->name); return view('user.create', compact('viewModel')); }
  38. class UserService { private $userRepository; public function __construct(UserRepositoryInterface $userRepository) {

    $this->userRepository = $userRepository; } public function create(UserCreateRequest $request) { $userId = new UserId(uniqid()); $createdUser = new User($userId, $request->getName()); $this->userRepository->save($createdUser); return new UserCreateResponse($userId->getValue()); } }
  39. class UserService { private $userRepository; public function __construct(UserRepositoryInterface $userRepository) {

    $this->userRepository = $userRepository; } public function create(UserCreateRequest $request) { $userId = new UserId(uniqid()); $createdUser = new User($userId, $request->getName()); $this->userRepository->save($createdUser); return new UserCreateResponse($userId->getValue()); } } class UserController extends BaseController { public function create(UserService $service, Request $request) { $name = $request->input('name'); $request = new UserCreateRequest($name); $response = $service->create($request); $viewModel = new UserCreateViewModel($response->getCreatedUserId(), $name); return view('user.create', compact('viewModel')); } }
  40. class UserService { private $userRepository; public function __construct(UserRepositoryInterface $userRepository) {

    $this->userRepository = $userRepository; } public function create(UserCreateRequest $request) { $userId = new UserId(uniqid()); $createdUser = new User($userId, $request->getName()); $this->userRepository->save($createdUser); return new UserCreateResponse($userId->getValue()); } } class UserRepository implements UserRepositoryInterface { public function findByName(string $name) { $target = DB::table('users')->find('name', $name)->first(); return new User($target->id, $target->name); } public function save(User $user) { DB::table('users')->insert( [ 'id' => $user->id, 'name' => $user->name ] ); } } 本番用
  41. class UserService { private $userRepository; public function __construct(UserRepositoryInterface $userRepository) {

    $this->userRepository = $userRepository; } public function create(UserCreateRequest $request) { $userId = new UserId(uniqid()); $createdUser = new User($userId, $request->getName()); $this->userRepository->save($createdUser); return new UserCreateResponse($userId->getValue()); } } テスト用 class UserRepository implements UserRepositoryInterface { private $db = []; public function findByName(string $name) { $targets = array_filter($this->db, function ($e) use ($name) { return $e->name === $name; }); if (count($targets) > 0) { $target = array_shift($targets); return $this->cloneUser($target); } else { return null; } } public function save(User $user) { $cloned = $this->cloneUser($user); $this->db[$cloned->id] = $cloned; } private function cloneUser(User $user) { return new User($user->id, $user->name); } }
  42. class UserCreateTest extends TestCase { /** * テスト:正常にユーザを生成する * *

    @return void */ public function testValidCreate() { $repository = new InMemoryUserRepository(); $interactor = new UserService($repository); $request = new UserCreateRequest('test-name'); $response = $interactor->create($request); $this->assertNotNull($response); $this->assertNotNull($response->getCreatedUserId()); $userId = new UserId($response->getCreatedUserId()); $saved = $repository->find($userId); $this->assertEquals($saved->getName(), 'test-name'); } /** * テスト:重複エラー * * @return void */ public function testErrorXxx(){
  43. class UserCreateTest extends TestCase { /** * テスト:正常にユーザを生成する * *

    @return void */ public function testValidCreate() { $repository = new InMemoryUserRepository(); $interactor = new UserService($repository); $request = new UserCreateRequest('test-name'); $response = $interactor->create($request); $this->assertNotNull($response); $this->assertNotNull($response->getCreatedUserId()); $userId = new UserId($response->getCreatedUserId()); $saved = $repository->find($userId); $this->assertEquals($saved->getName(), 'test-name'); } /** * テスト:重複エラー * * @return void */ public function testErrorXxx(){ IN を表現
  44. class UserCreateTest extends TestCase { /** * テスト:正常にユーザを生成する * *

    @return void */ public function testValidCreate() { $repository = new InMemoryUserRepository(); $interactor = new UserService($repository); $request = new UserCreateRequest('test-name'); $response = $interactor->create($request); $this->assertNotNull($response); $this->assertNotNull($response->getCreatedUserId()); $userId = new UserId($response->getCreatedUserId()); $saved = $repository->find($userId); $this->assertEquals($saved->getName(), 'test-name'); } /** * テスト:重複エラー * * @return void */ public function testErrorXxx(){ IN を表現 OUT を表現
  45. class UserCreateTest extends TestCase { /** * テスト:正常にユーザを生成する * *

    @return void */ public function testValidCreate() { $repository = new InMemoryUserRepository(); $interactor = new UserService($repository); $request = new UserCreateRequest('test-name'); $response = $interactor->create($request); $this->assertNotNull($response); $this->assertNotNull($response->getCreatedUserId()); $userId = new UserId($response->getCreatedUserId()); $saved = $repository->find($userId); $this->assertEquals($saved->getName(), 'test-name'); } /** * テスト:重複エラー * * @return void */ public function testErrorXxx(){ IN を表現 OUT を表現 これこそ仕様書