public function testConstruct() { $pdo = $this->factory->getConnection(); $pdo->query('CREATE TABLE foo ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(255) not null )'); $repository = new Repository('foo', 'Db\\Repository\\Foo', $this->factory); $foo = new Foo(); $foo->setName('hello world'); $repository->insert($foo); $this->assertEquals(1, $foo->getId()); $this->assertEquals('hello world', $foo->getName()); /** @var $foo Foo */ $foo = $repository->select()->whereEquals('id', 1)->fetch(); $this->assertEquals(1, $foo->getId()); $this->assertEquals('hello world', $foo->getName()); $foo->setName('2341234'); $repository->update($foo); $this->assertEquals(1, $foo->getId()); $this->assertEquals('2341234', $foo->getName()); $foo = $repository->select()->whereEquals('id', 1)->fetch(); $this->assertEquals(1, $foo->getId()); $this->assertEquals('2341234', $foo->getName()); foreach ($repository->select()->query() as $foo) { $this->assertEquals(1, $foo->getId()); $this->assertEquals('2341234', $foo->getName()); } $repository->delete($foo); $foo = $repository->select()->whereEquals('id', 1)->fetch(); $this->assertNull($foo); }
public function testConstruct() { $pdo = $this->factory->getConnection(); $pdo->query('CREATE TABLE foo ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(255) null )'); $table = new Table('foo', $this->factory); $this->assertEquals('foo', $table->getTableName()); $this->assertCount(2, $table->getTableColumns()); $this->assertArrayHasKey('id', $table->getTableColumns()); $this->assertArrayHasKey('name', $table->getTableColumns()); $id = $table->getColumn('id'); $this->assertNotNull($id); $this->assertEquals(null, $id->getDefault()); $this->assertEquals(false, $id->getIsNull()); $this->assertEquals(true, $id->getIsPrimary()); $this->assertEquals('id', $id->getName()); $this->assertEquals('integer', $id->getType()); $name = $table->getColumn('name'); $this->assertNotNull($name); $this->assertEquals(null, $name->getDefault()); $this->assertEquals(true, $name->getIsNull()); $this->assertEquals(false, $name->getIsPrimary()); $this->assertEquals('name', $name->getName()); $this->assertEquals('string', $name->getType()); }
public function testUnitOfWork() { $this->factory->getConnection()->query('CREATE TABLE foo ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(255) not null )'); $repository = new Repository('foo', 'Db\\Mapper\\FooUnitEntity', $this->factory); $mapper = new RepositoryMapper(); $mapper->setRepository('Db\\Mapper\\FooUnitEntity', $repository); $uof = new UnitOfWork($mapper); $entity = new FooUnitEntity(); $entity->setName('hello world'); $uof->registerNew($entity); $this->assertEquals(null, $entity->getId()); $fetched = $repository->select()->fetch(); $this->assertNull($fetched); $uof->commit(); $this->assertEquals(1, $entity->getId()); $fetched = $repository->select()->fetch(); $this->assertNotNull($fetched); }
public function __construct($tableName, ConnectionFactory $connectionFactory) { $this->tableName = (string) $tableName; $pdo = $connectionFactory->getConnection(); $driverName = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); switch ($driverName) { case 'sqlite': $stmt = $pdo->query("PRAGMA table_info({$tableName})"); while ($columnData = $stmt->fetch(\PDO::FETCH_ASSOC)) { $column = new TableColumn($columnData); $column->setName($columnData['name']); $column->setType($columnData['type']); $column->setIsNull((int) $columnData['notnull'] === 0); $column->setIsPrimary((int) $columnData['pk'] === 1); $column->setDefault($columnData['dflt_value']); $this->tableColumns[$column->getName()] = $column; } break; default: throw new \ErrorException('unsupported driver'); } }
public function delete($entity) { $delete = new Delete($this->tableName); foreach ($this->connectionFactory->getTable($this->tableName)->getTableColumns() as $column) { if ($column->getIsPrimary()) { $value = $column->getDefault(); $name = $column->getName(); if (method_exists($entity, 'get' . $name)) { $value = $entity->{'get' . $name}(); } elseif (method_exists($entity, $name)) { $value = $entity->{$name}(); } elseif (isset($entity->{$name})) { $value = $entity->{$name}; } $delete->whereEquals($name, $value); } } if (!$delete->hasCondition()) { throw new QueryError('No condition found'); } $this->connectionFactory->query($delete); }
public function testInvalidQuery() { $this->setExpectedException('Db\\ExecuteException'); $this->factory->query(new RawQueryTest('Error')); }