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()); }
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)); }
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)); }
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()); }
public function testSet() { $users = new Collection([['id' => 1, 'username' => 'foo'], ['id' => 2, 'username' => 'bar']], new MockModelForTestCollection()); $users->username = '******'; $this->assertSame('baz', $users->getAt(0)->username); $this->assertSame('baz', $users->getAt(1)->username); }