public function test() { $rows = [['id' => 2, 'name' => 'foo'], ['id' => 3, 'name' => 'bar']]; $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 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 = 1))")->willReturn($stmt); $connectionManager = new ConnectionManager(); $connectionManager->setConnection($connection); $user = new User(['id' => 1]); $user->setDefaultConnectionManager($connectionManager); $roles = $user->getHasMany('Roles'); $this->assertSame($rows, $roles->toArray()); }
public function test() { $rows = [['id' => 2, 'user_id' => 1], ['id' => 3, 'user_id' => 1]]; $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.id, posts.user_id, posts.title FROM posts WHERE (posts.user_id = 1)")->willReturn($stmt); $connectionManager = new ConnectionManager(); $connectionManager->setConnection($connection); $user = new User(['id' => 1]); $user->setDefaultConnectionManager($connectionManager); $posts = $user->getHasMany('Posts'); $this->assertSame($rows, $posts->toArray()); $this->assertTrue($user->hasHasMany('Posts')); }