From 7259f2b414705a21e63762f4cc3b375652182a0b Mon Sep 17 00:00:00 2001 From: Petro Sokolnykov Date: Mon, 27 Nov 2017 15:38:20 +0200 Subject: [PATCH] add test coverage for all classes --- composer.json | 4 +- .../YandexTurboPages/Counter.php | 3 +- src/sokolnikov911/YandexTurboPages/Item.php | 6 +- .../RelatedItemsListInterface.php | 2 +- .../YandexTurboPages/CounterTest.php | 40 ++++ .../YandexTurboPages/ItemTest.php | 185 ++++++++++++++++++ .../YandexTurboPages/RelatedItemTest.php | 31 +++ .../YandexTurboPages/RelatedItemsListTest.php | 46 +++++ 8 files changed, 308 insertions(+), 9 deletions(-) create mode 100644 tests/sokolnikov911/YandexTurboPages/CounterTest.php create mode 100644 tests/sokolnikov911/YandexTurboPages/ItemTest.php create mode 100644 tests/sokolnikov911/YandexTurboPages/RelatedItemTest.php create mode 100644 tests/sokolnikov911/YandexTurboPages/RelatedItemsListTest.php diff --git a/composer.json b/composer.json index 3e819fc..f16af41 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "sokolnikov911/yandex-turbo-pages-php5", "type": "library", - "description": "Simple Yandex Turbo Pages RSS writer library for PHP 5.4 or later.", + "description": "PHP5.4 Yandex Turbo Pages RSS feed generator", "keywords": [ "yandex", "turbo", @@ -11,7 +11,7 @@ "feed", "php" ], - "version": "v0.9.2", + "version": "v0.9.3", "homepage": "https://github.com/sokolnikov91/yandex-turbo-pages-php5", "license": "MIT", "authors": [ diff --git a/src/sokolnikov911/YandexTurboPages/Counter.php b/src/sokolnikov911/YandexTurboPages/Counter.php index 476ef1b..ba37388 100644 --- a/src/sokolnikov911/YandexTurboPages/Counter.php +++ b/src/sokolnikov911/YandexTurboPages/Counter.php @@ -18,7 +18,8 @@ class Counter implements CounterInterface private $type; private $id; - public function __construct($type, $id){ + public function __construct($type, $id) + { $this->type = $type; $this->id = $id; } diff --git a/src/sokolnikov911/YandexTurboPages/Item.php b/src/sokolnikov911/YandexTurboPages/Item.php index d826aab..e94a548 100644 --- a/src/sokolnikov911/YandexTurboPages/Item.php +++ b/src/sokolnikov911/YandexTurboPages/Item.php @@ -81,11 +81,7 @@ public function asXML() { $xml = new SimpleXMLElement('', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL); - - if (!empty($this->title)) { - $xml->addChild('title', $this->title); - } - + $xml->addChild('title', $this->title); $xml->addChild('link', $this->link); $xml->addCdataChild('turbo:content', $this->turboContent, 'http://turbo.yandex.ru'); $xml->addChild('pubDate', date(DATE_RSS, $this->pubDate)); diff --git a/src/sokolnikov911/YandexTurboPages/RelatedItemsListInterface.php b/src/sokolnikov911/YandexTurboPages/RelatedItemsListInterface.php index 51da4de..f4cf166 100644 --- a/src/sokolnikov911/YandexTurboPages/RelatedItemsListInterface.php +++ b/src/sokolnikov911/YandexTurboPages/RelatedItemsListInterface.php @@ -3,7 +3,7 @@ namespace sokolnikov911\YandexTurboPages; /** - * Interface RelatedItemsInterface + * Interface RelatedItemsListInterface * @package sokolnikov911\YandexTurboPages */ interface RelatedItemsListInterface diff --git a/tests/sokolnikov911/YandexTurboPages/CounterTest.php b/tests/sokolnikov911/YandexTurboPages/CounterTest.php new file mode 100644 index 0000000..130f689 --- /dev/null +++ b/tests/sokolnikov911/YandexTurboPages/CounterTest.php @@ -0,0 +1,40 @@ +assertSame($counter, $counter->appendTo($channel)); + $this->assertAttributeSame([$counter], 'counters', $channel); + } + + public function testCounterAttributes() + { + $counterType = uniqid(); + $counterId = uniqid(); + $counter = new Counter($counterType, $counterId); + $this->assertAttributeEquals($counterType, 'type', $counter); + $this->assertAttributeEquals($counterId, 'id', $counter); + } + + public function testAsXML() + { + $counterType = uniqid(); + $counterId = uniqid(); + + $counter = new Counter($counterType, $counterId); + + $expect = ' + + '; + $this->assertXmlStringEqualsXmlString($expect, $counter->asXML()->asXML()); + } +} diff --git a/tests/sokolnikov911/YandexTurboPages/ItemTest.php b/tests/sokolnikov911/YandexTurboPages/ItemTest.php new file mode 100644 index 0000000..0c77f63 --- /dev/null +++ b/tests/sokolnikov911/YandexTurboPages/ItemTest.php @@ -0,0 +1,185 @@ +assertSame($item, $item->title($title)); + $this->assertAttributeSame($title, 'title', $item); + } + + public function testLink() + { + $link = uniqid(); + $item = new Item(); + $this->assertSame($item, $item->link($link)); + $this->assertAttributeSame($link, 'link', $item); + } + + public function testTurboContent() + { + $turboContent = uniqid(); + $item = new Item(); + $this->assertSame($item, $item->turboContent($turboContent)); + $this->assertAttributeSame($turboContent, 'turboContent', $item); + } + + public function testCategory() + { + $category = uniqid(); + $item = new Item(); + $this->assertSame($item, $item->category($category)); + $this->assertAttributeSame($category, 'category', $item); + } + + public function testAuthor() + { + $author = uniqid(); + $item = new Item(); + $this->assertSame($item, $item->author($author)); + $this->assertAttributeSame($author, 'author', $item); + } + + public function testPubDate() + { + $pubDate = mt_rand(1000000, 9999999); + $item = new Item(); + $this->assertSame($item, $item->pubDate($pubDate)); + $this->assertAttributeSame($pubDate, 'pubDate', $item); + } + + public function testAppendTo() + { + $item = new Item(); + $channel = new Channel(); + $this->assertSame($channel, $channel->addItem($item)); + $this->assertAttributeSame([$item], 'items', $channel); + } + + public function testCdataTurboContent() + { + $content = '
content
'; + $pubDate = time(); + + $item = new Item();; + $this->assertSame($item, $item->pubDate($pubDate)); + $this->assertSame($item, $item->turboContent($content)); + $this->assertAttributeSame($content, 'turboContent', $item); + + $feed = new Feed(); + $channel = new Channel(); + $item->appendTo($channel); + $channel->appendTo($feed); + + $expected = ' + + + + <link/> + <description/> + <item turbo="true"> + <title/> + <link/> + <turbo:content xmlns:turbo="http://turbo.yandex.ru"><![CDATA[' . $content . ']]></turbo:content> + <pubDate>' . date(DATE_RSS, $pubDate). '</pubDate> + </item> + </channel> + </rss>'; + $this->assertXmlStringEqualsXmlString($expected, strval($feed)); + } + + public function testAddRelatedItemList() + { + $relatedItemsList = Mockery::mock($this->relatedItemsListInterface); + $item = new Item(); + $this->assertSame($item, $item->addRelatedItemsList($relatedItemsList)); + $this->assertAttributeSame($relatedItemsList, 'relatedItemsList', $item); + } + + public function testAsXML() + { + $data = $this->dataForXmlTests(); + + $item = new Item(); + $item + ->author($data['author']) + ->pubDate($data['now']) + ->title($data['title']) + ->link($data['link']) + ->turboContent($data['turboContent']) + ->category($data['category']); + + $expect = ' + <item turbo="true"> + <title>' . $data['title'] . ' + ' . $data['link'] . ' + + ' . $data['pubDate'] . ' + ' . $data['category'] . ' + ' . $data['author'] . ' + + '; + + $this->assertXmlStringEqualsXmlString($expect, $item->asXML()->asXML()); + } + + public function testAsXMLWithRelated() + { + $data = $this->dataForXmlTests(); + + $item = new Item(); + $item + ->author($data['author']) + ->pubDate($data['now']) + ->title($data['title']) + ->link($data['link']) + ->turboContent($data['turboContent']) + ->category($data['category']); + + $relatedItemsList = new RelatedItemsList(); + $item->addRelatedItemsList($relatedItemsList); + + $expect = ' + + ' . $data['title'] . ' + ' . $data['link'] . ' + + ' . $data['pubDate'] . ' + ' . $data['category'] . ' + ' . $data['author'] . ' + + + '; + + $this->assertXmlStringEqualsXmlString($expect, $item->asXML()->asXML()); + } + + private function dataForXmlTests() + { + $now = time(); + + $data = [ + 'now' => $now, + 'pubDate' => date(DATE_RSS, $now), + 'title' => 'Thirst page!', + 'link' => 'http://www.example.com/page1.html', + 'turboContent' => 'Some content here!
Second content string.', + 'author' => 'John Smith', + 'category' => 'Auto', + ]; + + return $data; + } +} diff --git a/tests/sokolnikov911/YandexTurboPages/RelatedItemTest.php b/tests/sokolnikov911/YandexTurboPages/RelatedItemTest.php new file mode 100644 index 0000000..0cbe5cc --- /dev/null +++ b/tests/sokolnikov911/YandexTurboPages/RelatedItemTest.php @@ -0,0 +1,31 @@ +assertSame($relatedItem, $relatedItem->appendTo($relatedItemsList)); + $this->assertAttributeSame([$relatedItem], 'relatedItems', $relatedItemsList); + } + + public function testAttributes() + { + $relatedItem = new RelatedItem('Title', 'http://site.com/page.html', 'http://site.com/img.jpg'); + $this->assertAttributeEquals('Title', 'title', $relatedItem); + $this->assertAttributeEquals('http://site.com/page.html', 'link', $relatedItem); + $this->assertAttributeEquals('http://site.com/img.jpg', 'img', $relatedItem); + } + + public function testAsXML() + { + $relatedItem = new RelatedItem('Title', 'http://site.com/page.html', 'http://site.com/img.jpg'); + $expect = 'Title'; + $this->assertXmlStringEqualsXmlString($expect, $relatedItem->asXML()->asXML()); + } +} diff --git a/tests/sokolnikov911/YandexTurboPages/RelatedItemsListTest.php b/tests/sokolnikov911/YandexTurboPages/RelatedItemsListTest.php new file mode 100644 index 0000000..0a1af64 --- /dev/null +++ b/tests/sokolnikov911/YandexTurboPages/RelatedItemsListTest.php @@ -0,0 +1,46 @@ +assertSame($relatedItemsList, $relatedItemsList->appendTo($item)); + $this->assertAttributeSame($relatedItemsList, 'relatedItemsList', $item); + } + + public function testAddItem() + { + $relatedItem = new RelatedItem('Title', 'http://www.site.com/page.html' + , 'http://www.site.com/image.jpg'); + $relatedItemsList = new RelatedItemsList(); + $this->assertSame($relatedItemsList, $relatedItemsList->addItem($relatedItem)); + $this->assertAttributeSame([$relatedItem], 'relatedItems', $relatedItemsList); + } + + public function testAsXML() + { + $relatedItemsList = new RelatedItemsList(); + $expect = ''; + $this->assertXmlStringEqualsXmlString($expect, $relatedItemsList->asXML()->asXML()); + } + + public function testAsXMLWithRelatedPage() + { + $relatedItemsList = new RelatedItemsList(); + $relatedItem = new RelatedItem('Title', 'http://www.site.com/page.html' + , 'http://www.site.com/image.jpg'); + $this->assertSame($relatedItemsList, $relatedItemsList->addItem($relatedItem)); + $expect = ' + + Title + + '; + $this->assertXmlStringEqualsXmlString($expect, $relatedItemsList->asXML()->asXML()); + } +}