mirror of
https://github.com/symfony/demo.git
synced 2026-03-24 00:02:32 +01:00
170 lines
5.8 KiB
PHP
170 lines
5.8 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Symfony package.
|
|
*
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Tests\AppBundle\Controller\Admin;
|
|
|
|
use AppBundle\DataFixtures\FixturesTrait;
|
|
use AppBundle\Entity\Post;
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
/**
|
|
* Functional test for the controllers defined inside the BlogController used
|
|
* for managing the blog in the backend.
|
|
*
|
|
* See https://symfony.com/doc/current/book/testing.html#functional-tests
|
|
*
|
|
* Whenever you test resources protected by a firewall, consider using the
|
|
* technique explained in:
|
|
* https://symfony.com/doc/current/cookbook/testing/http_authentication.html
|
|
*
|
|
* Execute the application tests using this command (requires PHPUnit to be installed):
|
|
*
|
|
* $ cd your-symfony-project/
|
|
* $ ./vendor/bin/phpunit
|
|
*/
|
|
class BlogControllerTest extends WebTestCase
|
|
{
|
|
use FixturesTrait;
|
|
|
|
/**
|
|
* @dataProvider getUrlsForRegularUsers
|
|
*/
|
|
public function testAccessDeniedForRegularUsers($httpMethod, $url)
|
|
{
|
|
$client = static::createClient([], [
|
|
'PHP_AUTH_USER' => 'john_user',
|
|
'PHP_AUTH_PW' => 'kitten',
|
|
]);
|
|
|
|
$client->request($httpMethod, $url);
|
|
$this->assertSame(Response::HTTP_FORBIDDEN, $client->getResponse()->getStatusCode());
|
|
}
|
|
|
|
public function getUrlsForRegularUsers()
|
|
{
|
|
yield ['GET', '/en/admin/post/'];
|
|
yield ['GET', '/en/admin/post/1'];
|
|
yield ['GET', '/en/admin/post/1/edit'];
|
|
yield ['POST', '/en/admin/post/1/delete'];
|
|
}
|
|
|
|
public function testAdminBackendHomePage()
|
|
{
|
|
$client = static::createClient([], [
|
|
'PHP_AUTH_USER' => 'jane_admin',
|
|
'PHP_AUTH_PW' => 'kitten',
|
|
]);
|
|
|
|
$crawler = $client->request('GET', '/en/admin/post/');
|
|
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
|
|
|
|
$this->assertCount(
|
|
30,
|
|
$crawler->filter('body#admin_post_index #main tbody tr'),
|
|
'The backend homepage displays all the available posts.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* This test changes the database contents by creating a new blog post. However,
|
|
* thanks to the DAMADoctrineTestBundle and its PHPUnit listener, all changes
|
|
* to the database are rolled back when this test completes. This means that
|
|
* all the application tests begin with the same database contents.
|
|
*/
|
|
public function testAdminNewPost()
|
|
{
|
|
$postTitle = 'Blog Post Title '.mt_rand();
|
|
$postSummary = $this->getRandomPostSummary();
|
|
$postContent = $this->getPostContent();
|
|
|
|
$client = static::createClient([], [
|
|
'PHP_AUTH_USER' => 'jane_admin',
|
|
'PHP_AUTH_PW' => 'kitten',
|
|
]);
|
|
$crawler = $client->request('GET', '/en/admin/post/new');
|
|
$form = $crawler->selectButton('Create post')->form([
|
|
'post[title]' => $postTitle,
|
|
'post[summary]' => $postSummary,
|
|
'post[content]' => $postContent,
|
|
]);
|
|
$client->submit($form);
|
|
|
|
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
|
|
|
|
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->findOneBy([
|
|
'title' => $postTitle,
|
|
]);
|
|
$this->assertNotNull($post);
|
|
$this->assertSame($postSummary, $post->getSummary());
|
|
$this->assertSame($postContent, $post->getContent());
|
|
}
|
|
|
|
public function testAdminShowPost()
|
|
{
|
|
$client = static::createClient([], [
|
|
'PHP_AUTH_USER' => 'jane_admin',
|
|
'PHP_AUTH_PW' => 'kitten',
|
|
]);
|
|
$client->request('GET', '/en/admin/post/1');
|
|
|
|
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
|
|
}
|
|
|
|
/**
|
|
* This test changes the database contents by editing a blog post. However,
|
|
* thanks to the DAMADoctrineTestBundle and its PHPUnit listener, all changes
|
|
* to the database are rolled back when this test completes. This means that
|
|
* all the application tests begin with the same database contents.
|
|
*/
|
|
public function testAdminEditPost()
|
|
{
|
|
$newBlogPostTitle = 'Blog Post Title '.mt_rand();
|
|
|
|
$client = static::createClient([], [
|
|
'PHP_AUTH_USER' => 'jane_admin',
|
|
'PHP_AUTH_PW' => 'kitten',
|
|
]);
|
|
$crawler = $client->request('GET', '/en/admin/post/1/edit');
|
|
$form = $crawler->selectButton('Save changes')->form([
|
|
'post[title]' => $newBlogPostTitle,
|
|
]);
|
|
$client->submit($form);
|
|
|
|
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
|
|
|
|
/** @var Post $post */
|
|
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
|
|
$this->assertSame($newBlogPostTitle, $post->getTitle());
|
|
}
|
|
|
|
/**
|
|
* This test changes the database contents by deleting a blog post. However,
|
|
* thanks to the DAMADoctrineTestBundle and its PHPUnit listener, all changes
|
|
* to the database are rolled back when this test completes. This means that
|
|
* all the application tests begin with the same database contents.
|
|
*/
|
|
public function testAdminDeletePost()
|
|
{
|
|
$client = static::createClient([], [
|
|
'PHP_AUTH_USER' => 'jane_admin',
|
|
'PHP_AUTH_PW' => 'kitten',
|
|
]);
|
|
$crawler = $client->request('GET', '/en/admin/post/1');
|
|
$client->submit($crawler->filter('#delete-form')->form());
|
|
|
|
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
|
|
|
|
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
|
|
$this->assertNull($post);
|
|
}
|
|
}
|