Exemple #1
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();
 }
Exemple #2
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());
 }
Exemple #3
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));
 }
Exemple #4
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));
 }
Exemple #5
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());
 }
Exemple #6
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());
 }
 public function testRunSqlInsert()
 {
     $connectionManager = new ConnectionManager();
     $connection = $this->getMock('Sloths\\Db\\Connection', ['exec', 'getLastInsertId'], ['dsn']);
     $connection->expects($this->once())->method('exec');
     $connection->expects($this->once())->method('getLastInsertId')->willReturn('id');
     $connectionManager->setConnection($connection);
     $insert = new Insert();
     $this->assertSame('id', $connectionManager->run($insert));
 }