-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
/** | ||
* ark.database | ||
* @copyright 2015 Liu Dong <ddliuhb@gmail.com> | ||
* @license MIT | ||
*/ | ||
|
||
class AutoSlaveTest extends PHPUnit_Framework_TestCase{ | ||
protected $db; | ||
|
||
protected function getSampleUser($key = 1){ | ||
return array( | ||
'name' => 'user'.$key, | ||
'email' => 'user'.$key.'@example.com', | ||
'point' => $key | ||
); | ||
} | ||
|
||
protected function setup(){ | ||
$this->db = new \Ark\Database\Connection('sqlite::memory:', '', '', [ | ||
'auto_slave' => true, | ||
]); | ||
|
||
$this->db->exec(" | ||
CREATE TABLE IF NOT EXISTS contact ( | ||
id INTEGER PRIMARY KEY, | ||
name TEXT, | ||
email TEXT, | ||
point INTEGER DEFAULT 0 | ||
) | ||
"); | ||
|
||
$this->db->addConnection('slave', 'sqlite::memory:', '', ''); | ||
|
||
$this->db->switchConnection('slave'); | ||
$this->db->exec(" | ||
CREATE TABLE IF NOT EXISTS contact ( | ||
id INTEGER PRIMARY KEY, | ||
name TEXT, | ||
email TEXT, | ||
point INTEGER DEFAULT 0 | ||
) | ||
"); | ||
|
||
$this->db->switchConnection(); | ||
} | ||
|
||
protected function getAllByConnection($connection) { | ||
$this->db->switchConnection($connection); | ||
$result = []; | ||
foreach ($this->db->query('SELECT * FROM contact') as $row) { | ||
$result[] = $row['name']; | ||
} | ||
|
||
// switch to automatic mode | ||
$this->db->switchConnection(); | ||
|
||
return $result; | ||
} | ||
|
||
public function testAutoSlave() { | ||
// insert into master | ||
$rst = $this->db->builder() | ||
->insert('contact', [ | ||
'name' => 'user1', | ||
'email' => 'user1@test.com', | ||
]); | ||
|
||
$this->assertTrue($rst > 0); | ||
|
||
// the slave is not updated in our test case | ||
$rst = $this->db->builder() | ||
->select('name') | ||
->from('contact') | ||
->where('name = ?', ['user1']) | ||
->queryValue(); | ||
|
||
$this->assertFalse($rst); | ||
|
||
// confirm above tests | ||
$this->assertContains('user1', $this->getAllByConnection('default')); | ||
$this->assertNotContains('user1', $this->getAllByConnection('slave')); | ||
} | ||
} |
File renamed without changes.