Skip to content

Commit 5a604fd

Browse files
committed
Added tests
1 parent 585892a commit 5a604fd

7 files changed

Lines changed: 203 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
22
composer.lock
3-
example/cache/*
3+
example/cache/*
4+
phpunit.xml

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ $ php composer.phar update sprain/newsparser
3535
See [example.php](example/example.php).
3636

3737
## Todos
38-
* Write unit tests
38+
* Improve unit tests (e.q. test caching of fetched articles)
3939
* Add more news platforms
4040
* Add functionality to get latest articles
4141
* Add functionality to get details of fetched articles
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Sprain\NewsParser\Test\NewsParser\Cache;
4+
5+
use Doctrine\Common\Cache\FilesystemCache;
6+
use Sprain\NewsParser\Cache\Cache;
7+
8+
class CacheTest extends \PHPUnit_Framework_TestCase
9+
{
10+
protected $cacheDir = '../../testcache';
11+
12+
public function setUp()
13+
{
14+
$this->cache = new Cache();
15+
$this->cache->setCacheDir(__DIR__ . DIRECTORY_SEPARATOR . $this->cacheDir);
16+
}
17+
18+
public function tearDown()
19+
{
20+
$this->rrmdir(__DIR__ . DIRECTORY_SEPARATOR . $this->cacheDir);
21+
}
22+
23+
public function testGetCache()
24+
{
25+
$this->assertTrue($this->cache->getCache() instanceof FilesystemCache);
26+
}
27+
28+
public function testGetCacheFailsWithoutCacheDir()
29+
{
30+
$this->cache->setCacheDir(null);
31+
$this->assertFalse($this->cache->getCache() instanceof FilesystemCache);
32+
}
33+
34+
public function testDisableCache()
35+
{
36+
$this->cache->disable();
37+
$this->assertFalse($this->cache->getCache() instanceof FilesystemCache);
38+
}
39+
40+
public function testSaveAndFetch()
41+
{
42+
$this->cache->save('foo', 'bar');
43+
$this->assertSame('bar', $this->cache->fetch('foo'));
44+
}
45+
46+
public function testClearCache()
47+
{
48+
$this->cache->save('foo', 'bar');
49+
$this->cache->clear();
50+
$this->assertFalse($this->cache->fetch('foo'));
51+
}
52+
53+
public function testSetLifetime()
54+
{
55+
$this->cache->setCacheLifetime(1);
56+
$this->assertSame(1, $this->cache->getCacheLifetime());
57+
$this->cache->save('foo', 'bar');
58+
$this->assertSame('bar', $this->cache->fetch('foo'));
59+
sleep(2);
60+
$this->assertFalse($this->cache->fetch('foo'));
61+
}
62+
63+
protected function rrmdir($dir) {
64+
if (is_dir($dir)) {
65+
$objects = scandir($dir);
66+
foreach ($objects as $object) {
67+
if ($object != "." && $object != "..") {
68+
if (filetype($dir."/".$object) == "dir") $this->rrmdir($dir."/".$object); else unlink($dir."/".$object);
69+
}
70+
}
71+
reset($objects);
72+
rmdir($dir);
73+
}
74+
}
75+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Sprain\NewsParser\Test\NewsParser;
4+
5+
use Sprain\NewsParser\Cache\Cache;
6+
use Sprain\NewsParser\NewsParser;
7+
use Sprain\NewsParser\Parser\Platforms\CH\NzzParser;
8+
9+
class NewsParserTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function setUp()
12+
{
13+
$this->newsParser = new NewsParser();
14+
}
15+
16+
public function testGetPlatform()
17+
{
18+
$this->assertTrue($this->newsParser->getPlatform('ch-nzz') instanceof NzzParser);
19+
}
20+
21+
public function testGetCache()
22+
{
23+
$this->assertTrue($this->newsParser->getCache() instanceof Cache);
24+
}
25+
26+
public function testSetCacheDir()
27+
{
28+
$this->newsParser->setCacheDir('foo');
29+
$this->assertSame('foo', $this->newsParser->getCache()->getCacheDir());
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Sprain\NewsParser\Test\NewsParser\Parser\Platforms;
4+
5+
require_once(__DIR__ . '/../../../TestData/Platforms/FOO/BarParser.php');
6+
use Sprain\NewsParser\Cache\Cache;
7+
use Sprain\NewsParser\Test\TestData\Platforms\FOO\BarParser;
8+
9+
class PlatformParserTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function setUp()
12+
{
13+
$this->parser = new BarParser();
14+
}
15+
16+
public function testGetMostReadArticles()
17+
{
18+
$this->assertSame('5 most read', $this->parser->getMostReadArticles(5));
19+
}
20+
21+
public function testGetRecommendedArticles()
22+
{
23+
$this->assertSame('5 recommended', $this->parser->getRecommendedArticles(5));
24+
}
25+
26+
public function testGetMostCommentedArticles()
27+
{
28+
$this->assertSame('5 most commented', $this->parser->getMostCommentedArticles(5));
29+
}
30+
31+
public function testSetGetCache()
32+
{
33+
$this->parser->setCache(new Cache());
34+
$this->assertTrue($this->parser->getCache() instanceof Cache);
35+
}
36+
37+
public function testGetRootUrl()
38+
{
39+
$this->assertSame('http://www.bar.com', $this->parser->getRootUrl());
40+
}
41+
42+
public function testGetIconUrl()
43+
{
44+
$this->assertSame('http://www.google.com/s2/favicons?domain=http://www.bar.com', $this->parser->getIconUrl());
45+
}
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Sprain\NewsParser\Test\TestData\Platforms\FOO;
4+
5+
use Sprain\NewsParser\Parser\Platforms\PlatformParser;
6+
use Sunra\PhpSimple\HtmlDomParser;
7+
8+
class BarParser extends PlatformParser
9+
{
10+
protected $rootUrl = 'http://www.bar.com';
11+
12+
/**
13+
* @inherit
14+
*/
15+
public function doGetMostReadArticles($limit = null)
16+
{
17+
return $limit . ' most read';
18+
}
19+
20+
/**
21+
* @inherit
22+
*/
23+
public function doGetRecommendedArticles($limit = null)
24+
{
25+
return $limit . ' recommended';
26+
}
27+
28+
/**
29+
* @inherit
30+
*/
31+
public function doGetMostCommentedArticles($limit = null)
32+
{
33+
return $limit . ' most commented';
34+
}
35+
}

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true" bootstrap="vendor/autoload.php">
3+
<testsuites>
4+
<testsuite name="NewsParser">
5+
<directory>./Tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<filter>
9+
<whitelist>
10+
<directory>./Tests</directory>
11+
</whitelist>
12+
</filter>
13+
</phpunit>

0 commit comments

Comments
 (0)