44
55use App \DTO \Request \PostedArticle ;
66use App \Entity \Article ;
7+ use App \Model \ArticleCounterInterface ;
78use App \Model \ArticlePersisterInterface ;
9+ use App \Service \UseCase \Article \Exception \RegisterException ;
810use App \Service \UseCase \Article \RegisterUseCase ;
911use PHPUnit \Framework \TestCase ;
12+ use Prophecy \Argument ;
1013use Prophecy \Prophecy \ObjectProphecy ;
1114
1215class RegisterUseCaseTest extends TestCase
@@ -16,22 +19,49 @@ class RegisterUseCaseTest extends TestCase
1619 */
1720 private $ articlePersisterP ;
1821
22+ /**
23+ * @var ArticleCounterInterface|ObjectProphecy
24+ */
25+ private $ articleCounterP ;
26+
1927 protected function setUp (): void
2028 {
2129 $ this ->articlePersisterP = $ this ->prophesize (ArticlePersisterInterface::class);
30+ $ this ->articleCounterP = $ this ->prophesize (ArticleCounterInterface::class);
31+ }
32+
33+ protected function tearDown (): void
34+ {
35+ $ this ->articlePersisterP = null ;
36+ $ this ->articleCounterP = null ;
2237 }
2338
2439 public function test ()
2540 {
2641 $ postedArticle = $ this ->prophesize (PostedArticle::class)->reveal ();
2742
2843 $ this ->articlePersisterP ->persist ($ postedArticle , new Article ())->willReturnArgument (1 )->shouldBeCalled ();
44+ $ this ->articleCounterP ->count (Argument::type (Article::class))->shouldBeCalled ();
45+
46+ $ this ->getSUT ()->register ($ postedArticle );
47+ }
48+
49+ public function test_保存でエラーが出たらcountは更新しない ()
50+ {
51+ $ this ->expectException (RegisterException::class);
52+
53+ $ postedArticle = $ this ->prophesize (PostedArticle::class)->reveal ();
54+ $ this ->articlePersisterP ->persist ($ postedArticle , new Article ())->willThrow ( new \Exception ('dummy-error ' ))->shouldBeCalled ();
55+ $ this ->articleCounterP ->count (Argument::any ())->shouldNotBeCalled ();
2956
3057 $ this ->getSUT ()->register ($ postedArticle );
3158 }
3259
3360 private function getSUT (): RegisterUseCase
3461 {
35- return new RegisterUseCase ($ this ->articlePersisterP ->reveal ());
62+ return new RegisterUseCase (
63+ $ this ->articlePersisterP ->reveal (),
64+ $ this ->articleCounterP ->reveal ()
65+ );
3666 }
3767}
0 commit comments