Completed
Push — master ( 78e73b...e8e5da )
by Michael
16:28
created

BootstrapTestSuite::profileCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 13
nc 1
nop 1
1
<?php
2
3
namespace Tests\AppBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
7
abstract class BootstrapTestSuite extends WebTestCase
8
{
9
	public $client;
10
	public $crawler;
11
	public $response;
12
13
	protected function setupTest($path)
14
	{
15
16
		$client = static::createClient();
17
18
		$this->setClient($client);
19
		$client->enableProfiler();
20
		$crawler = $client->request('GET', $path);
21
		$response = $client->getResponse();
22
		$this->setupBootstrapping($client, $crawler, $response);
23
24
		return array('client' => $client, 'crawler' => $crawler, 'response' => $response);
25
	}
26
27
	public function setupBootstrapping($client, $crawler, $response)
28
	{
29
		$this->setClient($client);
30
		$this->setCrawler($crawler);
31
		$this->setResponse($response);
32
	}
33
34
	public function globalTests($status = 200)
35
	{
36
		if (strpos(strval($status), '2') === 0) {
37
			$this->assertTrue($this->client->getResponse()->isSuccessful(), 'Response is a sucessful one');
38
		}
39
		else
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
40
		{
41
			$this->assertEquals($this->response->getStatusCode(), $status, 'Response Code Check');
42
		}
43
44
		if ($status == 200) {
45
			$this->assertTrue($this->crawler->filter('html:contains("PHP FIG")')->count() > 0, 'Header Check');
46
		}
47
48
		if ($profile = $this->client->getProfile()) {
49
			$this->profileCheck($profile);
50
		}
51
	}
52
53
	private function profileCheck($profile)
54
	{
55
		$this->assertLessThan(
56
			50,
57
			$profile->getCollector('db')->getQueryCount(),
58
			('Checks that query count is less than 50 (token ' .  $profile->getToken() .')')
59
			);
60
61
		$this->assertLessThan(
62
                10000, // ms
63
                $profile->getCollector('time')->getDuration(),
64
			('Checks that time count is '. $profile->getCollector('time')->getDuration() .' which is not less than 10000ms (token ' .  $profile->getToken() .')')
65
            );
66
67
		$this->assertLessThan(
68
                70000000, // 70MB
69
                $profile->getCollector('memory')->getMemory(),
70
			('Checks that memory check is '. $profile->getCollector('memory')->getMemory() .' bytes which is not less than 70MB (token ' .  $profile->getToken() .')')
71
            );
72
	}
73
74
	public function get($dependency)
75
	{
76
		$container = $this->client->getContainer();
77
78
		return $container->get($dependency);
79
	}
80
81
	/**
82
	 * Gets the value of client.
83
	 *
84
	 * @return mixed
85
	 */
86
	public function getClient()
87
	{
88
		return $this->client;
89
	}
90
91
	/**
92
	 * Sets the value of client.
93
	 *
94
	 * @param mixed $client the client
95
	 *
96
	 * @return self
97
	 */
98
	public function setClient($client)
99
	{
100
		$this->client = $client;
101
102
		return $this;
103
	}
104
105
	/**
106
	 * Gets the value of crawler.
107
	 *
108
	 * @return mixed
109
	 */
110
	public function getCrawler()
111
	{
112
		return $this->crawler;
113
	}
114
115
	/**
116
	 * Sets the value of crawler.
117
	 *
118
	 * @param mixed $crawler the crawler
119
	 *
120
	 * @return self
121
	 */
122
	public function setCrawler($crawler)
123
	{
124
		$this->crawler = $crawler;
125
126
		return $this;
127
	}
128
129
	/**
130
	 * Gets the value of response.
131
	 *
132
	 * @return mixed
133
	 */
134
	public function getResponse()
135
	{
136
		return $this->response;
137
	}
138
139
	/**
140
	 * Sets the value of response.
141
	 *
142
	 * @param mixed $response the response
143
	 *
144
	 * @return self
145
	 */
146
	public function setResponse($response)
147
	{
148
		$this->response = $response;
149
150
		return $this;
151
	}
152
}
153