From 24b6fcc759ec643a952beae72f094c975368c1dc Mon Sep 17 00:00:00 2001 From: Kokororin Date: Sun, 12 Feb 2017 12:18:57 +0800 Subject: [PATCH] Add Mysql test --- .travis.yml | 4 ++ tests/AppTest.php | 93 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ed4bf93..1094e16 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,14 @@ language: php php: + - 5.4 - 5.5 - 5.6 - 7.0 - hhvm +services: + - mysql + before_install: - composer self-update diff --git a/tests/AppTest.php b/tests/AppTest.php index cf81f4e..52f8714 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -3,9 +3,22 @@ use Kotori\App; use Kotori\Core\Config; +use Kotori\Core\Database; +use PDO; +use PDOException; +use PHPUnit_Framework_TestCase; -class AppTest extends \PHPUnit_Framework_TestCase +class AppTest extends PHPUnit_Framework_TestCase { + const MYSQL_HOST = '127.0.0.1'; + const MYSQL_USER = 'root'; + const MYSQL_PWD = ''; + const MYSQL_DB = 'kotori_php_test_db'; + + public function __construct() + { + $this->createTestDatabase(); + } public function testApp() { @@ -35,4 +48,82 @@ public function testGetConfigArray() $config->initialize(); $this->assertTrue(is_array($config->getArray())); } + + protected function createTestDatabase() + { + try { + $pdo = new PDO('mysql:host=' . self::MYSQL_HOST, self::MYSQL_USER, self::MYSQL_PWD); + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $pdo->exec('DROP DATABASE IF EXISTS `' . self::MYSQL_DB . '`; +CREATE DATABASE `' . self::MYSQL_DB . '`; +USE `' . self::MYSQL_DB . '`; + +DROP TABLE IF EXISTS `table`; +CREATE TABLE `table` ( + `id` int(11) NOT NULL, + `name` varchar(255) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8;'); + } catch (PDOException $e) { + throw $e; + } + + } + + protected function getDatabaseInstance() + { + $config = Config::getSoul(); + $config->initialize([ + 'APP_DEBUG' => false, + 'DB' => [ + 'db' => [ + 'TYPE' => 'mysql', + 'NAME' => self::MYSQL_DB, + 'HOST' => self::MYSQL_HOST, + 'USER' => self::MYSQL_USER, + 'PWD' => self::MYSQL_PWD, + ], + ], + ]); + $database = Database::getSoul(); + return $database; + } + + public function testInsert() + { + $insertId = $this->getDatabaseInstance() + ->insert('table', [ + 'id' => 1, + 'name' => 'kotori', + ]); + $this->assertNotEquals(0, $insertId); + } + + public function testSelect() + { + $datas = $this->getDatabaseInstance() + ->select('table', '*', [ + 'id' => 1, + ]); + $this->assertEquals('kotori', $datas[0]['name']); + } + + public function testUpdate() + { + $effectedRows = $this->getDatabaseInstance() + ->update('table', [ + 'name' => 'honoka', + ], [ + 'id' => 1, + ]); + $this->assertGreaterThan(0, $effectedRows); + } + + public function testDelete() + { + $effectedRows = $this->getDatabaseInstance() + ->delete('table', [ + 'id' => 1, + ]); + $this->assertGreaterThan(0, $effectedRows); + } }