1
1
<?php
2
-
2
+
3
3
/**
4
4
* This file is part of the PHPFUI/HTMLUnitTester package
5
5
*
8
8
* For the full copyright and license information, please view
9
9
* the LICENSE.md file that was distributed with this source
10
10
* code
11
- */
12
-
13
- namespace PHPFUI \HTMLUnitTester ;
14
-
11
+ */
12
+
13
+ namespace PHPFUI \HTMLUnitTester ;
14
+
15
15
class Extensions extends \PHPUnit \Framework \TestCase implements \PHPUnit \Runner \Hook
16
- {
17
-
16
+ {
17
+
18
18
private static $ throttle ;
19
- private static $ validator ;
20
-
19
+ private static $ validator ;
20
+
21
21
public static function setUpBeforeClass () : void
22
22
{
23
23
$ url = $ _ENV [__CLASS__ . '_url ' ] ?? 'http://127.0.0.1:8888 ' ;
24
24
$ throttleMicroSeconds = $ _ENV [__CLASS__ . '_delay ' ] ?? 0 ;
25
25
if (! filter_var ($ url , FILTER_VALIDATE_URL ))
26
26
{
27
27
throw new \PHPUnit \Framework \Exception ($ url . ' is not a valid URL ' );
28
- }
29
-
28
+ }
29
+
30
30
self ::$ throttle = new Throttle ($ throttleMicroSeconds );
31
31
self ::$ validator = new \HtmlValidator \Validator ($ url );
32
- }
33
-
32
+ }
33
+
34
34
public function assertNotWarningCss (string $ css , string $ message = '' ) : void
35
35
{
36
36
$ response = $ this ->validateCss ($ css );
37
37
self ::assertThat ($ response , new WarningConstraint (), $ message );
38
- }
39
-
38
+ }
39
+
40
40
public function assertNotWarningCssFile (string $ file , string $ message = '' ) : void
41
41
{
42
42
$ response = $ this ->validateCss ($ this ->getFromFile ($ file ));
43
43
self ::assertThat ($ response , new WarningConstraint (), $ message );
44
- }
45
-
44
+ }
45
+
46
46
public function assertNotWarningCssUrl (string $ url , string $ message = '' ) : void
47
47
{
48
48
$ response = $ this ->validateCss ($ this ->getFromUrl ($ url ));
49
49
self ::assertThat ($ response , new WarningConstraint (), $ message );
50
- }
51
-
50
+ }
51
+
52
52
public function assertNotWarningFile (string $ file , string $ message = '' ) : void
53
53
{
54
54
$ response = $ this ->validateHtml ($ this ->getFromFile ($ file ));
55
55
self ::assertThat ($ response , new WarningConstraint (), $ message );
56
- }
57
-
56
+ }
57
+
58
58
public function assertNotWarningHtml (string $ html , string $ message = '' ) : void
59
59
{
60
60
$ response = $ this ->validateHtml ($ html );
61
61
self ::assertThat ($ response , new WarningConstraint (), $ message );
62
- }
63
-
62
+ }
63
+
64
64
public function assertNotWarningUrl (string $ url , string $ message = '' ) : void
65
65
{
66
66
$ response = $ this ->validateHtml ($ this ->getFromUrl ($ url ));
67
67
self ::assertThat ($ response , new ErrorConstraint (), $ message );
68
- }
69
-
68
+ }
69
+
70
70
public function assertValidCss (string $ css , string $ message = '' ) : void
71
71
{
72
72
$ response = $ this ->validateCss ($ css );
73
73
self ::assertThat ($ response , new ErrorConstraint (), $ message );
74
- }
75
-
74
+ }
75
+
76
76
public function assertValidCssFile (string $ file , string $ message = '' ) : void
77
77
{
78
78
$ response = $ this ->validateCss ($ this ->getFromFile ($ file ));
79
79
self ::assertThat ($ response , new ErrorConstraint (), $ message );
80
- }
81
-
80
+ }
81
+
82
82
public function assertValidCssUrl (string $ url , string $ message = '' ) : void
83
83
{
84
84
$ response = $ this ->validateCss ($ this ->getFromUrl ($ url ));
85
85
self ::assertThat ($ response , new ErrorConstraint (), $ message );
86
- }
87
-
86
+ }
87
+
88
88
public function assertValidFile (string $ file , string $ message = '' ) : void
89
89
{
90
90
$ response = $ this ->validateHtml ($ this ->getFromFile ($ file ));
91
91
self ::assertThat ($ response , new ErrorConstraint (), $ message );
92
- }
93
-
92
+ }
93
+
94
94
public function assertValidHtml (string $ html , string $ message = '' ) : void
95
95
{
96
96
$ response = $ this ->validateHtml ($ html );
97
97
self ::assertThat ($ response , new ErrorConstraint (), $ message );
98
- }
99
-
98
+ }
99
+
100
100
public function assertValidUrl (string $ url , string $ message = '' ) : void
101
101
{
102
102
$ response = $ this ->validateHtml ($ this ->getFromUrl ($ url ));
103
103
self ::assertThat ($ response , new ErrorConstraint (), $ message );
104
- }
105
-
104
+ }
105
+
106
106
private function getFromFile (string $ file ) : string
107
107
{
108
108
if (! file_exists ($ file ))
109
109
{
110
110
throw new \PHPUnit \Framework \Exception ("File {$ file } was not found. \n" );
111
- }
112
-
111
+ }
112
+
113
113
if (! is_readable ($ file ))
114
114
{
115
115
throw new \PHPUnit \Framework \Exception ("File {$ file } is not readable. \n" );
116
116
}
117
- $ html = file_get_contents ($ file );
118
-
117
+ $ html = file_get_contents ($ file );
118
+
119
119
return $ html ;
120
- }
121
-
120
+ }
121
+
122
122
private function getFromUrl (string $ url ) : string
123
123
{
124
124
// Check that $url is a valid url
125
125
if (false === filter_var ($ url , FILTER_VALIDATE_URL ))
126
126
{
127
127
throw new \PHPUnit \Framework \Exception ("Url {$ url } is not valid. \n" );
128
- }
129
-
128
+ }
129
+
130
130
$ context = stream_context_create (['http ' => ['user_agent ' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0 ' ]]);
131
- $ html = file_get_contents ($ url , false , $ context );
132
-
131
+ $ html = file_get_contents ($ url , false , $ context );
132
+
133
133
// Check that something was returned
134
134
if (! strlen ($ html ))
135
135
{
136
136
throw new \PHPUnit \Framework \Exception ("{$ url } is empty. \n" );
137
- }
138
-
137
+ }
138
+
139
139
return $ html ;
140
- }
141
-
140
+ }
141
+
142
142
private function validateCss (string $ css ) : \HtmlValidator \Response
143
143
{
144
144
if (false === stripos ($ css , 'html> ' ))
145
145
{
146
146
$ css = '<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Title</title><style> ' . $ css . '</style></head><body><hr></body></html> ' ;
147
147
}
148
148
self ::$ throttle ->delay ();
149
- $ response = self ::$ validator ->validateDocument ($ css );
150
-
149
+ $ response = self ::$ validator ->validateDocument ($ css );
150
+
151
151
return $ response ;
152
- }
153
-
152
+ }
153
+
154
154
private function validateHtml (string $ html ) : \HtmlValidator \Response
155
155
{
156
156
if (false === stripos ($ html , 'html> ' ))
157
157
{
158
158
$ html = '<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Title</title></head><body> ' . $ html . '</body></html> ' ;
159
159
}
160
160
self ::$ throttle ->delay ();
161
- $ response = self ::$ validator ->validateDocument ($ html );
162
-
161
+ $ response = self ::$ validator ->validateDocument ($ html );
162
+
163
163
return $ response ;
164
- }
165
-
164
+ }
165
+
166
166
}
0 commit comments