1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Functional\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
7
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
8
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
10
|
|
|
|
11
|
|
|
abstract class AbstractController extends WebTestCase |
12
|
|
|
{ |
13
|
|
|
protected static $options = [ |
14
|
|
|
'environment' => 'test', |
15
|
|
|
'debug' => true, |
16
|
|
|
]; |
17
|
|
|
protected $em; |
18
|
|
|
protected $sessionClient; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
|
|
protected function setUp(): void |
24
|
|
|
{ |
25
|
|
|
self::bootKernel(self::$options); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return EntityManager |
30
|
|
|
*/ |
31
|
|
|
public function getEm() |
32
|
|
|
{ |
33
|
|
|
if (!$this->em) { |
34
|
|
|
$this->em = $this->getContainer()->get('doctrine')->getManager(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this->em; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function dump($content, $destination = '/var/www/test.html') |
41
|
|
|
{ |
42
|
|
|
$filesystem = new \Symfony\Component\Filesystem\Filesystem(); |
43
|
|
|
$filesystem->dumpFile($destination, $content); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $path |
48
|
|
|
* @param string $method |
49
|
|
|
* @param int $expectedStatusCode |
50
|
|
|
* |
51
|
|
|
* @return \Symfony\Component\DomCrawler\Crawler |
52
|
|
|
*/ |
53
|
|
|
protected function request($path, $method = 'GET', $expectedStatusCode = 200, array $headers = []) |
54
|
|
|
{ |
55
|
|
|
$client = $this->getSessionClient(); |
56
|
|
|
|
57
|
|
|
$crawler = $client->request($method, $path, [], [], $headers); |
58
|
|
|
file_put_contents('/tmp/test.html', $client->getResponse()->getContent()); |
59
|
|
|
$this->assertEquals( |
60
|
|
|
$expectedStatusCode, |
61
|
|
|
$client->getResponse()->getStatusCode(), |
62
|
|
|
sprintf('We expected that uri "%s" will return %s status code, but had received %d', $path, $expectedStatusCode, $client->getResponse()->getStatusCode()) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return $crawler; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function restRequest(string $path, string $method = 'GET', int $expectedStatusCode = 200) |
69
|
|
|
{ |
70
|
|
|
return $this->request($path, $method, $expectedStatusCode, ['HTTP_accept' => 'application/json']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Selects form by a button by name or alt value for images. |
75
|
|
|
* |
76
|
|
|
* @param Crawler $pageObject |
77
|
|
|
* @return \Symfony\Component\DomCrawler\Form |
78
|
|
|
*/ |
79
|
|
|
protected function getConfirmDeleteFormObject(Crawler $pageObject) |
80
|
|
|
{ |
81
|
|
|
return $pageObject->filter('body > div > div > section.content > div > div > div > div.box-footer.clearfix > form > button')->form(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function getSessionClient(array $options = array(), array $server = array()) |
85
|
|
|
{ |
86
|
|
|
if (!$this->sessionClient) { |
87
|
|
|
$this->sessionClient = static::createClient($options, $server); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->sessionClient; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return \Symfony\Component\DependencyInjection\ContainerInterface |
95
|
|
|
*/ |
96
|
|
|
public function getContainer() |
97
|
|
|
{ |
98
|
|
|
return self::$container; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function getHttpHost() |
102
|
|
|
{ |
103
|
|
|
return $this->getContainer()->hasParameter('local_domain') |
104
|
|
|
? $this->getContainer()->getParameter('local_domain') |
105
|
|
|
: 'localhost' |
106
|
|
|
; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function logIn($username = 'admin', $password = 'admin') |
110
|
|
|
{ |
111
|
|
|
$client = $this->getSessionClient(); |
112
|
|
|
$crawler = $client->request('GET', '/login'); |
113
|
|
|
$form = $crawler->filter('.form-signin')->form(); |
114
|
|
|
$client->submit($form, ['form_login[username]' => $username, 'form_login[password]' => $password]); |
115
|
|
|
|
116
|
|
|
return $client; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|