-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
158 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
require_once(__DIR__ . '/../../vendor/autoload.php'); | ||
|
||
require_once(__DIR__ . '/ReplaceTableModel.php'); | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class DataMapperReplaceTest extends TestCase | ||
{ | ||
/** @var \PDO */ | ||
private $pdo; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->pdo = new \PDO('mysql:host=localhost;dbname=anorm_test', 'travis', ''); | ||
} | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
$pdo = new \PDO('mysql:host=localhost;dbname=anorm_test', 'travis', ''); | ||
$pdo->query('DROP TABLE IF EXISTS `replace_table`'); | ||
$sql = file_get_contents(__DIR__ . '/TestReplaceSchema.sql'); | ||
$pdo->query($sql); | ||
} | ||
|
||
public function testReplace_OK() | ||
{ | ||
$model0 = new ReplaceTableModel($this->pdo); | ||
$this->assertEquals($this->pdo, $model0->_mapper->pdo); | ||
// Count current rows | ||
$n0 = $model0->countRows(); | ||
$this->assertEquals(0, $n0); | ||
|
||
// Create | ||
$model0->replaceId = 'bob_id'; | ||
$model0->name = 'bob'; | ||
$model0->write(); | ||
|
||
// Count current rows (n+1) | ||
$n1 = $model0->countRows(); | ||
$this->assertEquals($n0 + 1, $n1); | ||
|
||
// Read (data present) | ||
$model1 = new ReplaceTableModel($this->pdo); | ||
$model1->read($model0->replaceId); | ||
$this->assertEquals($model0->name, $model1->name); | ||
|
||
// Update | ||
$model1->name = 'fred'; | ||
$model1->write(); | ||
|
||
// Read (data changed) | ||
$model2 = new ReplaceTableModel($this->pdo); | ||
$model2->read($model1->replaceId); | ||
$this->assertEquals($model1->name, $model2->name); | ||
|
||
// Delete | ||
$model0->_mapper->delete($model0->replaceId); | ||
|
||
// Count current rows (n) | ||
$n2 = $model0->countRows(); | ||
$this->assertEquals($n0, $n2); | ||
|
||
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage Key 'replaceId' must be set when using replace mode | ||
*/ | ||
public function testNoPrimaryKey_Throws() | ||
{ | ||
$model = new ReplaceTableModel($this->pdo); | ||
$model->write(); | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
use Anorm\DataMapper; | ||
use Anorm\Model; | ||
|
||
class ReplaceTableModel extends Model { | ||
public function __construct(\PDO $pdo) | ||
{ | ||
parent::__construct($pdo, DataMapper::createByClass($pdo, $this)); | ||
$this->_mapper->modelPrimaryKey = 'replaceId'; | ||
$this->_mapper->useReplace = true; | ||
} | ||
|
||
public function countRows() | ||
{ | ||
$result = $this->_mapper->query('SELECT * FROM `replace_table`'); | ||
return $result->rowCount(); | ||
} | ||
|
||
public $replaceId; | ||
public $name; | ||
} | ||
|
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,8 @@ | ||
# USE `anorm_test`; | ||
# DROP TABLE `replace_table` IF EXISTS; | ||
CREATE TABLE `replace_table` ( | ||
`replace_id` varchar(32) NOT NULL, | ||
`name` varchar(128) NOT NULL | ||
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | ||
ALTER TABLE `replace_table` | ||
ADD UNIQUE KEY `replace_id` (`replace_id`); |