Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Add unit tests for the selectors. #24

Merged
merged 1 commit into from
Jan 24, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 122 additions & 2 deletions Tests/Unit/EmogrifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ public function emogrifyForHtmlTagWithXhtml5DocumentTypeKeepsDocumentType() {
public function emogrifyCanAddMatchingElementRuleOnHtmlElementFromCss() {
$html = self::HTML5_DOCUMENT_TYPE . self::LF . '<html></html>' . self::LF;
$this->subject->setHtml($html);
$this->subject->setCss('html {color: #000;}');
$styleRule = 'color: #000;';
$this->subject->setCss('html {' . $styleRule . '}');

$this->assertContains(
'<html style="color: #000;">',
'<html style="' . $styleRule . '">',
$this->subject->emogrify()
);
}
Expand All @@ -147,4 +148,123 @@ public function emogrifyNotAddsNotMatchingElementRuleOnHtmlElementFromCss() {
$this->subject->emogrify()
);
}

/**
* @test
*/
public function emogrifyCanMatchTwoElements() {
$html = self::HTML5_DOCUMENT_TYPE . self::LF . '<html><p></p><p></p></html>' . self::LF;
$this->subject->setHtml($html);
$styleRule = 'color: #000;';
$this->subject->setCss('p {' . $styleRule . '}');

$this->assertSame(
2,
substr_count($this->subject->emogrify(), '<p style="' . $styleRule . '">')
);
}

/**
* @test
*/
public function emogrifyCanAssignTwoStyleRulesFromSameMatcherToElement() {
$html = self::HTML5_DOCUMENT_TYPE . self::LF . '<html><p></p></html>' . self::LF;
$this->subject->setHtml($html);
$styleRules = 'color: #000; text-align: left;';
$this->subject->setCss('p {' . $styleRules . '}');

$this->assertContains(
'<p style="' . $styleRules . '">',
$this->subject->emogrify()
);
}

/**
* @test
*/
public function emogrifyCanAssignStyleRulesFromTwoIdenticalMatchersToElement() {
$html = self::HTML5_DOCUMENT_TYPE . self::LF . '<html><p></p></html>' . self::LF;
$this->subject->setHtml($html);
$styleRule1 = 'color:#000;';
$styleRule2 = 'text-align:left;';
$this->subject->setCss('p {' . $styleRule1 . '} p {' . $styleRule2 . '}');

$this->assertContains(
'<p style="' . $styleRule1 . $styleRule2 . '">',
$this->subject->emogrify()
);
}

/**
* @test
*/
public function emogrifyCanAssignStyleRulesFromTwoDifferentMatchersToElement() {
$html = self::HTML5_DOCUMENT_TYPE . self::LF . '<html><p class="x"></p></html>' . self::LF;
$this->subject->setHtml($html);
$styleRule1 = 'color:#000;';
$styleRule2 = 'text-align:left;';
$this->subject->setCss('p {' . $styleRule1 . '} .x {' . $styleRule2 . '}');

$this->assertContains(
'<p class="x" style="' . $styleRule1 . $styleRule2 . '">',
$this->subject->emogrify()
);
}

/**
* Data provide for selectors.
*
* @return array
*
* @see emogrifierMatchesSelectors
*/
public function selectorDataProvider() {
$styleRule = 'color: red';
$styleAttribute = 'style="' . $styleRule . '"';

return array(
'universal selector HTML' => array('* {' . $styleRule . '} ', '#<html id="html" ' . $styleAttribute . '>#'),
'universal selector BODY' => array('* {' . $styleRule . '} ', '#<body ' . $styleAttribute . '>#'),
'universal selector P' => array('* {' . $styleRule . '} ', '#<p[^>]*' . $styleAttribute . '>#'),
'type selector matches first P' => array('p {' . $styleRule . '} ', '#<p class="p-1" ' . $styleAttribute . '>#'),
'type selector matches second P' => array('p {' . $styleRule . '} ', '#<p class="p-2" ' . $styleAttribute . '>#'),
'descendant selector P SPAN' => array('p span {' . $styleRule . '} ', '#<span ' . $styleAttribute . '>#'),
'descendant selector BODY SPAN' => array('body span {' . $styleRule . '} ', '#<span ' . $styleAttribute . '>#'),
'child selector P > SPAN matches direct child'
=> array('p > span {' . $styleRule . '} ', '#<span ' . $styleAttribute . '>#'),
'child selector BODY > SPAN not matches grandchild' => array('body > span {' . $styleRule . '} ', '#<span>#'),
'BODY:first-child not matches second child' => array('body:first-child {' . $styleRule . '} ', '#<p class="p-2">#'),
'ID selector #HTML' => array('#html {' . $styleRule . '} ', '#<html id="html" ' . $styleAttribute . '>#'),
'type and ID selector HTML#HTML'
=> array('html#html {' . $styleRule . '} ', '#<html id="html" ' . $styleAttribute . '>#'),
'class selector .P-1' => array('.p-1 {' . $styleRule . '} ', '#<p class="p-1" ' . $styleAttribute . '>#'),
'type and class selector P.P-1' => array('p.p-1 {' . $styleRule . '} ', '#<p class="p-1" ' . $styleAttribute . '>#'),
);
}

/**
* @test
*
* @param string $css the complete CSS
* @param string $containedHtml regular expression for the the HTML that needs to be contained in the merged HTML
*
* @dataProvider selectorDataProvider
*/
public function emogrifierMatchesSelectors($css, $containedHtml) {
$html = self::HTML5_DOCUMENT_TYPE . self::LF .
'<html id="html">' .
' <body>' .
' <p class="p-1"><span>some text</span></p>' .
' <p class="p-2">some text</p>' .
' </body>' .
'</html>';

$this->subject->setHtml($html);
$this->subject->setCss($css);

$this->assertRegExp(
$containedHtml,
$this->subject->emogrify()
);
}
}