示例#1
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());
 }
示例#2
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));
 }
示例#3
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));
 }
示例#4
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());
 }
示例#5
0
 public function testCallShouldPassToSelectIfMethodNotExists()
 {
     $select = $this->getMock('Sloths\\Db\\Table\\Sql\\Select', ['foo']);
     $select->expects($this->once())->method('foo');
     $collection = new Collection($select, $this->mockModel);
     $collection->foo();
 }
示例#6
0
文件: Url.php 项目: lytc/sloths
 /**
  * @param Collection $collection
  * @param array $queryParams
  * @return string
  */
 public function add(Collection $collection, array $queryParams = [])
 {
     $tableName = $collection->getModel()->getTableName();
     return $this->to(Inflector::dasherize($tableName) . '/new', $queryParams);
 }
示例#7
0
 /**
  * @param int $from
  * @param int $length
  * @return Collection
  */
 public function getRange($from, $length)
 {
     $this->collection->limit($length, $from);
     return $this->collection;
 }