From 1f43d2c71caabed9c41f384cddbe0b50d6751586 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sun, 27 Nov 2016 23:55:01 +0100 Subject: [PATCH] tests: first tests for our HTML classes --- .../Businessprocess/Html/AttributeTest.php | 150 ++++++++++++++++++ .../Businessprocess/Html/AttributesTest.php | 111 +++++++++++++ .../library/Businessprocess/Html/HtmlTest.php | 42 +++++ .../library/Businessprocess/Html/LinkTest.php | 43 +++++ 4 files changed, 346 insertions(+) create mode 100644 test/php/library/Businessprocess/Html/AttributeTest.php create mode 100644 test/php/library/Businessprocess/Html/AttributesTest.php create mode 100644 test/php/library/Businessprocess/Html/HtmlTest.php create mode 100644 test/php/library/Businessprocess/Html/LinkTest.php diff --git a/test/php/library/Businessprocess/Html/AttributeTest.php b/test/php/library/Businessprocess/Html/AttributeTest.php new file mode 100644 index 0000000..af4d82b --- /dev/null +++ b/test/php/library/Businessprocess/Html/AttributeTest.php @@ -0,0 +1,150 @@ +assertInstanceOf( + get_class(new Attribute('a', 'b')), + Attribute::create('a', 'b') + ); + } + + public function testKnowsItsName() + { + $a = new Attribute('target', '_blank'); + $this->assertEquals( + 'target', + $a->getName() + ); + } + + public function testKnowsItsValue() + { + $a = new Attribute('target', '_blank'); + $this->assertEquals( + '_blank', + $a->getValue() + ); + } + + public function testItsValueCanBeModified() + { + $a = new Attribute('target', '_blank'); + $a->setValue('_self'); + $this->assertEquals( + '_self', + $a->getValue() + ); + } + + public function testPreservesComplexValues() + { + $a = new Attribute('special', 'süß "\'&'); + $this->assertEquals( + 'süß "\'&', + $a->getValue() + ); + } + + public function testAllowsToExtendItsValue() + { + $a = new Attribute('class', 'first'); + $a->addValue('second'); + + $this->assertEquals( + array('first', 'second'), + $a->getValue() + ); + + $a->addValue('third'); + + $this->assertEquals( + array('first', 'second', 'third'), + $a->getValue() + ); + + $a->addValue(array('some', 'more')); + + $this->assertEquals( + array('first', 'second', 'third', 'some', 'more'), + $a->getValue() + ); + } + + public function testAcceptsAndReturnsArrayValues() + { + $value = array('first', 'second', 'third'); + $a = new Attribute('class', $value); + + $this->assertEquals( + $value, + $a->getValue() + ); + + $value[] = 'forth'; + + $a->setValue($value); + $this->assertEquals( + $value, + $a->getValue() + ); + } + + public function testCorrectlyRendersItsName() + { + $a = new Attribute('class', 'test'); + $this->assertEquals( + 'class', + $a->renderName() + ); + } + + public function testCorrectlyRendersItsValue() + { + $a = new Attribute('href', '/some/url?a=b&c=d'); + $this->assertEquals( + '/some/url?a=b&c=d', + $a->renderValue() + ); + } + + public function testCorrectlyRendersArrayValues() + { + $a = new Attribute('weird', array('"sü?ß', '/some/url?a=b&c=d')); + $this->assertEquals( + '"sü?ß /some/url?a=b&c=d', + $a->renderValue() + ); + } + + public function testCorrectlyEscapesAName() + { + $this->assertEquals( + 'class', + Attribute::escapeName('class') + ); + } + + public function testCorrectlyEscapesAValue() + { + $this->assertEquals( + ""sü?ß' /some/url?a=b&c=d", + Attribute::escapeValue('"sü?ß\' /some/url?a=b&c=d') + ); + } + + public function testRendersCorrectls() + { + $a = new Attribute('weird', array('"sü?ß', '/some/url?a=b&c=d')); + $this->assertEquals( + 'weird=""sü?ß /some/url?a=b&c=d"', + $a->render() + ); + } +} diff --git a/test/php/library/Businessprocess/Html/AttributesTest.php b/test/php/library/Businessprocess/Html/AttributesTest.php new file mode 100644 index 0000000..9cbfc08 --- /dev/null +++ b/test/php/library/Businessprocess/Html/AttributesTest.php @@ -0,0 +1,111 @@ + array('small', 'nice'), + 'target' => '_blank' + ) + ); + + $this->assertEquals( + ' class="small nice" target="_blank"', + $a->render() + ); + } + + public function testCanBeInstantiatedThroughCreate() + { + $class = get_class(new Attributes()); + + $this->assertInstanceOf( + $class, + Attributes::create() + ); + + $this->assertInstanceOf( + $class, + Attributes::create(array('some' => 'attr')) + ); + } + + public function testCanBeCreatedForArrayOrNullOrAttributes() + { + $empty = Attributes::wantAttributes(null); + $this->assertEquals('', $empty->render()); + + $array = Attributes::wantAttributes(array('a' => 'b')); + $this->assertEquals(' a="b"', $array->render()); + + $attributes = Attributes::wantAttributes( + Attributes::create(array('a' => 'b')) + ); + $this->assertEquals(' a="b"', $attributes->render()); + } + + public function testCanBeExtendedWithAnAttribute() + { + $a = Attributes::create(); + $a->add(Attribute::create('a', 'b')); + $this->assertEquals(' a="b"', $a->render()); + + $a->add(Attribute::create('c', 'd')); + $this->assertEquals(' a="b" c="d"', $a->render()); + + $a->add(Attribute::create('a', 'c')); + $this->assertEquals(' a="b c" c="d"', $a->render()); + } + + public function testCanBeExtendedWithAttributes() + { + $a = Attributes::create(); + $a->add(Attributes::create(array('a' => 'b'))); + $this->assertEquals(' a="b"', $a->render()); + + $a->add(Attributes::create( + array( + 'a' => 'c', + 'c' => 'd' + ) + )); + $this->assertEquals(' a="b c" c="d"', $a->render()); + } + + public function testCanBeExtendedWithANameValuePair() + { + $a = Attributes::create(); + $a->add('a', 'b'); + $this->assertEquals(' a="b"', $a->render()); + } + + public function testAttributesCanBeReplacedWithAnAttribute() + { + $a = Attributes::create(); + $a->add(Attribute::create('a', 'b')); + $a->set(Attribute::create('a', 'c')); + $this->assertEquals(' a="c"', $a->render()); + } + + public function testAttributesCanBeReplacedWithANameValuePair() + { + $a = Attributes::create(); + $a->add(Attribute::create('a', 'b')); + $a->set('a', 'c'); + $this->assertEquals(' a="c"', $a->render()); + } + + public function testCanBeConstructedAndRenderedEmpty() + { + $a = new Attributes(); + $this->assertEquals('', $a->render()); + } +} diff --git a/test/php/library/Businessprocess/Html/HtmlTest.php b/test/php/library/Businessprocess/Html/HtmlTest.php new file mode 100644 index 0000000..87200cd --- /dev/null +++ b/test/php/library/Businessprocess/Html/HtmlTest.php @@ -0,0 +1,42 @@ +setContent('Some content'); + $this->assertEquals( + 'Some content', + $h->render() + ); + } + + public function testContentCanBeExtended() + { + $h = new Html(); + $h->setContent('Some content'); + $h->addContent('More content'); + $this->assertEquals( + 'Some contentMore content', + $h->render() + ); + } + + public function testSeparatorsAreRespected() + { + $h = new Html(); + $h->setContent('Some content'); + $h->setSeparator(', and '); + $h->addContent('More content'); + $this->assertEquals( + 'Some content, and More content', + $h->render() + ); + } +} diff --git a/test/php/library/Businessprocess/Html/LinkTest.php b/test/php/library/Businessprocess/Html/LinkTest.php new file mode 100644 index 0000000..a8d4b37 --- /dev/null +++ b/test/php/library/Businessprocess/Html/LinkTest.php @@ -0,0 +1,43 @@ +assertEquals( + 'Click here', + $l->renderContent() + ); + } + + public function testSimpleLinkRendersCorrectly() + { + $l = Link::create('Click here', 'go/some/where'); + $this->assertEquals( + 'Click here', + $l->render() + ); + } + + public function testLinkWithParamsRendersCorrectly() + { + $l = Link::create( + 'Click here', + 'go/some/where', + array( + 'with' => 'me', + 'and' => 'aDog' + ) + ); + $this->assertEquals( + 'Click here', + $l->render() + ); + } +}