Search code examples
phpunitcodeception

Codeception improve readiness unit test


I'm new to test, I'm using codeception and phpunit to do some TDD.

However, my methods have a lot of code. Do I used best practices ? Is there a way to improve the readiness of my code, can it be 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);

    }



}

Solution

    1. You can probably extract body of testShouldHandleASuccessfulFormSubmissionForAddANews and testShouldHandleASuccessfulFormSubmissionForEditANews to other method with parameter like $action = 'add'|'edit' (or use your defined contants NewsFormHandler::EDIT etc), as they're almost the same.

    2. You can extract mock creation from above methods to a single parametrized method, as the process is almost the same (pass the differences as method arguments and let it do the dirty work).

    3. You can also add some readability by using BDD style, as in example in page http://codeception.com/docs/05-UnitTests#BDD-Specification-Testing