add new demo symfony files
This commit is contained in:
194
app/tests/Controller/Admin/BlogControllerTest.php
Normal file
194
app/tests/Controller/Admin/BlogControllerTest.php
Normal file
@@ -0,0 +1,194 @@
|
||||
<?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 App\Tests\Controller\Admin;
|
||||
|
||||
use App\Repository\PostRepository;
|
||||
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/testing.html#functional-tests
|
||||
*
|
||||
* Whenever you test resources protected by a firewall, consider using the
|
||||
* technique explained in:
|
||||
* https://symfony.com/doc/current/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
|
||||
{
|
||||
/**
|
||||
* @dataProvider getUrlsForRegularUsers
|
||||
*/
|
||||
public function testAccessDeniedForRegularUsers(string $httpMethod, string $url): void
|
||||
{
|
||||
$client = static::createClient([], [
|
||||
'PHP_AUTH_USER' => 'john_user',
|
||||
'PHP_AUTH_PW' => 'kitten',
|
||||
]);
|
||||
|
||||
$client->request($httpMethod, $url);
|
||||
|
||||
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
|
||||
}
|
||||
|
||||
public function getUrlsForRegularUsers(): ?\Generator
|
||||
{
|
||||
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(): void
|
||||
{
|
||||
$client = static::createClient([], [
|
||||
'PHP_AUTH_USER' => 'jane_admin',
|
||||
'PHP_AUTH_PW' => 'kitten',
|
||||
]);
|
||||
$client->request('GET', '/en/admin/post/');
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$this->assertSelectorExists(
|
||||
'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(): void
|
||||
{
|
||||
$postTitle = 'Blog Post Title '.mt_rand();
|
||||
$postSummary = $this->generateRandomString(255);
|
||||
$postContent = $this->generateRandomString(1024);
|
||||
|
||||
$client = static::createClient([], [
|
||||
'PHP_AUTH_USER' => 'jane_admin',
|
||||
'PHP_AUTH_PW' => 'kitten',
|
||||
]);
|
||||
$client->request('GET', '/en/admin/post/new');
|
||||
$client->submitForm('Create post', [
|
||||
'post[title]' => $postTitle,
|
||||
'post[summary]' => $postSummary,
|
||||
'post[content]' => $postContent,
|
||||
]);
|
||||
|
||||
$this->assertResponseRedirects('/en/admin/post/', Response::HTTP_FOUND);
|
||||
|
||||
/** @var \App\Entity\Post $post */
|
||||
$post = self::$container->get(PostRepository::class)->findOneByTitle($postTitle);
|
||||
$this->assertNotNull($post);
|
||||
$this->assertSame($postSummary, $post->getSummary());
|
||||
$this->assertSame($postContent, $post->getContent());
|
||||
}
|
||||
|
||||
public function testAdminNewDuplicatedPost(): void
|
||||
{
|
||||
$postTitle = 'Blog Post Title '.mt_rand();
|
||||
$postSummary = $this->generateRandomString(255);
|
||||
$postContent = $this->generateRandomString(1024);
|
||||
|
||||
$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);
|
||||
|
||||
// post titles must be unique, so trying to create the same post twice should result in an error
|
||||
$client->submit($form);
|
||||
|
||||
$this->assertSelectorTextSame('form .form-group.has-error label', 'Title');
|
||||
$this->assertSelectorTextContains('form .form-group.has-error .help-block', 'This title was already used in another blog post, but they must be unique.');
|
||||
}
|
||||
|
||||
public function testAdminShowPost(): void
|
||||
{
|
||||
$client = static::createClient([], [
|
||||
'PHP_AUTH_USER' => 'jane_admin',
|
||||
'PHP_AUTH_PW' => 'kitten',
|
||||
]);
|
||||
$client->request('GET', '/en/admin/post/1');
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(): void
|
||||
{
|
||||
$newBlogPostTitle = 'Blog Post Title '.mt_rand();
|
||||
|
||||
$client = static::createClient([], [
|
||||
'PHP_AUTH_USER' => 'jane_admin',
|
||||
'PHP_AUTH_PW' => 'kitten',
|
||||
]);
|
||||
$client->request('GET', '/en/admin/post/1/edit');
|
||||
$client->submitForm('Save changes', [
|
||||
'post[title]' => $newBlogPostTitle,
|
||||
]);
|
||||
|
||||
$this->assertResponseRedirects('/en/admin/post/1/edit', Response::HTTP_FOUND);
|
||||
|
||||
/** @var \App\Entity\Post $post */
|
||||
$post = self::$container->get(PostRepository::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(): void
|
||||
{
|
||||
$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->assertResponseRedirects('/en/admin/post/', Response::HTTP_FOUND);
|
||||
|
||||
$post = self::$container->get(PostRepository::class)->find(1);
|
||||
$this->assertNull($post);
|
||||
}
|
||||
|
||||
private function generateRandomString(int $length): string
|
||||
{
|
||||
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
return mb_substr(str_shuffle(str_repeat($chars, ceil($length / mb_strlen($chars)))), 1, $length);
|
||||
}
|
||||
}
|
98
app/tests/Controller/BlogControllerTest.php
Normal file
98
app/tests/Controller/BlogControllerTest.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?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 App\Tests\Controller;
|
||||
|
||||
use App\Entity\Post;
|
||||
use App\Pagination\Paginator;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* Functional test for the controllers defined inside BlogController.
|
||||
*
|
||||
* See https://symfony.com/doc/current/testing.html#functional-tests
|
||||
*
|
||||
* Execute the application tests using this command (requires PHPUnit to be installed):
|
||||
*
|
||||
* $ cd your-symfony-project/
|
||||
* $ ./vendor/bin/phpunit
|
||||
*/
|
||||
class BlogControllerTest extends WebTestCase
|
||||
{
|
||||
public function testIndex(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$crawler = $client->request('GET', '/en/blog/');
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
|
||||
$this->assertCount(
|
||||
Paginator::PAGE_SIZE,
|
||||
$crawler->filter('article.post'),
|
||||
'The homepage displays the right number of posts.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testRss(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$crawler = $client->request('GET', '/en/blog/rss.xml');
|
||||
|
||||
$this->assertResponseHeaderSame('Content-Type', 'text/xml; charset=UTF-8');
|
||||
|
||||
$this->assertCount(
|
||||
Paginator::PAGE_SIZE,
|
||||
$crawler->filter('item'),
|
||||
'The xml file displays the right number of posts.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This test changes the database contents by creating a new comment. 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 testNewComment(): void
|
||||
{
|
||||
$client = static::createClient([], [
|
||||
'PHP_AUTH_USER' => 'john_user',
|
||||
'PHP_AUTH_PW' => 'kitten',
|
||||
]);
|
||||
$client->followRedirects();
|
||||
|
||||
// Find first blog post
|
||||
$crawler = $client->request('GET', '/en/blog/');
|
||||
$postLink = $crawler->filter('article.post > h2 a')->link();
|
||||
|
||||
$client->click($postLink);
|
||||
$crawler = $client->submitForm('Publish comment', [
|
||||
'comment[content]' => 'Hi, Symfony!',
|
||||
]);
|
||||
|
||||
$newComment = $crawler->filter('.post-comment')->first()->filter('div > p')->text();
|
||||
|
||||
$this->assertSame('Hi, Symfony!', $newComment);
|
||||
}
|
||||
|
||||
public function testAjaxSearch(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->xmlHttpRequest('GET', '/en/blog/search', ['q' => 'lorem']);
|
||||
|
||||
$results = json_decode($client->getResponse()->getContent(), true);
|
||||
|
||||
$this->assertResponseHeaderSame('Content-Type', 'application/json');
|
||||
$this->assertCount(1, $results);
|
||||
$this->assertSame('Lorem ipsum dolor sit amet consectetur adipiscing elit', $results[0]['title']);
|
||||
$this->assertSame('Jane Doe', $results[0]['author']);
|
||||
}
|
||||
}
|
95
app/tests/Controller/DefaultControllerTest.php
Normal file
95
app/tests/Controller/DefaultControllerTest.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?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 App\Tests\Controller;
|
||||
|
||||
use App\Entity\Post;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Functional test that implements a "smoke test" of all the public and secure
|
||||
* URLs of the application.
|
||||
* See https://symfony.com/doc/current/best_practices.html#smoke-test-your-urls.
|
||||
*
|
||||
* Execute the application tests using this command (requires PHPUnit to be installed):
|
||||
*
|
||||
* $ cd your-symfony-project/
|
||||
* $ ./vendor/bin/phpunit
|
||||
*/
|
||||
class DefaultControllerTest extends WebTestCase
|
||||
{
|
||||
/**
|
||||
* PHPUnit's data providers allow to execute the same tests repeated times
|
||||
* using a different set of data each time.
|
||||
* See https://symfony.com/doc/current/testing.html#testing-against-different-sets-of-data.
|
||||
*
|
||||
* @dataProvider getPublicUrls
|
||||
*/
|
||||
public function testPublicUrls(string $url): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', $url);
|
||||
|
||||
$this->assertResponseIsSuccessful(sprintf('The %s public URL loads correctly.', $url));
|
||||
}
|
||||
|
||||
/**
|
||||
* A good practice for tests is to not use the service container, to make
|
||||
* them more robust. However, in this example we must access to the container
|
||||
* to get the entity manager and make a database query. The reason is that
|
||||
* blog post fixtures are randomly generated and there's no guarantee that
|
||||
* a given blog post slug will be available.
|
||||
*/
|
||||
public function testPublicBlogPost(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
// the service container is always available via the test client
|
||||
$blogPost = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
|
||||
$client->request('GET', sprintf('/en/blog/posts/%s', $blogPost->getSlug()));
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
}
|
||||
|
||||
/**
|
||||
* The application contains a lot of secure URLs which shouldn't be
|
||||
* publicly accessible. This tests ensures that whenever a user tries to
|
||||
* access one of those pages, a redirection to the login form is performed.
|
||||
*
|
||||
* @dataProvider getSecureUrls
|
||||
*/
|
||||
public function testSecureUrls(string $url): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', $url);
|
||||
|
||||
$this->assertResponseRedirects(
|
||||
'http://localhost/en/login',
|
||||
Response::HTTP_FOUND,
|
||||
sprintf('The %s secure URL redirects to the login form.', $url)
|
||||
);
|
||||
}
|
||||
|
||||
public function getPublicUrls(): ?\Generator
|
||||
{
|
||||
yield ['/'];
|
||||
yield ['/en/blog/'];
|
||||
yield ['/en/login'];
|
||||
}
|
||||
|
||||
public function getSecureUrls(): ?\Generator
|
||||
{
|
||||
yield ['/en/admin/post/'];
|
||||
yield ['/en/admin/post/new'];
|
||||
yield ['/en/admin/post/1'];
|
||||
yield ['/en/admin/post/1/edit'];
|
||||
}
|
||||
}
|
99
app/tests/Controller/UserControllerTest.php
Normal file
99
app/tests/Controller/UserControllerTest.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?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 App\Tests\Controller;
|
||||
|
||||
use App\Repository\UserRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Functional test for the controllers defined inside the UserController used
|
||||
* for managing the current logged user.
|
||||
*
|
||||
* See https://symfony.com/doc/current/testing.html#functional-tests
|
||||
*
|
||||
* Whenever you test resources protected by a firewall, consider using the
|
||||
* technique explained in:
|
||||
* https://symfony.com/doc/current/testing/http_authentication.html
|
||||
*
|
||||
* Execute the application tests using this command (requires PHPUnit to be installed):
|
||||
*
|
||||
* $ cd your-symfony-project/
|
||||
* $ ./vendor/bin/phpunit
|
||||
*/
|
||||
class UserControllerTest extends WebTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getUrlsForAnonymousUsers
|
||||
*/
|
||||
public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $url): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request($httpMethod, $url);
|
||||
|
||||
$this->assertResponseRedirects(
|
||||
'http://localhost/en/login',
|
||||
Response::HTTP_FOUND,
|
||||
sprintf('The %s secure URL redirects to the login form.', $url)
|
||||
);
|
||||
}
|
||||
|
||||
public function getUrlsForAnonymousUsers(): ?\Generator
|
||||
{
|
||||
yield ['GET', '/en/profile/edit'];
|
||||
yield ['GET', '/en/profile/change-password'];
|
||||
}
|
||||
|
||||
public function testEditUser(): void
|
||||
{
|
||||
$newUserEmail = 'admin_jane@symfony.com';
|
||||
|
||||
$client = static::createClient([], [
|
||||
'PHP_AUTH_USER' => 'jane_admin',
|
||||
'PHP_AUTH_PW' => 'kitten',
|
||||
]);
|
||||
$client->request('GET', '/en/profile/edit');
|
||||
$client->submitForm('Save changes', [
|
||||
'user[email]' => $newUserEmail,
|
||||
]);
|
||||
|
||||
$this->assertResponseRedirects('/en/profile/edit', Response::HTTP_FOUND);
|
||||
|
||||
/** @var \App\Entity\User $user */
|
||||
$user = self::$container->get(UserRepository::class)->findOneByEmail($newUserEmail);
|
||||
|
||||
$this->assertNotNull($user);
|
||||
$this->assertSame($newUserEmail, $user->getEmail());
|
||||
}
|
||||
|
||||
public function testChangePassword(): void
|
||||
{
|
||||
$newUserPassword = 'new-password';
|
||||
|
||||
$client = static::createClient([], [
|
||||
'PHP_AUTH_USER' => 'jane_admin',
|
||||
'PHP_AUTH_PW' => 'kitten',
|
||||
]);
|
||||
$client->request('GET', '/en/profile/change-password');
|
||||
$client->submitForm('Save changes', [
|
||||
'change_password[currentPassword]' => 'kitten',
|
||||
'change_password[newPassword][first]' => $newUserPassword,
|
||||
'change_password[newPassword][second]' => $newUserPassword,
|
||||
]);
|
||||
|
||||
$this->assertResponseRedirects(
|
||||
'/en/logout',
|
||||
Response::HTTP_FOUND,
|
||||
'Changing password logout the user.'
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user