Skip to content

Commit

Permalink
Test suite changes:
Browse files Browse the repository at this point in the history
 * sqlite runs in memory
 * ensure the same db resource is used all over
 * small changes to to confirm my refactoring
  • Loading branch information
till committed Nov 3, 2012
1 parent c0220ed commit f1af57a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
4 changes: 4 additions & 0 deletions tests/Mail/Queue/ContainerTest.php
Expand Up @@ -14,6 +14,10 @@ public function testContainerGet()
$body = 'Lorem ipsum';

$mailId = $this->queue->put($sender, $recipient, $headers, $body, 0, true, $id_user);
if (!is_numeric($mailId)) {
$this->fail("Could not save email.");
return;
}

$message = $this->queue->container->getMailById($mailId);
$this->assertTrue(($message instanceof Mail_Queue_Body));
Expand Down
54 changes: 28 additions & 26 deletions tests/Mail/QueueAbstract.php
Expand Up @@ -4,11 +4,6 @@
*/
abstract class Mail_QueueAbstract extends PHPUnit_Framework_TestCase
{
/**
* @var string $db Name of the database file.
*/
protected $db = 'mailqueueTestSuite';

/**
* @var string $dsn String to connect to sqlite.
*/
Expand All @@ -19,6 +14,11 @@ abstract class Mail_QueueAbstract extends PHPUnit_Framework_TestCase
*/
protected $queue;

/**
* @var MDB2
*/
protected $mdb2;

/**
* @var string $table The table name.
*/
Expand All @@ -37,13 +37,16 @@ public function setUp()
return;
}

$this->dsn = 'sqlite:///' . __DIR__ . "/{$this->db}?mode=0644";
$this->dsn = 'sqlite:///:memory:';

$this->mdb2 = MDB2::connect($this->dsn);
$this->handlePearError($this->mdb2, "DB connection init");

$this->setUpDatabase($this->dsn);
$this->setUpDatabase();

$container_opts = array(
'type' => 'mdb2',
'dsn' => $this->dsn,
'dsn' => $this->mdb2,
'mail_table' => $this->table,
);

Expand Down Expand Up @@ -71,31 +74,30 @@ public function setUp()
*/
public function tearDown()
{
$mdb2 = MDB2::connect($this->dsn);
$mdb2->query("DROP TABLE '{$this->table}'");
$mdb2->disconnect();
if ($this->mdb2 instanceof MDB2_Error) {
var_dump($mdb2); exit;
}
if ($this->mdb2 === null) {
return;
}
$this->mdb2->loadModule('manager');
$this->mdb2->dropTable($this->table);
$this->mdb2->disconnect();

//$this->queue->container->db->disconnect();
unset($this->queue);

unlink(__DIR__ . "/{$this->db}");
}

/**
* Create the table.
*
* @param string $dsn The DSN.
*
* @return void
* @see self::setUp()
* @see self::tearDown()
*/
protected function setUpDatabase($dsn)
protected function setUpDatabase()
{
$mdb2 = MDB2::connect($dsn);
$this->handlePearError($mdb2, "DB connection init");

$status = $mdb2->loadModule('Manager');
$status = $this->mdb2->loadModule('Manager');
$this->handlePearError($status, "Loading manager module");

$cols = array(
Expand Down Expand Up @@ -148,10 +150,10 @@ protected function setUpDatabase($dsn)
),
);

$status = $mdb2->manager->createTable($this->table, $cols);
$status = $this->mdb2->manager->createTable($this->table, $cols);
$this->handlePearError($status, "Create table");

$status = $mdb2->manager->createConstraint(
$status = $this->mdb2->manager->createConstraint(
$this->table,
'idx',
array(
Expand All @@ -163,7 +165,7 @@ protected function setUpDatabase($dsn)
);
$this->handlePearError($status, "Create primary key");

$status = $mdb2->manager->createIndex(
$status = $this->mdb2->manager->createIndex(
$this->table,
't2s',
array('fields' => array(
Expand All @@ -172,15 +174,15 @@ protected function setUpDatabase($dsn)
);
$this->handlePearError($status, "Index on time_to_send");

$status = $mdb2->manager->createIndex(
$status = $this->mdb2->manager->createIndex(
$this->table,
'idu',
array('fields' => array(
'id_user' => array())
)
);

$this->handlePearError($status, 'Index on id_user');
$mdb2->disconnect();
}

/**
Expand All @@ -194,7 +196,7 @@ protected function setUpDatabase($dsn)
*/
protected function handlePearError($err, $action)
{
if (PEAR::isError($err)) {
if (MDB2::isError($err)) {
$this->fail("{$action}: {$err->getDebugInfo()}");
return;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Mail/QueueMock.php
Expand Up @@ -24,7 +24,7 @@ public function setUp()

$this->queueMock->expects($this->once())
->method('get')
->will($this->returnValue(Pear::raiseError("OH NOEZ")));
->will($this->returnValue(new Pear_Error("OH NOEZ")));
}

/**
Expand Down
14 changes: 8 additions & 6 deletions tests/Mail/QueueTest.php
Expand Up @@ -15,21 +15,23 @@ public function testPut()
$body = 'Lorem ipsum';

$mailId = $this->queue->put($sender, $recipient, $headers, $body, $time_to_send=0, true, $id_user);
if (!is_numeric($mailId)) {
$this->handlePearError($mailId, "Could not save email.");
}

$this->assertEquals(1, $mailId); // it's the first email, after all :-)
$this->assertEquals(1, count($this->queue->getQueueCount()));
}

/**
* This should return a MDB2_Error
*
* @expectedException Mail_Queue_Exception
*/
public function testSendMailByIdWithInvalidId()
{
$randomId = rand(1, 12);
$status = $this->queue->sendMailById($randomId);

$this->assertContains('no such message', $status->getDebugInfo());
$this->assertTrue(($status instanceof Mail_Queue_Error));
}

/**
Expand All @@ -48,7 +50,7 @@ public function testSendMailsInQueue()
$body = 'Lorem ipsum';

$mailId1 = $this->queue->put($sender, $recipient, $headers, $body);
if (PEAR::isError($mailId1)) {
if ($mailId1 instanceof PEAR_Error) {
$this->fail("Queueing first mail failed: {$mailId1->getMessage()}");
return;
}
Expand All @@ -60,7 +62,7 @@ public function testSendMailsInQueue()
$body = 'Lorem ipsum sit dolor';

$mailId2 = $this->queue->put($sender, $recipient, $headers, $body);
if (PEAR::isError($mailId2)) {
if ($mailId2 instanceof PEAR_Error) {
$this->fail("Queueing first mail failed: {$mailId2->getMessage()}");
return;
}
Expand All @@ -77,7 +79,7 @@ public function testSendMailsInQueue()
$this->assertEquals(2, $queueCount, "Failed to count 2 messages.");

$status = $this->queue->sendMailsInQueue();
if (Pear::isError($status)) {
if ($status instanceof PEAR_Error) {
$this->fail("Error sending emails: {$status->getMessage()}.");
return;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/TestInit.php
Expand Up @@ -13,6 +13,9 @@ class MailQueueTestInit
{
public static function autoload($className)
{
if (strpos($className, 'Mail') !== 0) {
return;
}
$file = str_replace('_', '/', $className) . '.php';
return include $file;
}
Expand Down

0 comments on commit f1af57a

Please sign in to comment.