Add basic checkbox test
[MAILPOET-265]
This commit is contained in:
committed by
Jack Kitterhing
parent
c8663c3b79
commit
072c6e48a0
65
tests/unit/Form/Block/CheckboxTest.php
Normal file
65
tests/unit/Form/Block/CheckboxTest.php
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Test\Form\Block;
|
||||||
|
|
||||||
|
use MailPoet\Form\Block\Base;
|
||||||
|
use MailPoet\Form\Block\Checkbox;
|
||||||
|
use MailPoet\Test\Form\HtmlParser;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../HtmlParser.php';
|
||||||
|
|
||||||
|
class CheckboxTest extends \MailPoetUnitTest {
|
||||||
|
/** @var Checkbox */
|
||||||
|
private $checkbox;
|
||||||
|
|
||||||
|
/** @var MockObject|WPFunctions */
|
||||||
|
private $wpMock;
|
||||||
|
|
||||||
|
/** @var MockObject|Base */
|
||||||
|
private $baseMock;
|
||||||
|
|
||||||
|
/** @var HtmlParser */
|
||||||
|
private $htmlParser;
|
||||||
|
|
||||||
|
private $block = [
|
||||||
|
'type' => 'checkbox',
|
||||||
|
'name' => 'Custom checkbox',
|
||||||
|
'id' => '1',
|
||||||
|
'unique' => '1',
|
||||||
|
'static' => '0',
|
||||||
|
'params' => [
|
||||||
|
'label' => 'Input label',
|
||||||
|
'required' => '',
|
||||||
|
'hide_label' => '',
|
||||||
|
'values' => [[
|
||||||
|
'value' => 'Checkbox label',
|
||||||
|
'is_checked' => '1',
|
||||||
|
]],
|
||||||
|
],
|
||||||
|
'position' => '1',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function _before() {
|
||||||
|
parent::_before();
|
||||||
|
$this->wpMock = $this->createMock(WPFunctions::class);
|
||||||
|
$this->wpMock->method('escAttr')->will($this->returnArgument(0));
|
||||||
|
$this->baseMock = $this->createMock(Base::class);
|
||||||
|
$this->checkbox = new Checkbox($this->baseMock, $this->wpMock);
|
||||||
|
$this->htmlParser = new HtmlParser();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItShouldRenderCheckbox() {
|
||||||
|
$this->baseMock->expects($this->once())->method('renderLabel')->willReturn('<label></label>');
|
||||||
|
$this->baseMock->expects($this->once())->method('getFieldName')->willReturn('Field name');
|
||||||
|
$this->baseMock->expects($this->once())->method('getInputValidation')->willReturn('validation="1"');
|
||||||
|
$this->baseMock->expects($this->once())->method('getFieldValue')->willReturn('1');
|
||||||
|
$html = $this->checkbox->render($this->block);
|
||||||
|
$checkboxLabel = $this->htmlParser->getElementByXpath($html, "//label[@class='mailpoet_checkbox_label']");
|
||||||
|
expect($checkboxLabel->nodeValue)->equals(' Checkbox label');
|
||||||
|
$checkbox = $this->htmlParser->getChildElement($checkboxLabel, 'input');
|
||||||
|
$checked = $this->htmlParser->getAttribute($checkbox, 'checked');
|
||||||
|
expect($checked->value)->equals('checked');
|
||||||
|
}
|
||||||
|
}
|
@@ -9,4 +9,23 @@ class HtmlParser extends \MailPoetUnitTest {
|
|||||||
$value = (new \DOMXPath($dom))->query($xpath);
|
$value = (new \DOMXPath($dom))->query($xpath);
|
||||||
return $value ?: new \DOMNodeList();
|
return $value ?: new \DOMNodeList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getElementByXpath(string $html, string $xpath, int $index = 0): \DOMElement {
|
||||||
|
$value = $this->findByXpath($html, $xpath);
|
||||||
|
$element = $value->item($index);
|
||||||
|
assert($element instanceof \DOMElement);
|
||||||
|
return $element;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getChildElement(\DOMElement $element, string $tagName, int $index = 0): \DOMElement {
|
||||||
|
$result = $element->getElementsByTagName($tagName)->item($index);
|
||||||
|
assert($result instanceof \DOMElement);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAttribute(\DOMElement $element, string $attrNam): \DOMAttr {
|
||||||
|
$attr = $element->attributes->getNamedItem($attrNam);
|
||||||
|
assert($attr instanceof \DOMAttr);
|
||||||
|
return $attr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user