phpunit - Codeception improve readiness unit test -
i'm new test, i'm using codeception , phpunit tdd.
however, methods have lot of code. used best practices ? there way improve readiness of code, can more clean ?
class newsformhandlertest extends \codeception\test\unit { /** * @var \unittester */ protected $tester; protected function _before() { } protected function _after() { } private function getformmock(){ return $this->getmockbuilder(forminterface::class) ->disableoriginalconstructor() ->getmock(); } private function getnewsmanagermock(){ return $this->getmockbuilder(inewsmanager::class) ->disableoriginalconstructor() ->getmock(); } // tests public function testshouldhandleasuccessfulformsubmissionforaddanews() { // prepare $request = new \symfony\component\httpfoundation\request(); $news = new news(); $form = $this->getformmock(); $form->expects($this->once()) ->method('isvalid') ->will($this->returnvalue(true)); $form->expects($this->once()) ->method('submit'); $form->expects($this->once()) ->method('getdata') ->will($this->returnvalue($news)); $newsmanager = $this->getnewsmanagermock(); $newsmanager->expects($this->once()) ->method('add'); $user = stub::make(webserviceuser::class, []); // test $handler = new newsformhandler($newsmanager, $user); $newsreturned = $handler->handle($form, $request, newsformhandler::add); // assert $this->assertinstanceof(news::class, $newsreturned); $this->assertequals($news, $newsreturned); } public function testshouldhandleasuccessfulformsubmissionforeditanews() { // prepare $request = new \symfony\component\httpfoundation\request(); $news = new news(); $form = $this->getformmock(); $form->expects($this->once()) ->method('isvalid') ->will($this->returnvalue(true)); $form->expects($this->once()) ->method('submit'); $form->expects($this->once()) ->method('getdata') ->will($this->returnvalue($news)); $newsmanager = $this->getnewsmanagermock(); $newsmanager->expects($this->once()) ->method('edit'); $user = stub::make(webserviceuser::class, []); // test $handler = new newsformhandler($newsmanager, $user); $newsreturned = $handler->handle($form, $request, newsformhandler::edit); // assert $this->assertinstanceof(news::class, $newsreturned); $this->assertequals($news, $newsreturned); } public function testfailformwithinvaliddata() { // prepare $request = new \symfony\component\httpfoundation\request(); $form = $this->getformmock(); $form->expects($this->once()) ->method('isvalid') ->will($this->returnvalue(false)); $newsmanager = $this->getnewsmanagermock(); $newsmanager->expects($this->never()) ->method('edit'); $this->expectexception(invalidformexception::class); $user = stub::make(webserviceuser::class, []); // test $handler = new newsformhandler($newsmanager, $user); $newsreturned = $handler->handle($form, $request, newsformhandler::add); // assert $this->assertnull($newsreturned); } }
you can extract body of testshouldhandleasuccessfulformsubmissionforaddanews , testshouldhandleasuccessfulformsubmissionforeditanews other method parameter $action = 'add'|'edit' (or use defined contants newsformhandler::edit etc), they're same.
you can extract mock creation above methods single parametrized method, process same (pass differences method arguments , let dirty work).
you can add readability using bdd style, in example in page http://codeception.com/docs/05-unittests#bdd-specification-testing
Comments
Post a Comment