Files
piratepoet/tests/unit/Form/Block/CheckboxTest.php
Rostislav Wolny 072c6e48a0 Add basic checkbox test
[MAILPOET-265]
2020-02-13 18:42:55 +00:00

66 lines
2.1 KiB
PHP

<?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');
}
}