S
S
Sergey Krivochenko2016-07-02 20:33:09
Yii
Sergey Krivochenko, 2016-07-02 20:33:09

How to write tests for a Yii2 application?

Good day to all. There are difficulties with understanding what needs to be tested using different types of tests. Difficulties are generated by various classifications of tests found on the Internet. Somewhere you can find the phrases "functional / acceptance", somewhere "functional / integration".
If you look at the tests that come with yii2-advanced and yii2-basic, the question arises why, in the presence of acceptance, functional are needed. In essence, they simply check for the presence of certain strings in the server's responses. In acceptance tests, you can do the same, and even with the ability to specify CSS selectors for the lines that you need to check for.
Additional questions are generated by the article . In Yii, even the banal

$model = \Mockery::mock(News::className())->shouldReceive('save')->once()->andReturn(true)->getMock();
$model->title = 'Первый пост';

generates a query to the database to get the structure of the table news. "Kill" all calls to the database, as I understand it, will not succeed. Especially so that the tests retain a readable form. What remains to be tested with unit tests in this case?
In the article cited above, the author calls the tests, in which there is interaction with the database, integration tests. The yii2-advanced and yii2-basic templates have unit, functional, acceptance. Is it worth highlighting a separate group of tests - integration?
By the sum of all of the above, it seems that integration==functional, and these tests are still not those that come in the Yii2 application templates. In my understanding, integration tests should, like unit tests, test individual class methods, but without any mocks, but using a database filled through fixtures. How right am I?
Thanks everyone for the replies.
UPD:
Let's say the model has a method:
public function setSuccessStatus()
{
    $this->status = self::STATUS_SUCCESS;
    return $this->save();
}

In the test of this method, make sure that it sets the correct model status:
public function testSetSuccessStatus()
{
    $post = $this->getMockBuilder('\app\model\Post')->setMethods(['save'])->getMock();
    $post->method('save')->willReturn(true);
    $post->setSuccessStatus();
    $this->assertEquals($post->status, Post::STATUS_SUCCESS);
}

We muted saving the model, but when accessed , the $post->statustest will get into the database to find out if there is a field in it status. Am I misunderstanding something?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Makarov, 2016-07-02
@krivochenko

In Yii, even the banal ... spawns a database query to get the structure of the news table.

Getting wet is real. I don’t know how through Mockery, but through PHPUnit I did quite well.
This name means that the tests are not strictly unit. They can be implemented by the same PHPUnit.
No. integration = tests the real base, but still in terms of code. functional = tests in terms of the end user: pages, URLs, response body parsing. In this case, the real browser does not start. acceptance = the same, but with a real browser.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question