PHPUnit dataProvider from database

Viewed 1174

I have simple PostControllerTest class:

<?php

namespace AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Client;

class PostControllerTest extends WebTestCase
{
    /**
     * @var Client
     */
    private $client;

    public function setUp() {
        $this->client = static::createClient();

    }

    public function idProvider()
    {
        return [
            ["1"],
            ["2"],
            ["3"],
        ];
    }

    /**
     * @dataProvider idProvider
     */
    public function testGet($id)
    {
        $this->client->request('GET', '/post/'.$id);
        $this->assertTrue($this->client->getResponse()->isSuccessful());
    }
}

testGet method uses idProvider for making request like /post/{id} and asserting successful response, where {id} is real id of Post entity in database.

Everything is OK, but what if Post with id = 1 is going to be removed?

I need to fetch a few id's from database, for example last or\and random 3 ones and pass it into testGet as dataProvider (or not dataProvider?)

I use Symfony 2.8.3 with PHPUnit 5.2.11.

How to solve it? Any advice is appreciated. Thanks!

2 Answers
Related