11<?php
2-
2+
33/**
44 * This file is part of the PHPFUI/HTMLUnitTester package
55 *
88 * For the full copyright and license information, please view
99 * the LICENSE.md file that was distributed with this source
1010 * code
11- */
12-
13- namespace PHPFUI \HTMLUnitTester ;
14-
11+ */
12+
13+ namespace PHPFUI \HTMLUnitTester ;
14+
1515class Extensions extends \PHPUnit \Framework \TestCase implements \PHPUnit \Runner \Hook
16- {
17-
16+ {
17+
1818 private static $ throttle ;
19- private static $ validator ;
20-
19+ private static $ validator ;
20+
2121 public static function setUpBeforeClass () : void
2222 {
2323 $ url = $ _ENV [__CLASS__ . '_url ' ] ?? 'http://127.0.0.1:8888 ' ;
2424 $ throttleMicroSeconds = $ _ENV [__CLASS__ . '_delay ' ] ?? 0 ;
2525 if (! filter_var ($ url , FILTER_VALIDATE_URL ))
2626 {
2727 throw new \PHPUnit \Framework \Exception ($ url . ' is not a valid URL ' );
28- }
29-
28+ }
29+
3030 self ::$ throttle = new Throttle ($ throttleMicroSeconds );
3131 self ::$ validator = new \HtmlValidator \Validator ($ url );
32- }
33-
32+ }
33+
3434 public function assertNotWarningCss (string $ css , string $ message = '' ) : void
3535 {
3636 $ response = $ this ->validateCss ($ css );
3737 self ::assertThat ($ response , new WarningConstraint (), $ message );
38- }
39-
38+ }
39+
4040 public function assertNotWarningCssFile (string $ file , string $ message = '' ) : void
4141 {
4242 $ response = $ this ->validateCss ($ this ->getFromFile ($ file ));
4343 self ::assertThat ($ response , new WarningConstraint (), $ message );
44- }
45-
44+ }
45+
4646 public function assertNotWarningCssUrl (string $ url , string $ message = '' ) : void
4747 {
4848 $ response = $ this ->validateCss ($ this ->getFromUrl ($ url ));
4949 self ::assertThat ($ response , new WarningConstraint (), $ message );
50- }
51-
50+ }
51+
5252 public function assertNotWarningFile (string $ file , string $ message = '' ) : void
5353 {
5454 $ response = $ this ->validateHtml ($ this ->getFromFile ($ file ));
5555 self ::assertThat ($ response , new WarningConstraint (), $ message );
56- }
57-
56+ }
57+
5858 public function assertNotWarningHtml (string $ html , string $ message = '' ) : void
5959 {
6060 $ response = $ this ->validateHtml ($ html );
6161 self ::assertThat ($ response , new WarningConstraint (), $ message );
62- }
63-
62+ }
63+
6464 public function assertNotWarningUrl (string $ url , string $ message = '' ) : void
6565 {
6666 $ response = $ this ->validateHtml ($ this ->getFromUrl ($ url ));
6767 self ::assertThat ($ response , new ErrorConstraint (), $ message );
68- }
69-
68+ }
69+
7070 public function assertValidCss (string $ css , string $ message = '' ) : void
7171 {
7272 $ response = $ this ->validateCss ($ css );
7373 self ::assertThat ($ response , new ErrorConstraint (), $ message );
74- }
75-
74+ }
75+
7676 public function assertValidCssFile (string $ file , string $ message = '' ) : void
7777 {
7878 $ response = $ this ->validateCss ($ this ->getFromFile ($ file ));
7979 self ::assertThat ($ response , new ErrorConstraint (), $ message );
80- }
81-
80+ }
81+
8282 public function assertValidCssUrl (string $ url , string $ message = '' ) : void
8383 {
8484 $ response = $ this ->validateCss ($ this ->getFromUrl ($ url ));
8585 self ::assertThat ($ response , new ErrorConstraint (), $ message );
86- }
87-
86+ }
87+
8888 public function assertValidFile (string $ file , string $ message = '' ) : void
8989 {
9090 $ response = $ this ->validateHtml ($ this ->getFromFile ($ file ));
9191 self ::assertThat ($ response , new ErrorConstraint (), $ message );
92- }
93-
92+ }
93+
9494 public function assertValidHtml (string $ html , string $ message = '' ) : void
9595 {
9696 $ response = $ this ->validateHtml ($ html );
9797 self ::assertThat ($ response , new ErrorConstraint (), $ message );
98- }
99-
98+ }
99+
100100 public function assertValidUrl (string $ url , string $ message = '' ) : void
101101 {
102102 $ response = $ this ->validateHtml ($ this ->getFromUrl ($ url ));
103103 self ::assertThat ($ response , new ErrorConstraint (), $ message );
104- }
105-
104+ }
105+
106106 private function getFromFile (string $ file ) : string
107107 {
108108 if (! file_exists ($ file ))
109109 {
110110 throw new \PHPUnit \Framework \Exception ("File {$ file } was not found. \n" );
111- }
112-
111+ }
112+
113113 if (! is_readable ($ file ))
114114 {
115115 throw new \PHPUnit \Framework \Exception ("File {$ file } is not readable. \n" );
116116 }
117- $ html = file_get_contents ($ file );
118-
117+ $ html = file_get_contents ($ file );
118+
119119 return $ html ;
120- }
121-
120+ }
121+
122122 private function getFromUrl (string $ url ) : string
123123 {
124124 // Check that $url is a valid url
125125 if (false === filter_var ($ url , FILTER_VALIDATE_URL ))
126126 {
127127 throw new \PHPUnit \Framework \Exception ("Url {$ url } is not valid. \n" );
128- }
129-
128+ }
129+
130130 $ 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+
133133 // Check that something was returned
134134 if (! strlen ($ html ))
135135 {
136136 throw new \PHPUnit \Framework \Exception ("{$ url } is empty. \n" );
137- }
138-
137+ }
138+
139139 return $ html ;
140- }
141-
140+ }
141+
142142 private function validateCss (string $ css ) : \HtmlValidator \Response
143143 {
144144 if (false === stripos ($ css , 'html> ' ))
145145 {
146146 $ css = '<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Title</title><style> ' . $ css . '</style></head><body><hr></body></html> ' ;
147147 }
148148 self ::$ throttle ->delay ();
149- $ response = self ::$ validator ->validateDocument ($ css );
150-
149+ $ response = self ::$ validator ->validateDocument ($ css );
150+
151151 return $ response ;
152- }
153-
152+ }
153+
154154 private function validateHtml (string $ html ) : \HtmlValidator \Response
155155 {
156156 if (false === stripos ($ html , 'html> ' ))
157157 {
158158 $ html = '<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Title</title></head><body> ' . $ html . '</body></html> ' ;
159159 }
160160 self ::$ throttle ->delay ();
161- $ response = self ::$ validator ->validateDocument ($ html );
162-
161+ $ response = self ::$ validator ->validateDocument ($ html );
162+
163163 return $ response ;
164- }
165-
164+ }
165+
166166 }
0 commit comments