如何在Symfony功能测试中模拟?

时间:2019-05-22 13:20:22

标签: php symfony testing phpunit symfony-3.4

我正在Symfony 3.4应用程序上创建功能测试。

<?php

namespace AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\HttpFoundation\Response;

class UserControllerTest extends WebTestCase
{
    /**
     * Connect to the website while being logged in
     * Logs in with (admin, password : a)
     */
    public function connection()
    {
        $client = static::createClient();
        $container = static::$kernel->getContainer();
        $session = $container->get('session');
        // Get the user (has to exist in the database)
        $person = self::$kernel->getContainer()->get('doctrine')->getRepository('AppBundle:User')->findOneByUsername('admin');
        $token = new UsernamePasswordToken($person, null, 'main', $person->getRoles());
        $session->set('_security_main', serialize($token));
        $session->save();
        $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));
        // Return the client
        return $client;
    }

    public function accessEditPage()
    {
        $client = $this->connection();
        $crawler = $client->request('GET', '/user/');
        $this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
        $this->assertContains(
                'Liste des utilisateurices',
                $client->getResponse()->getContent()
        );
        // Select the button of the user created for the test
        // Wont work if there are already more than 10 users in the database
        $link = $crawler
            ->filter('tr > td > a:contains("")')
            ->last()
            ->link()
        ;
        $crawler = $client->click($link);
        return array($client,$crawler);
    }

    /**
     * Create a new user
     */
    public function testCreate()
    {
        $client = $this->connection();
        $crawler = $client->request('GET', '/user/new');
        $this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());

        // Vérifie si la page affiche le bon texte
        $this->assertContains(
                'Enregistrer',
                $client->getResponse()->getContent()
        );

        // Select the form and fill its values
        $form = $crawler->selectButton(' Créer')->form();
        $values = $form->getPhpValues();
        $values['appbundle_user']['username'] = 'Jean';
        $values['appbundle_user']['plainPassword']['first'] = 'motdepasse';
        $values['appbundle_user']['plainPassword']['second'] = 'motdepasse';

        $crawler = $client->request($form->getMethod(), $form->getUri(), $values,$form->getPhpFiles());
        $crawler = $client->followRedirect();
        $this->assertContains(
                'Jean',
                $client->getResponse()->getContent()
        );
    }
}

当前,我的Controller测试创建数据库条目,并且依赖现有条目,这是一个问题。

我想模拟控制器中使用的存储库,以避免在测试控制器时创建条目,但是我找不到关于它的有用文档。由于找不到文档,我也想知道我想做的是一种好的做法。

0 个答案:

没有答案