Skip to content

Commit

Permalink
Add Mysql test
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Jun 29, 2017
1 parent 3991b91 commit 24b6fcc
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

services:
- mysql

before_install:
- composer self-update

Expand Down
93 changes: 92 additions & 1 deletion tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 24b6fcc

Please sign in to comment.