From 980d59332e0f8208dcb27a7b4f754e41158c7dc3 Mon Sep 17 00:00:00 2001 From: uckelman Date: Sun, 11 Apr 2010 12:54:04 +0000 Subject: Added setMessageIdIfAbsent(). git-svn-id: https://vassalengine.svn.sourceforge.net/svnroot/vassalengine/site-src/trunk@6639 67b53d14-2c14-4ace-a08f-0dab2b34000c --- src/Bridge.php | 8 ++++++++ test/BridgeTest.php | 21 +++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Bridge.php b/src/Bridge.php index ad3b6c3..537fb5c 100644 --- a/src/Bridge.php +++ b/src/Bridge.php @@ -53,6 +53,14 @@ class Bridge { return $row['forum_id']; } + public function setMessageIdIfAbsent($messageId) { + $sql = 'INSERT IGNORE INTO posts (message_id) VALUES (' . + $this->db->quote($messageId) . ')'; + + $count = $this->db->exec($sql); + return $count == 1; + } + protected function get_exactly_one_row($sql) { $result = $this->db->query($sql); diff --git a/test/BridgeTest.php b/test/BridgeTest.php index b90ae02..309a959 100644 --- a/test/BridgeTest.php +++ b/test/BridgeTest.php @@ -18,11 +18,11 @@ class BridgeTest extends PHPUnit_Framework_TestCase { $this->db->exec( 'CREATE TABLE posts (' . - 'post_id MEDIUMINT UNSIGNED NOT NULL, ' . + 'post_id MEDIUMINT UNSIGNED, ' . 'message_id VARCHAR(255) NOT NULL, ' . 'in_reply_to VARCHAR(255), ' . 'refs BLOB, ' . - 'PRIMARY KEY (post_id), ' . + 'UNIQUE KEY (post_id), ' . 'UNIQUE KEY (message_id))' ); @@ -141,6 +141,23 @@ class BridgeTest extends PHPUnit_Framework_TestCase { array('messages@forums.vassalengine.org', 2, null), ); } + + /** + * @dataProvider providerSetMessageIdIfAbsent + */ + public function testSetMessageIdIfAbsent($messageId, $expected, $ex) { + if ($ex) $this->setExpectedException($ex); + $bridge = new Bridge($this->db); + $this->assertEquals($expected, $bridge->setMessageIdIfAbsent($messageId)); + } + + public function providerSetMessageIdIfAbsent() { + return array( + array(null, null, 'PHPUnit_Framework_Error'), + array('<20100302094228.33F0310091@charybdis.ellipsis.cx>', false, null), + array('<10100302094228.33F0310091@charybdis.ellipsis.cx>', true, null), + ); + } } ?> -- cgit v1.2.3