Example #1
0
 public function test()
 {
     $readConnection = new Connection('dsn');
     $writeConnection = new Connection('dsn');
     $cacheManager = new CacheManager();
     $connectionManager = new ConnectionManager();
     $connectionManager->setReadConnection($readConnection);
     $connectionManager->setWriteConnection($writeConnection);
     $connectionManager->setCacheManager($cacheManager);
     $table = new Table('users', 'id');
     $table->setConnectionManager($connectionManager);
     $select = $table->select('name');
     $this->assertSame($readConnection, $select->getConnection());
     $this->assertSame($cacheManager, $select->getCacheManager());
     $this->assertSame("SELECT users.name FROM users", $select->toString());
     $select = $table->selectById(1, 'name');
     $this->assertSame("SELECT users.name FROM users WHERE (users.id = 1)", $select->toString());
     $insert = $table->insert(['name' => 'foo']);
     $this->assertSame($writeConnection, $insert->getConnection());
     $this->assertSame("INSERT INTO users SET name = 'foo'", $insert->toString());
     $update = $table->update(['name' => 'foo']);
     $this->assertSame($writeConnection, $update->getConnection());
     $this->assertSame("UPDATE users SET name = 'foo'", $update->toString());
     $delete = $table->delete('id', 1);
     $this->assertSame($writeConnection, $delete->getConnection());
     $this->assertSame("DELETE FROM users WHERE (id = 1)", $delete->toString());
 }
Example #2
0
 /**
  * @param ConnectionManager $connectionManager
  * @return $this
  */
 public function setConnectionManager(ConnectionManager $connectionManager)
 {
     $this->connectionManager = $connectionManager;
     if (!$this->cacheManager && ($cacheManager = $connectionManager->getCacheManager(false))) {
         $this->setCacheManager($cacheManager);
     }
     return $this;
 }
Example #3
0
 public function testRollback()
 {
     $directory = __DIR__ . '/fixtures/migrations';
     $migration = ['version' => '20140901000000', 'className' => 'MigrationStub\\Foo', 'file' => $directory . '/20140901000000-Foo.php'];
     $pdo = $this->getMock('mockpdo', ['exec']);
     $pdo->expects($this->at(0))->method('exec')->with("foo down");
     $pdo->expects($this->at(1))->method('exec')->with("DELETE FROM `migrations` WHERE `version` = '20140901000000'");
     $connection = $this->getMock('Sloths\\Db\\Connection', ['getPdo'], ['dsn']);
     $connection->expects($this->atLeast(1))->method('getPdo')->willReturn($pdo);
     $connectionManager = new ConnectionManager();
     $connectionManager->setConnection($connection);
     $migrator = $this->getMock('Sloths\\Db\\Migration\\Migrator', ['getLastMigrated', 'triggerEventListener']);
     $migrator->expects($this->once())->method('getLastMigrated')->willReturn($migration);
     $migrator->expects($this->at(1))->method('triggerEventListener')->with('rollback', [$migration]);
     $migrator->expects($this->at(2))->method('triggerEventListener')->with('rolledback', [$migration]);
     $migrator->setDirectory($directory)->setConnectionManager($connectionManager);
     $migrator->rollback();
 }
Example #4
0
 public function testWithParentCollection()
 {
     $userRows = [['id' => 1], ['id' => 2], ['id' => 3]];
     $postRows = [['id' => 1, 'user_id' => 1, 'title' => 'foo'], ['id' => 2, 'user_id' => 1, 'title' => 'bar'], ['id' => 3, 'user_id' => 2, 'title' => 'baz']];
     $users = new Collection($userRows, new User());
     $user1 = $users->getAt(0);
     $user2 = $users->getAt(1);
     $user3 = $users->getAt(2);
     $stmt = $this->getMock('stmt', ['fetchAll']);
     $stmt->expects($this->once())->method('fetchAll')->with(\PDO::FETCH_ASSOC)->willReturn($postRows);
     $connection = $this->getMock('Sloths\\Db\\Connection', ['query'], ['dsn']);
     $connection->expects($this->once())->method('query')->with("SELECT posts.id, posts.user_id, posts.title FROM posts WHERE (posts.user_id IN (1, 2, 3))")->willReturn($stmt);
     $connectionManager = new ConnectionManager();
     $connectionManager->setConnection($connection);
     $user1->setDefaultConnectionManager($connectionManager);
     $posts = $user1->getHasMany('Posts');
     $this->assertSame([['id' => 1, 'user_id' => 1, 'title' => 'foo'], ['id' => 2, 'user_id' => 1, 'title' => 'bar']], $posts->toArray());
     $this->assertSame([['id' => 3, 'user_id' => 2, 'title' => 'baz']], $user2->getRelation('Posts', true)->toArray());
     $this->assertSame([], $user3->getRelation('Posts', true)->toArray());
 }
Example #5
0
 public function testWithParentCollection()
 {
     $userRows = [['id' => 1], ['id' => 2], ['id' => 3]];
     $users = new Collection($userRows, new User());
     $user1 = $users->getAt(0);
     $user2 = $users->getAt(1);
     $user3 = $users->getAt(2);
     $profileRows = [['user_id' => 1, 'resume' => 'foo'], ['user_id' => 2, 'resume' => 'bar']];
     $stmt = $this->getMock('stmt', ['fetchAll']);
     $stmt->expects($this->once())->method('fetchAll')->with(\PDO::FETCH_ASSOC)->willReturn($profileRows);
     $connection = $this->getMock('Sloths\\Db\\Connection', ['query'], ['dsn']);
     $connection->expects($this->once())->method('query')->with("SELECT profiles.* FROM profiles WHERE (profiles.user_id IN (1, 2, 3))")->willReturn($stmt);
     $connectionManager = new ConnectionManager();
     $connectionManager->setConnection($connection);
     $user1->setDefaultConnectionManager($connectionManager);
     $profile = $user1->getHasOne('Profile');
     $this->assertSame($profileRows[0], $profile->toArray());
     $this->assertSame($profileRows[1], $user2->getRelation('Profile', true)->toArray());
     $this->assertNull($user3->getRelation('Profile', true));
 }
Example #6
0
 /**
  * @return string
  */
 public function toString()
 {
     $values = (array) $this->values;
     $values = ConnectionManager::quote($values);
     $sets = [];
     foreach ($values as $column => $value) {
         $sets[] = $column . ' = ' . $value;
     }
     $result = implode(', ', $sets);
     return $result;
 }
Example #7
0
 public function testWithParentCollection()
 {
     $postRows = [['id' => 1, 'user_id' => 2], ['id' => 2, 'user_id' => 2], ['id' => 3, 'user_id' => 3], ['id' => 4, 'user_id' => null]];
     $posts = new Collection($postRows, new Post());
     $post1 = $posts->getAt(0);
     $post2 = $posts->getAt(1);
     $post3 = $posts->getAt(2);
     $post4 = $posts->getAt(3);
     $userRows = [['id' => 2, 'name' => 'foo'], ['id' => 3, 'name' => 'bar']];
     $stmt = $this->getMock('stmt', ['fetchAll']);
     $stmt->expects($this->once())->method('fetchAll')->with(\PDO::FETCH_ASSOC)->willReturn($userRows);
     $connection = $this->getMock('Sloths\\Db\\Connection', ['query'], ['dsn']);
     $connection->expects($this->once())->method('query')->with("SELECT users.* FROM users WHERE (users.id IN (2, 3))")->willReturn($stmt);
     $connectionManager = new ConnectionManager();
     $connectionManager->setConnection($connection);
     $post1->setDefaultConnectionManager($connectionManager);
     $user = $post1->getBelongsTo('User');
     $this->assertSame($userRows[0], $user->toArray());
     $this->assertSame($user, $post2->getRelation('User', true));
     $this->assertSame($userRows[1], $post3->getRelation('User', true)->toArray());
     $this->assertNull($post4->getRelation('User', true));
 }
Example #8
0
 public function testWithParentCollection()
 {
     $userRows = [['id' => 1], ['id' => 2], ['id' => 3]];
     $roleRows = [['id' => 1, 'name' => 'foo', 'user_id' => 1], ['id' => 2, 'name' => 'bar', 'user_id' => 2], ['id' => 3, 'name' => 'baz', 'user_id' => 1]];
     $users = new Collection($userRows, new User());
     $user1 = $users->getAt(0);
     $user2 = $users->getAt(1);
     $user3 = $users->getAt(2);
     $stmt = $this->getMock('stmt', ['fetchAll']);
     $stmt->expects($this->once())->method('fetchAll')->with(\PDO::FETCH_ASSOC)->willReturn($roleRows);
     $connection = $this->getMock('Sloths\\Db\\Connection', ['query'], ['dsn']);
     $connection->expects($this->once())->method('query')->with("SELECT roles.id, roles.name, user_roles.user_id FROM roles INNER JOIN user_roles ON ((user_roles.role_id = roles.id) AND (user_roles.user_id IN (1, 2, 3)))")->willReturn($stmt);
     $connectionManager = new ConnectionManager();
     $connectionManager->setConnection($connection);
     $user1->setDefaultConnectionManager($connectionManager);
     $roles = $user1->getHasMany('Roles');
     $expected = [['id' => 1, 'name' => 'foo', 'user_id' => 1], ['id' => 3, 'name' => 'baz', 'user_id' => 1]];
     $this->assertSame($expected, $roles->toArray());
     $expected = [['id' => 2, 'name' => 'bar', 'user_id' => 2]];
     $this->assertSame($expected, $user2->getRelation('Roles', true)->toArray());
     $this->assertSame([], $user3->getRelation('Roles', true)->toArray());
 }
Example #9
0
 /**
  * @param $filter
  * @return array
  */
 protected function compileFilter($filter)
 {
     list($type, $condition) = $filter;
     if (is_string($condition) || $filter instanceof Raw) {
         return [$type, $condition];
     } elseif (is_array($condition)) {
         $result = [];
         foreach ($condition as $k => $v) {
             if (is_numeric($k)) {
                 $result[] = $v;
             } else {
                 $result[] = ConnectionManager::bind($k, $v);
             }
         }
         return [$type, implode(' AND ', $result)];
     }
     $subFilter = new static();
     $subFilter->type = '';
     call_user_func($condition, $subFilter);
     return [$type, $subFilter->toString()];
 }
Example #10
0
 public function dataProvider()
 {
     return [["SELECT foo.* FROM foo", 'foo'], ["SELECT foo.* FROM foo", 'foo', '*'], ["SELECT f.* FROM foo AS f", 'foo f'], ["SELECT f.* FROM foo AS f", 'foo AS f'], ["SELECT f.* FROM foo AS f", 'foo as f'], ["SELECT f.* FROM foo AS f", ' foo   as   f '], ["SELECT foo.bar, foo.qux AS q FROM foo", 'foo', ['bar', 'q' => 'qux']], ["SELECT bar, qux AS q FROM foo", 'foo', 'bar, qux AS q'], ["SELECT f.bar, f.qux AS q FROM foo AS f", 'foo f', ['bar', 'q' => 'qux']], ["SELECT COUNT(*) AS c FROM foo AS f", 'foo f', ['c' => 'COUNT(*)']], ["SELECT CURRENT_TIMESTAMP, f.bar AS b FROM foo AS f", 'foo f', ['#CURRENT_TIMESTAMP', 'b' => 'bar']], ["SELECT CURRENT_TIMESTAMP, f.bar AS b FROM foo AS f", 'foo f', [ConnectionManager::raw('CURRENT_TIMESTAMP'), 'b' => 'bar']]];
 }
Example #11
0
 public function testFirst()
 {
     $rows = [['id' => 1, 'title' => 'foo']];
     $stmt = $this->getMock('stmt', ['fetchAll']);
     $stmt->expects($this->once())->method('fetchAll')->with(\PDO::FETCH_ASSOC)->willReturn($rows);
     $connection = $this->getMock('Sloths\\Db\\Connection', ['query'], ['dsn']);
     $connection->expects($this->once())->method('query')->with("SELECT posts.* FROM posts WHERE (id = 1) LIMIT 1")->willReturn($stmt);
     $connectionManager = new ConnectionManager();
     $connectionManager->setConnection($connection);
     MockModel::setDefaultConnectionManager($connectionManager);
     $model = MockModel::first(1);
     $this->assertSame($rows[0], $model->toArray());
 }
Example #12
0
 public function dataProviderTestBind()
 {
     return [["foo = 1", 'foo', 1], ["foo = 1", 'foo = ?', 1], ["foo != 1", 'foo != ?', 1], ["foo = 'foo'", 'foo', 'foo'], ["foo IS NULL", 'foo', null], ["foo IS NOT NULL", 'foo != ?', null], ["foo IN (1, 2, 3)", 'foo IN (?)', [1, 2, 3]], ["foo NOT IN (1, 2, 3)", 'foo NOT IN (?)', [1, 2, 3]], ["foo IN (SELECT 1, 2, 3)", 'foo IN(?)', ConnectionManager::raw('SELECT 1, 2, 3')], ["foo LIKE '%foo%'", "foo LIKE %?%", 'foo'], ["foo LIKE '%foo'", "foo LIKE %?", 'foo'], ["foo LIKE 'foo%'", "foo LIKE ?%", 'foo'], ["foo = 1 bar = 'bar'", 'foo = ? bar = ?', [1, 'bar']], ["foo = 1 bar = 'bar'", 'foo = :foo bar = :bar', ['foo' => 1, 'bar' => 'bar']]];
 }