Skip to content

Commit a8b6fa1

Browse files
author
Bruce Wells
committed
Basics
1 parent fd1bdd7 commit a8b6fa1

File tree

7 files changed

+386
-0
lines changed

7 files changed

+386
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vendor/
2+
.idea/
3+
bin/
4+
composer.lock
5+
.php_cs.cache
6+
.phpunit.result.cache

.php_cs.dist

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__.'\src')
5+
;
6+
7+
return PhpCsFixer\Config::create()
8+
->setIndent("\t")
9+
->setLineEnding("\r\n")
10+
->setRules([
11+
'array_syntax' => [
12+
'syntax' => 'short'
13+
],
14+
'blank_line_after_namespace' => true,
15+
'blank_line_before_return' => true,
16+
// 'braces' => [
17+
// 'position_after_control_structures' => 'next',
18+
// 'position_after_functions_and_oop_constructs' => 'next',
19+
// 'position_after_anonymous_constructs' => 'next',
20+
// ],
21+
'combine_consecutive_unsets' => true,
22+
'elseif' => true,
23+
'no_trailing_whitespace' => true,
24+
'no_unneeded_control_parentheses' => true,
25+
'no_useless_return' => true,
26+
'no_whitespace_in_blank_line' => true,
27+
'not_operator_with_successor_space' => true,
28+
'ordered_class_elements' => [
29+
'sortAlgorithm' => 'alpha',
30+
],
31+
'return_type_declaration' => [
32+
'space_before' => 'one',
33+
],
34+
'single_quote' => true,
35+
'standardize_not_equals' => true,
36+
])
37+
->setFinder($finder)
38+
;
39+

CODE_OF_CONDUCT.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project email address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [email protected]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "phpfui/HTMLUnitTester",
3+
"description": "PHPUnit Testing extensions for HMTL and CSS",
4+
"minimum-stability": "stable",
5+
"keywords": ["PHPUnit","html","css","unit","test","php"],
6+
"homepage": "https://github.com/phpfui/HTMLUnitTester",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Bruce Wells",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": ">=7.1",
16+
"rexxars/html-validator": "^2.2"
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "~8.0"
20+
},
21+
"autoload": {
22+
"psr-0": {"PHPFUI": "src/"}
23+
},
24+
"config": {
25+
"bin-dir": "bin"
26+
}
27+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHPFUI/HTMLUnitTester package
5+
*
6+
* (c) Bruce Wells
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source
10+
* code
11+
*/
12+
13+
namespace PHPFUI\HTMLUnitTester;
14+
15+
class Extensions extends \PHPUnit\Framework\TestCase
16+
{
17+
18+
private $throttle;
19+
private $validator;
20+
21+
public function __construct(string $url, int $throttleMicroSeconds = 0)
22+
{
23+
if (! filter_var($url, FILTER_VALIDATE_URL))
24+
{
25+
throw new \PHPUnit\Framework\Exception($url . ' is not a valid URL');
26+
}
27+
28+
$throttle = new Throttle($throttleMicroSeconds);
29+
$this->validator = new \HtmlValidator\Validator($url);
30+
}
31+
32+
public function assertNotValidCss(string $css, string $message = '') : void
33+
{
34+
$response = $this->validateCss($css);
35+
}
36+
37+
public function assertNotValidCssFile(string $file, string $message = '') : void
38+
{
39+
$response = $this->validateCss($this->getFromFile($file));
40+
}
41+
42+
public function assertNotValidCssUrl(string $url, string $message = '') : void
43+
{
44+
$response = $this->validateCss($this->getFromUrl($url));
45+
}
46+
47+
public function assertNotValidFile(string $file, string $message = '') : void
48+
{
49+
$response = $this->validateHtml($this->getFromFile($file));
50+
}
51+
52+
public function assertNotValidHtml(string $html, string $message = '') : void
53+
{
54+
$response = $this->validateHtml($html);
55+
}
56+
57+
public function assertNotValidUrl(string $url, string $message = '') : void
58+
{
59+
$response = $this->validateHtml($this->getFromUrl($url));
60+
}
61+
62+
public function assertValidCss(string $css, string $message = '') : void
63+
{
64+
$response = $this->validateCss($css);
65+
}
66+
67+
public function assertValidCssFile(string $file, string $message = '') : void
68+
{
69+
$response = $this->validateCss($this->getFromFile($file));
70+
}
71+
72+
public function assertValidCssUrl(string $url, string $message = '') : void
73+
{
74+
$response = $this->validateCss($this->getFromUrl($url));
75+
}
76+
77+
public function assertValidFile(string $file, string $message = '') : void
78+
{
79+
$response = $this->validateHtml($this->getFromFile($file));
80+
}
81+
82+
public function assertValidHtml(string $html, string $message = '') : void
83+
{
84+
$response = $this->validateHtml($html);
85+
86+
self::assertThat($response, new HtmlConstraint(), $message);
87+
}
88+
89+
public function assertValidUrl(string $url, string $message = '') : void
90+
{
91+
$response = $this->validateHtml($this->getFromUrl($url));
92+
}
93+
94+
private function getFromFile(string $file) : string
95+
{
96+
if (! file_exists($file))
97+
{
98+
throw new \PHPUnit\Framework\Exception("File {$file} was not found.\n");
99+
}
100+
101+
if (! is_readable($file))
102+
{
103+
throw new \PHPUnit\Framework\Exception("File {$file} is not readable.\n");
104+
}
105+
$html = file_get_contents($file);
106+
107+
return $html;
108+
}
109+
110+
private function getFromUrl(string $url) : string
111+
{
112+
// Check that $url is a valid url.
113+
if (false === filter_var($url, FILTER_VALIDATE_URL))
114+
{
115+
throw new \PHPUnit\Framework\Exception("Url {$url} is not valid.\n");
116+
}
117+
$html = file_get_contents($url);
118+
119+
return $html;
120+
}
121+
122+
private function validateCss(string $css) : \HtmlValidator\Response
123+
{
124+
if (false === stripos($css, 'html>'))
125+
{
126+
$css = '<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Title</title><style>' . $css . '</style></head><body><hr></body></html>';
127+
}
128+
$this->throttle->delay();
129+
$response = self::$validator->validateDocument($css);
130+
131+
return $response;
132+
}
133+
134+
private function validateHtml(string $html) : \HtmlValidator\Response
135+
{
136+
if (false === stripos($html, 'html>'))
137+
{
138+
$html = '<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Title</title></head><body>' . $html . '</body></html>';
139+
}
140+
$this->throttle->delay();
141+
$response = self::$validator->validateDocument($html);
142+
143+
return $response;
144+
}
145+
146+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the HTMLUnitTester package.
5+
*
6+
* (c) Bruce Wells <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PHPFUI\HTMLUnitTester;
13+
14+
class HtmlConstraint extends \PHPUnit\Framework\Constraint\Constraint
15+
{
16+
/**
17+
* Returns a string representation of the constraint.
18+
*
19+
*/
20+
public function toString() : string
21+
{
22+
return 'is valid';
23+
}
24+
25+
/**
26+
* Return additional failure description where needed.
27+
*
28+
* The function can be overridden to provide additional failure
29+
* information like a diff
30+
*
31+
* @param mixed $other Evaluated value or object.
32+
*
33+
*/
34+
protected function additionalFailureDescription($other) : string
35+
{
36+
return implode("\n", $other->getErrors());
37+
}
38+
39+
/**
40+
* Returns the description of the failure.
41+
*
42+
* The beginning of failure messages is "Failed asserting that" in most
43+
* cases. This method should return the second part of that sentence.
44+
*
45+
* @param mixed $other Evaluated value or object.
46+
*
47+
*/
48+
protected function failureDescription($other) : string
49+
{
50+
return 'the markup ' . $this->toString();
51+
}
52+
53+
/**
54+
* Evaluates the constraint for parameter $other. Returns TRUE if the
55+
* constraint is met, FALSE otherwise.
56+
*
57+
* @param mixed $other Value or object to evaluate.
58+
*
59+
*/
60+
protected function matches($other) : bool
61+
{
62+
return $other->hasErrors();
63+
}
64+
65+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the HTMLUnitTester package.
5+
*
6+
* (c) Bruce Wells <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PHPFUI\HTMLUnitTester;
13+
14+
/**
15+
* Simple Throttle class. Call $throttle->delay() to wait the
16+
* minimum number of microseconds specified in the constructor.
17+
*
18+
* Default constructor does not result in any delay.
19+
*/
20+
class Throttle
21+
{
22+
23+
private $lastAccessed = 0;
24+
private $microseconds = 0;
25+
26+
/**
27+
* There are 1 million microsecond in a second.
28+
*/
29+
public function __construct(int $microseconds = 0)
30+
{
31+
$this->lastAccessed = microtime(true);
32+
if ($microseconds)
33+
{
34+
$this->microseconds = 1 / 1000000 * $microseconds;
35+
}
36+
}
37+
38+
/**
39+
* Wait at least the number of microseconds since the last
40+
* delay call.
41+
*/
42+
public function delay() : void
43+
{
44+
if ($this->microseconds)
45+
{
46+
$now = microtime(true);
47+
$timeDifference = $now - $this->lastAccessed;
48+
49+
if ($timeDifference < $this->microseconds)
50+
{
51+
usleep(($this->microseconds - $timeDifference) * 1000000);
52+
}
53+
$this->lastAccessed = $now = microtime(true);
54+
}
55+
}
56+
57+
}

0 commit comments

Comments
 (0)