namespace pq\Gateway;
-include __DIR__."/../../../setup.inc";
+include_once __DIR__."/../../../setup.inc";
-/**
- * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-03-05 at 16:08:03.
- */
class TableTest extends \PHPUnit_Framework_TestCase {
/**
/**
* @var Table
*/
- protected $object;
+ protected $table;
- /**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- */
protected function setUp() {
- $this->conn = new \pq\Connection(PQ_DSN);
- $this->conn->exec(PQ_TEST_DROP_TABLE);
- $this->conn->exec(PQ_TEST_CREATE_TABLE);
+ $this->conn = new \pq\Connection(PQ_TEST_DSN);
+ $this->conn->exec(PQ_TEST_SETUP_SQL);
Table::$defaultConnection = $this->conn;
- $this->object = new Table(PQ_TEST_TABLE_NAME);
+ $this->table = new Table("test");
+ $this->table->getQueryExecutor()->attach(new \QueryLogger());
}
- /**
- * Tears down the fixture, for example, closes a network connection.
- * This method is called after a test is executed.
- */
protected function tearDown() {
- $this->conn->exec(PQ_TEST_DROP_TABLE);
+ $this->conn->exec(PQ_TEST_TEARDOWN_SQL);
}
- /**
- * Creates test data in the test table
- */
- protected function createTestData() {
- $this->conn->exec(PQ_TEST_CREATE_DATA);
- }
-
- /**
- * @covers pq\Gateway\Table::setRowsetPrototype
- */
public function testSetRowsetPrototype() {
$prop = new \ReflectionProperty("\\pq\\Gateway\\Table", "rowset");
$prop->setAccessible(true);
- $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->object));
- $this->object->setRowsetPrototype(null);
- $this->assertNull($prop->getValue($this->object));
- $rowset = new \pq\Gateway\Rowset($this->object);
- $this->object->setRowsetPrototype($rowset);
- $this->assertSame($rowset, $prop->getValue($this->object));
+ $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->table));
+ $this->table->setRowsetPrototype(null);
+ $this->assertNull($prop->getValue($this->table));
+ $rowset = new \pq\Gateway\Rowset($this->table);
+ $this->table->setRowsetPrototype($rowset);
+ $this->assertSame($rowset, $prop->getValue($this->table));
}
- /**
- * @covers pq\Gateway\Table::getConnection
- */
public function testGetConnection() {
- $this->assertSame($this->conn, $this->object->getConnection());
+ $this->assertSame($this->conn, $this->table->getConnection());
}
- /**
- * @covers pq\Gateway\Table::getName
- */
public function testGetName() {
- $this->assertSame(PQ_TEST_TABLE_NAME, $this->object->getName());
+ $this->assertSame("test", $this->table->getName());
}
- /**
- * @covers pq\Gateway\Table::find
- */
public function testFind() {
- $rowset = $this->object->find();
+ $rowset = $this->table->find();
$this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
- $rowset = $this->object->find(array("id = " => 1));
+ $rowset = $this->table->find(array("id = " => 1));
$this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
- $rowset = $this->object->find(array("id = " => 0));
+ $rowset = $this->table->find(array("id = " => 0));
$this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
- $rowset = $this->object->find(array(array("id<" => 2), array("id>" => 2)));
+ $rowset = $this->table->find(array(array("id<" => 2), array("id>" => 2)));
$this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
}
- /**
- * @covers pq\Gateway\Table::create
- */
public function testCreate() {
- $rowset = $this->object->create(array("id" => new \pq\Query\Expr("DEFAULT")));
+ $rowset = $this->table->create(array("id" => new \pq\Query\Expr("DEFAULT")));
$this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
$this->assertCount(1, $rowset);
}
- /**
- * @covers pq\Gateway\Table::update
- */
public function testUpdate() {
- $row = $this->object->create(array())->current();
+ $row = $this->table->create(array())->current();
$data = array(
- "created" => "2013-03-03 03:03:03",
+ "created" => new \pq\DateTime("2013-03-03 03:03:03"),
"counter" => 2,
"number" => 2.2,
"data" => "this is a test",
+ "list" => array(3,2,1),
+ "prop" => null
);
- $row = $this->object->update(array("id = " => $row->id), $data)->current();
+ $row = $this->table->update(array("id = " => $row->id), $data)->current();
$data = array("id" => $row->id->get()) + $data;
- $this->assertSame(array_map(function($v){return strval($v);}, $data), $row->getData());
+ $this->assertEquals($data, $row->getData());
}
- /**
- * @covers pq\Gateway\Table::delete
- */
public function testDelete() {
- $this->object->delete(array("id!=" => 0));
- $this->assertCount(0, $this->object->find());
+ $this->table->delete(array("id!=" => 0));
+ $this->assertCount(0, $this->table->find());
+ }
+
+ public function testWith() {
+ $rowset = $this->table->with(["reftest"], array("another_test_id=" => 2));
+ $this->assertCount(1, $rowset);
+ $this->assertEquals(array(
+ "id" => 2,
+ "created" => new \pq\DateTime("today"),
+ "counter" => 0,
+ "number" => 0,
+ "data" => "today",
+ "list" => array(0,1,2),
+ "prop" => null
+ ), $rowset->current()->getData());
}
-
}