/** * @coversNothing */ public function testRels() { $user = User::find(1); $user->name = 'New Name'; $user->isBlocked = true; $user->object = new SaveableObject(); $user->object->setVar('value'); $address = $user->getAddress(); $address->location = 'Somewhere else'; $address->zipCode = '1234'; $posts = $user->getPosts(); $post = $posts->getFirst(); $post->body = 'Changed Body'; $post = new Post(['title' => 'new post', 'body' => 'Lorem Ipsum', 'price' => 123.23]); $posts->add($post); $tags = Tag::whereIn('id', [1, 2])->load(); $post->getTags()->addModels($tags); $this->assertQueries(['SELECT `User`.* FROM `User` WHERE (`id` = 1) AND (`User`.`deletedAt` IS NULL) LIMIT 1', 'SELECT `Address`.* FROM `Address` WHERE (`id` IN (1))', 'SELECT `Post`.`class`, `Post`.* FROM `Post` WHERE (`userId` IN (1))', 'SELECT `Tag`.* FROM `Tag` WHERE (`id` IN (1, 2))']); User::save($user); $this->assertQueries(['SELECT `User`.* FROM `User` WHERE (`id` = 1) AND (`User`.`deletedAt` IS NULL) LIMIT 1', 'SELECT `Address`.* FROM `Address` WHERE (`id` IN (1))', 'SELECT `Post`.`class`, `Post`.* FROM `Post` WHERE (`userId` IN (1))', 'SELECT `Tag`.* FROM `Tag` WHERE (`id` IN (1, 2))', 'INSERT INTO `Post` (`id`, `title`, `body`, `price`, `tags`, `createdAt`, `updatedAt`, `publishedAt`, `userId`, `class`) VALUES (NULL, "new post", "Lorem Ipsum", "123.23", NULL, NULL, NULL, NULL, NULL, "Harp\\Harp\\Test\\TestModel\\Post")', 'INSERT INTO `PostTag` (`id`, `postId`, `tagId`) VALUES (NULL, NULL, 1), (NULL, NULL, 2)', 'UPDATE `User` SET `name` = "New Name", `isBlocked` = 1, `object` = "C:41:"Harp\\Harp\\Test\\Integration\\SaveableObject":22:{a:1:{i:0;s:5:"value";}}" WHERE (`id` = 1)', 'UPDATE `Address` SET `zipCode` = "1234", `location` = "Somewhere else" WHERE (`id` = 1)', 'UPDATE `Post` SET `body` = CASE `id` WHEN 1 THEN "Changed Body" ELSE `body` END, `userId` = CASE `id` WHEN 5 THEN 1 ELSE `userId` END WHERE (`id` IN (1, 5))', 'UPDATE `PostTag` SET `postId` = CASE `id` WHEN 4 THEN "5" WHEN 5 THEN "5" ELSE `postId` END WHERE (`id` IN (4, 5))']); Container::clear(); $user = User::find(1); $this->assertEquals('New Name', $user->name); $this->assertEquals('value', $user->object->getVar()); $this->assertEquals(true, $user->isBlocked); $address = $user->getAddress(); $this->assertEquals('Somewhere else', $address->location); $this->assertEquals('1234', $address->zipCode); $posts = $user->getPosts(); $post = $posts->getFirst(); $this->assertEquals('Changed Body', $post->body); $newPost = Post::where('title', 'new post')->loadFirst(); $this->assertTrue($posts->has($newPost)); $this->assertEquals([1, 2], $newPost->getTags()->get()->getIds()); }
public function testTest() { $user1 = User::find(1); $address1 = $user1->getAddress(); $post1 = $user1->getPosts()->getFirst(); $user2 = User::find(1); $address2 = $user2->getAddress(); $post2 = $user2->getPosts()->getFirst(); $address3 = Address::find(1); $post3 = Post::find(1); $this->assertSame($user1, $user2); $this->assertSame($address1, $address2); $this->assertSame($post1, $post2); $this->assertSame($address1, $address3); $this->assertSame($post1, $post3); }
/** * @covers ::join */ public function testJoinSoftDelete() { $repo = Tag::getRepo(); $repo->getConfig()->setSoftDelete(true); $rel = new HasManyThrough('tags', Post::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\Tag', 'postTags'); $select = new Select(Post::getRepo()); $rel->join($select, 'Address'); $this->assertEquals('SELECT `Post`.* FROM `Post` JOIN `PostTag` AS `postTags` ON `postTags`.`postId` = `Address`.`id` JOIN `Tag` AS `tags` ON `tags`.`id` = `postTags`.`tagId` AND `tags`.`deletedAt` IS NULL', $select->humanize()); }
/** * @covers ::isModel */ public function testIsModel() { $postConfig = Post::getRepo()->getConfig(); $blogPostConfig = BlogPost::getRepo()->getConfig(); $post = new Post(); $blogPost = new BlogPost(); $this->assertTrue($postConfig->isModel($post)); $this->assertTrue($postConfig->isModel($blogPost)); $this->assertTrue($blogPostConfig->isModel($post)); $this->assertTrue($blogPostConfig->isModel($blogPost)); $city = new City(); $this->assertFalse($postConfig->isModel($city)); }
public function testInheritence() { $post1 = Post::find(1); $post2 = BlogPost::find(1); $this->assertSame($post1, $post2); }
/** * @covers ::getRootRepo */ public function testGetRootRepo() { $this->assertSame(Post::getRepo(), Post::getRepo()->getRootRepo()); $this->assertSame(Post::getRepo(), BlogPost::getRepo()->getRootRepo()); }
/** * @covers ::loadCount */ public function testLoadCount() { $find = $this->getMock('Harp\\Harp\\Find', ['applyFlags'], [Post::getRepo()]); $find->expects($this->once())->method('applyFlags')->with($this->equalTo(State::DELETED)); $count = $find->loadCount(State::DELETED); $this->assertEquals(4, $count); $this->assertQueries(['SELECT COUNT(`Post`.`id`) AS `countAll` FROM `Post`']); }
/** * @covers ::getId * @covers ::setId */ public function testGetSetId() { $post = Post::find(2); $this->assertEquals(2, $post->getId()); $post->setId(4); $this->assertEquals(4, $post->getId()); }