Skip to content
This repository was archived by the owner on May 2, 2023. It is now read-only.

Commit dd3c498

Browse files
committed
Some tests
1 parent 9987199 commit dd3c498

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

.env.test

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This file is a "template" of which env vars need to be defined for your application
2+
# Copy this file to .env file for development, create environment variables when deploying to production
3+
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration
4+
5+
###> symfony/framework-bundle ###
6+
APP_ENV=dev
7+
APP_SECRET=68bbee1defe23f927e4e61f7d848609c
8+
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
9+
#TRUSTED_HOSTS=localhost,example.com
10+
###< symfony/framework-bundle ###
11+
12+
###> symfony/swiftmailer-bundle ###
13+
# For Gmail as a transport, use: "gmail://username:password@localhost"
14+
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
15+
# Delivery is disabled by default via "null://localhost"
16+
MAILER_URL=null://localhost
17+
###< symfony/swiftmailer-bundle ###
18+
19+
###> doctrine/doctrine-bundle ###
20+
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
21+
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
22+
# Configure your db driver and server_version in config/packages/doctrine.yaml
23+
DATABASE_URL=mysql://travis:@127.0.0.1:3306/symfony
24+
###< doctrine/doctrine-bundle ###

phpunit.xml.dist

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
bootstrap="vendor/autoload.php"
9+
>
10+
<php>
11+
<ini name="error_reporting" value="-1" />
12+
<env name="KERNEL_CLASS" value="App\Kernel" />
13+
<env name="APP_ENV" value="test" />
14+
<env name="APP_DEBUG" value="1" />
15+
<env name="APP_SECRET" value="s$cretf0rt3st" />
16+
<env name="SHELL_VERBOSITY" value="-1" />
17+
<!-- define your env variables for the test env here -->
18+
<env name="DATABASE_URL" value="mysql://root:root@127.0.0.1:3306/symfony" />
19+
</php>
20+
21+
<testsuites>
22+
<testsuite name="Project Test Suite">
23+
<directory>tests/</directory>
24+
</testsuite>
25+
</testsuites>
26+
27+
<filter>
28+
<whitelist>
29+
<directory>./src/</directory>
30+
</whitelist>
31+
</filter>
32+
33+
<listeners>
34+
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
35+
</listeners>
36+
</phpunit>

tests/OrmTest.php

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace App\Tests;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
use App\Tests\Utils\LoggedUserTrait;
7+
use App\Entity\Todo;
8+
use App\Application\Sonata\UserBundle\Entity\User;
9+
10+
11+
class PostControllerTest extends WebTestCase
12+
{
13+
use LoggedUserTrait;
14+
15+
private $todo;
16+
private $user;
17+
18+
protected function setUp()
19+
{
20+
self::bootKernel();
21+
22+
$this->em = static::$kernel->getContainer()
23+
->get('doctrine')
24+
->getManager();
25+
$user = new User();
26+
$user->setUserName('TestUserName');
27+
$user->setFirstName('TestAdmin');
28+
$user->setLastname('TestSurname');
29+
$user->setUsername('testadmin');
30+
$user->setEmail('email@tests.com');
31+
$user->setPlainPassword('testadmin');
32+
$user->setEnabled(true);
33+
$user->setRoles(array('ROLE_SONATA_ADMIN', 'ROLE_SUPER_ADMIN'));
34+
35+
$this->em->persist($user);
36+
$this->user = $user;
37+
38+
$todo = new Todo();
39+
$todo->setTitle('Test title');
40+
$todo->setOwner($user);
41+
$todo->setCompleted(false);
42+
43+
$this->em->persist($todo);
44+
$this->em->flush();
45+
$this->todo = $todo;
46+
47+
}
48+
49+
protected function tearDown()
50+
{
51+
//Cleaning things :)
52+
$this->em->remove($this->em->getRepository('ApplicationSonataUserBundle:User')
53+
->find($this->user->getId()));
54+
$this->em->remove($this->em->getRepository('App:Todo')
55+
->find($this->todo->getId()));
56+
$this->em->flush();
57+
$this->em = null;
58+
}
59+
60+
/*
61+
* Verify that a not authenticated user cannot see the API
62+
*/
63+
public function testRedirectNotAuthenticated()
64+
{
65+
$client = static::createClient();
66+
67+
$client->request('GET', '/api/doc');
68+
69+
$this->assertEquals(302, $client->getResponse()->getStatusCode());
70+
}
71+
72+
/*
73+
* Verify that home is reachable
74+
*/
75+
public function testHome()
76+
{
77+
$client = static::createClient();
78+
79+
$client->request('GET', '/');
80+
$this->assertEquals(302, $client->getResponse()->getStatusCode());
81+
}
82+
83+
/*
84+
* Verify that the login page contains the login action
85+
*/
86+
public function testLogin()
87+
{
88+
$client = static::createClient();
89+
90+
$crawler = $client->request('GET', '/admin/login');
91+
$this->assertGreaterThan(
92+
0,
93+
$crawler->filter('html:contains("Login")')->count()
94+
);
95+
}
96+
/*
97+
* Verify that, once logged, admin can see his dashboard
98+
*/
99+
public function testAdminAuthenticatedIndexAction()
100+
{
101+
$client = $this->getLoggedClient('admin', 'ROLE_SUPER_ADMIN', 'user');
102+
$crawler = $client->request('GET', '/admin/dashboard');
103+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
104+
105+
}
106+
/*
107+
* Verify that a todo has an ID (it's saved in database)
108+
*/
109+
public function testTodoHasId()
110+
{
111+
$this->assertGreaterThan(0,$this->todo->getId());
112+
}
113+
114+
}

tests/Utils/LoggedUserTrait.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace App\Tests\Utils;
3+
4+
use Symfony\Component\BrowserKit\Cookie;
5+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
6+
7+
trait LoggedUserTrait {
8+
9+
private function getLoggedClient($user, $role = 'ROLE_USER', $firewall = 'main')
10+
{
11+
$client = static::createClient();
12+
$session = $client->getContainer()->get('session');
13+
14+
$token = new UsernamePasswordToken($user, null, $firewall, array($role));
15+
$session->set('_security_'.$firewall, serialize($token));
16+
$session->save();
17+
18+
$cookie = new Cookie($session->getName(), $session->getId());
19+
$client->getCookieJar()->set($cookie);
20+
21+
return $client;
22+
}
23+
}

0 commit comments

Comments
 (0)