Skip to content

Commit

Permalink
add test coverage for all classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Petro Sokolnykov committed Nov 27, 2017
1 parent 595d1cc commit 7259f2b
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 9 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": [
Expand Down
3 changes: 2 additions & 1 deletion src/sokolnikov911/YandexTurboPages/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 1 addition & 5 deletions src/sokolnikov911/YandexTurboPages/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ public function asXML()
{
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><item turbo="true"></item>', 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace sokolnikov911\YandexTurboPages;

/**
* Interface RelatedItemsInterface
* Interface RelatedItemsListInterface
* @package sokolnikov911\YandexTurboPages
*/
interface RelatedItemsListInterface
Expand Down
40 changes: 40 additions & 0 deletions tests/sokolnikov911/YandexTurboPages/CounterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace sokolnikov911\YandexTurboPages;

use PHPUnit\Framework\TestCase;

class CounterTest extends TestCase
{
public function testAppendTo()
{
$counterType = uniqid();
$counterId = uniqid();
$counter = new Counter($counterType, $counterId);
$channel = new Channel();
$this->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 = '
<yandex:analytics id="' . $counterId . '" type="' . $counterType . '"/>
';
$this->assertXmlStringEqualsXmlString($expect, $counter->asXML()->asXML());
}
}
185 changes: 185 additions & 0 deletions tests/sokolnikov911/YandexTurboPages/ItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

namespace sokolnikov911\YandexTurboPages;

use Mockery;
use PHPUnit\Framework\TestCase;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

class ItemTest extends TestCase
{
use MockeryPHPUnitIntegration;

private $relatedItemsListInterface = '\sokolnikov911\YandexTurboPages\RelatedItemsList';

public function testTitle()
{
$title = uniqid();
$item = new Item();
$this->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 = '<div>content</div>';
$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 = '<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:yandex="http://news.yandex.ru" xmlns:media="http://search.yahoo.com/mrss/" xmlns:turbo="http://turbo.yandex.ru" version="2.0">
<channel>
<title/>
<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'] . '</title>
<link>' . $data['link'] . '</link>
<turbo:content xmlns:turbo="http://turbo.yandex.ru"><![CDATA[' . $data['turboContent'] . ']]></turbo:content>
<pubDate>' . $data['pubDate'] . '</pubDate>
<category>' . $data['category'] . '</category>
<author>' . $data['author'] . '</author>
</item>
';

$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 = '
<item turbo="true">
<title>' . $data['title'] . '</title>
<link>' . $data['link'] . '</link>
<turbo:content xmlns:turbo="http://turbo.yandex.ru"><![CDATA[' . $data['turboContent'] . ']]></turbo:content>
<pubDate>' . $data['pubDate'] . '</pubDate>
<category>' . $data['category'] . '</category>
<author>' . $data['author'] . '</author>
<yandex:related/>
</item>
';

$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!<br>Second content string.',
'author' => 'John Smith',
'category' => 'Auto',
];

return $data;
}
}
31 changes: 31 additions & 0 deletions tests/sokolnikov911/YandexTurboPages/RelatedItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace sokolnikov911\YandexTurboPages;

use PHPUnit\Framework\TestCase;

class RelatedItemTest extends TestCase
{
public function testAppendTo()
{
$relatedItem = new RelatedItem('Title', 'http://site.com/page.html', 'http://site.com/img.jpg');
$relatedItemsList = new RelatedItemsList();
$this->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 = '<link url="http://site.com/page.html" img="http://site.com/img.jpg">Title</link>';
$this->assertXmlStringEqualsXmlString($expect, $relatedItem->asXML()->asXML());
}
}
46 changes: 46 additions & 0 deletions tests/sokolnikov911/YandexTurboPages/RelatedItemsListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace sokolnikov911\YandexTurboPages;

use PHPUnit\Framework\TestCase;

class RelatedItemsListTest extends TestCase
{
public function testAppendTo()
{
$relatedItemsList = new RelatedItemsList();
$item = new Item();
$this->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 = '<yandex:related/>';
$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 = '
<yandex:related>
<link url="http://www.site.com/page.html" img="http://www.site.com/image.jpg">Title</link>
</yandex:related>
';
$this->assertXmlStringEqualsXmlString($expect, $relatedItemsList->asXML()->asXML());
}
}

0 comments on commit 7259f2b

Please sign in to comment.