working on Comic model
This commit is contained in:
parent
714e1995cd
commit
2e3427f500
5
lib/ComicException.php
Normal file
5
lib/ComicException.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class ComicException extends Exception {}
|
||||
|
||||
?>
|
64
model/Comic.php
Normal file
64
model/Comic.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
require_once(dirname(__FILE__) . '/../lib/ComicException.php');
|
||||
|
||||
class Comic {
|
||||
public function __construct($info) {
|
||||
if (!is_array($info)) { throw new ComicException("must be an array"); }
|
||||
|
||||
foreach ($info as $key => $value) {
|
||||
if (is_string($key)) {
|
||||
switch ($key) {
|
||||
case "date":
|
||||
if (!is_numeric($value)) {
|
||||
if (($value = strtotime($value)) === false) {
|
||||
throw new ComicException("must be a valid time");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function process_directory($directory) {
|
||||
if (!is_array($directory) && !is_string($directory)) { throw new ComicException("must be an array or a string"); }
|
||||
|
||||
$result = array();
|
||||
$found_entries = array();
|
||||
|
||||
if (is_array($directory)) {
|
||||
foreach ($directory as $filename) {
|
||||
$base = pathinfo($filename, PATHINFO_FILENAME);
|
||||
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
||||
|
||||
if (!isset($found_entries[$base])) {
|
||||
$found_entries[$base] = array();
|
||||
}
|
||||
|
||||
if (($time_result = strtotime($base)) !== false) {
|
||||
$found_entries[$base]['date'] = $time_result;
|
||||
}
|
||||
|
||||
if (!isset($found_entries[$base]['title'])) {
|
||||
$found_entries[$base]['title'] = $base;
|
||||
}
|
||||
|
||||
switch ($extension) {
|
||||
case "txt":
|
||||
$found_entries[$base]['copy'] = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($found_entries as $entry_info) {
|
||||
$result[] = new Comic($entry_info);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
68
test/model/TestComic.php
Normal file
68
test/model/TestComic.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
require_once('PHPUnit/Framework.php');
|
||||
require_once(dirname(__FILE__) . '/../../model/Comic.php');
|
||||
require_once(dirname(__FILE__) . '/../../lib/ComicException.php');
|
||||
|
||||
class TestComic extends PHPUnit_Framework_TestCase {
|
||||
/**
|
||||
* @dataProvider directoryListProvider
|
||||
*/
|
||||
public function testProcessDirectoryList($entries, $results) {
|
||||
$this->assertEquals($results, Comic::process_directory($entries));
|
||||
}
|
||||
|
||||
public function directoryListProvider() {
|
||||
return array(
|
||||
array(array(), array()),
|
||||
array(
|
||||
array("2006-01-01.txt"),
|
||||
array(new Comic(array('date' => '2006-01-01',
|
||||
'title' => '2006-01-01',
|
||||
'copy' => "")))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider badDirectoryInfoProvider
|
||||
*/
|
||||
public function testPassBadDirectoryInfo($bad) {
|
||||
$caught = false;
|
||||
try {
|
||||
Comic::process_directory($bad);
|
||||
} catch (ComicException $e) { $caught = true; }
|
||||
$this->assertTrue($caught);
|
||||
}
|
||||
|
||||
public function badDirectoryInfoProvider() {
|
||||
return array(
|
||||
array(null),
|
||||
array(false)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider badConstructorInfoProvider
|
||||
*/
|
||||
public function testBadInstantiateComic($bad) {
|
||||
$caught = false;
|
||||
try {
|
||||
$a = new Comic($bad);
|
||||
} catch (ComicException $e) { $caught = true; }
|
||||
$this->assertTrue($caught);
|
||||
}
|
||||
|
||||
public function badConstructorInfoProvider() {
|
||||
return array(
|
||||
array(null),
|
||||
array(false),
|
||||
array("test"),
|
||||
array(1)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user