Php 拉威尔积垢控制器试验

Php 拉威尔积垢控制器试验,php,laravel,testing,mocking,phpunit,Php,Laravel,Testing,Mocking,Phpunit,基本上,我必须为许多Laravel控制器编写测试,其中大多数是CRUD(读取、存储、更新),大多数逻辑都放在这些控制器中(继承的代码,不是我的) 我需要做的是从用户的角度自动化测试。因此,我需要点击所有端点,并对真实数据库进行测试,检查是否一切正常 我几乎没有测试经验,但据我所知,控制器应该通过集成/验收测试进行测试。现在,通过扩展Laravel的测试用例,我在测试读取方法方面做得很好,下面是一个示例: class SongsTest extends TestCase { public

基本上,我必须为许多Laravel控制器编写测试,其中大多数是CRUD(读取、存储、更新),大多数逻辑都放在这些控制器中(继承的代码,不是我的)

我需要做的是从用户的角度自动化测试。因此,我需要点击所有端点,并对真实数据库进行测试,检查是否一切正常

我几乎没有测试经验,但据我所知,控制器应该通过集成/验收测试进行测试。现在,通过扩展Laravel的测试用例,我在测试读取方法方面做得很好,下面是一个示例:

class SongsTest extends TestCase
{
    public function testBasicIndex()
    {   
        $arguments = [];

        $response = $this->call('GET', '/songs', $arguments);

        $this->assertResponseOk();
        $this->seeJson();
    }

    /**
        * @dataProvider providerSearchQuery
    */
    public function testSearchIndex($query)
    {
        $arguments = ['srquery' => $query];

        $response = $this->call('GET', '/songs', $arguments);

        $this->assertResponseOk();
        $this->seeJson();
    }

    public function providerSearchQuery()
    {
        return array(
            array('a'),
            array('as%+='),
            array('test?Someqsdag8hj$%$') 
            );
    }


    public function testGetSongsById()
    {   
        $id = 1;

        $response = $this->call('GET', '/songs/' . $id);

        $this->assertContains($response->getStatusCode(), [200, 404]);
        $this->seeJson();

        if($response->getStatusCode() == 404)
        {   
            $content = json_decode($response->getContent());
            $this->assertContains($content->message[0], ['Song not found', 'Item not active']);
        }
    }
}
这些测试命中端点并检查响应是否为200,格式是否为JSON以及其他一些内容。这些很好用

我的问题是:

比如说,我们有一个UserController和一个创建用户的方法。之后,应该在TokensController中使用所述用户创建一个令牌,该令牌应该以某种方式被记住,并在将来的测试中使用令牌保护请求

我的问题:

如何实现自动化:通过在测试数据库中创建真实用户来测试UserController的存储方法(无需模拟),通过使用该用户的电子邮件来测试TokensController的存储方法,使用创建的令牌测试其他控制器,并在测试完成后删除该令牌,以便再次执行测试


我无法将所有这些概念化,因为我还没有真正做过很多测试。

这是一个使用令牌和用户数据进行测试的示例-

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class PostTest extends TestCase
{
    use WithoutMiddleware;
    public $token = "lrSFMf0DpqRAh4BXTXWHp5VgFTq4CqA68qY3jG2CqvcpNTT6m0y9Qs6OdpSn";

/*
    A browser that receives a 302 response code HAS to redirect which means it will take the URL in the response and submit a new request. The result you see in your browser is the redirected page.

    Unit testing does not redirect. Your unit test is only doing what you direct it to do. If your unit test should test for the redirect then you evaluate the response and the correct assertion is 302 and not 200.
*/
public function testExample()
{
    $this->assertTrue(true);
}

public function testLogin()
{
    $this->visit('/')
     ->type('abc@gmail.com', 'email')
     ->type('123456', 'password')
     ->press('Login') // type submit - value / button - lable
     ->seePageIs('/Wall'); // for redirect url
} 


public function testFavourite()
{
    $this->testLogin();
    $request = [
        'user_id' => '172',
        'token'   => $this->token,
        'post_id' => '500'
    ];

    $response = $this->call('POST', '/DoFavoriteDisc',$request);
    $this->assertEquals(200, $response->getStatusCode());

}

}