diff --git a/application/database/seeds/DatabaseSeeder.php b/application/database/seeds/DatabaseSeeder.php new file mode 100644 index 0000000..acb3375 --- /dev/null +++ b/application/database/seeds/DatabaseSeeder.php @@ -0,0 +1,15 @@ +db->query($line); + } + } +} diff --git a/application/database/seeds/librivox_catalog_scrubbed.sql.bz2 b/application/database/seeds/librivox_catalog_scrubbed.sql.bz2 new file mode 100644 index 0000000..51e0a41 Binary files /dev/null and b/application/database/seeds/librivox_catalog_scrubbed.sql.bz2 differ diff --git a/application/libraries/Seeder.php b/application/libraries/Seeder.php new file mode 100644 index 0000000..02f1d60 --- /dev/null +++ b/application/libraries/Seeder.php @@ -0,0 +1,133 @@ + + * @license MIT License + * @copyright 2015 Kenji Suzuki + * @link https://github.com/kenjis/ci-phpunit-test + */ + +class Seeder +{ + /** + * @var CI_Controller + */ + private $CI; + + /** + * @var CI_DB_query_builder + */ + protected $db; + + /** + * @var CI_DB_forge + */ + protected $dbforge; + + /** + * @var string + */ + protected $seedPath; + + /** + * @var array + */ + protected $depends = []; + + public function __construct() + { + $this->CI =& get_instance(); + $this->CI->load->database(); + $this->CI->load->dbforge(); + $this->db = $this->CI->db; + $this->dbforge = $this->CI->dbforge; + } + + /** + * Run another seeder + * + * @param string $seeder Seeder classname + * @param bool $callDependencies + */ + public function call($seeder, $callDependencies = true) + { + if ($this->seedPath === null) + { + $this->seedPath = APPPATH . 'database/seeds/'; + } + + $obj = $this->loadSeeder($seeder); + if ($callDependencies === true && $obj instanceof Seeder) { + $obj->callDependencies($this->seedPath); + } + $obj->run(); + } + + /** + * Get Seeder instance + * + * @param string $seeder + * @return Seeder + */ + protected function loadSeeder($seeder) + { + $file = $this->seedPath . $seeder . '.php'; + require_once $file; + + return new $seeder; + } + + /** + * Call dependency seeders + * + * @param string $seedPath + */ + public function callDependencies($seedPath) + { + foreach ($this->depends as $path => $seeders) { + $this->seedPath = $seedPath; + if (is_string($path)) { + $this->setPath($path); + } + + $this->callDependency($seeders); + } + $this->setPath($seedPath); + } + + /** + * Call dependency seeder + * + * @param string|array $seederName + */ + protected function callDependency($seederName) + { + if (is_array($seederName)) { + array_map([$this, 'callDependency'], $seederName); + return; + } + + $seeder = $this->loadSeeder($seederName); + if (is_string($this->seedPath)) { + $seeder->setPath($this->seedPath); + } + + $seeder->call($seederName, true); + } + + /** + * Set path for seeder files + * + * @param string $path + */ + public function setPath($path) + { + $this->seedPath = rtrim($path, '/').'/'; + } + + public function __get($property) + { + return $this->CI->$property; + } +} diff --git a/application/tests/DbTestCase.php b/application/tests/DbTestCase.php index 62df864..bbc94ee 100644 --- a/application/tests/DbTestCase.php +++ b/application/tests/DbTestCase.php @@ -2,4 +2,12 @@ class DbTestCase extends CIPHPUnitTestDbTestCase { + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + + $CI =& get_instance(); + $CI->load->library('Seeder'); + $CI->seeder->call('DatabaseSeeder'); + } } diff --git a/application/tests/controllers/API_test.php b/application/tests/controllers/API_test.php new file mode 100644 index 0000000..676a9c5 --- /dev/null +++ b/application/tests/controllers/API_test.php @@ -0,0 +1,31 @@ +request( + 'GET', + '/api/feed/audiobooks/id/52', + [ + 'coverart' => 1, + 'format' => 'json', + ] + ); + $books = json_decode($output); + $this->assertEquals( + $books[0]['coverart_jpg'], + 'https://www.archive.org/download/LibrivoxCdCoverArt12/Letters_Two_Brides_1110.jpg' + ); + $this->assertEquals( + $books[0]['coverart_pdf'], + 'https://www.archive.org/download/LibrivoxCdCoverArt12/Letters_Two_Brides_1110.pdf' + ); + $this->assertEquals( + $books[0]['coverart_thumbnail'], + 'https://www.archive.org/download/LibrivoxCdCoverArt12/Letters_Two_Brides_1110_thumb.jpg' + ); + } +} + +?>