From aa20b81229c524a4b007e110911977767023556a Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Thu, 15 May 2014 11:51:01 +0200 Subject: [PATCH] Enhanced support for MySQL --- lib/chibi-sql | 2 +- src/Models/Entities/CommentEntity.php | 4 ++-- src/Models/Entities/PostEntity.php | 11 ++++++----- src/Models/Entities/TokenEntity.php | 4 ++-- tests/Tests/JobTests/AddCommentJobTest.php | 2 +- tests/Tests/JobTests/EditCommentJobTest.php | 2 +- .../Tests/JobTests/EditPostContentJobTest.php | 12 ++++++------ tests/Tests/JobTests/ListPostsJobTest.php | 19 ++++++++++++------- 8 files changed, 31 insertions(+), 25 deletions(-) diff --git a/lib/chibi-sql b/lib/chibi-sql index f912adb6..6bb18c1c 160000 --- a/lib/chibi-sql +++ b/lib/chibi-sql @@ -1 +1 @@ -Subproject commit f912adb6115feaac885c0fc6728cff8c8ce606fc +Subproject commit 6bb18c1c6ed7ea952ae7a8dab792d5364a334201 diff --git a/src/Models/Entities/CommentEntity.php b/src/Models/Entities/CommentEntity.php index cd99598d..9ce9d424 100644 --- a/src/Models/Entities/CommentEntity.php +++ b/src/Models/Entities/CommentEntity.php @@ -15,9 +15,9 @@ final class CommentEntity extends AbstractEntity implements IValidatable { $this->id = (int) $row['id']; $this->text = $row['text']; - $this->postId = (int) $row['post_id']; + $this->postId = TextHelper::toIntegerOrNull($row['post_id']); $this->commentDate = $row['comment_date']; - $this->commenterId = (int) $row['commenter_id']; + $this->commenterId = TextHelper::toIntegerOrNull($row['commenter_id']); } public function validate() diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 656d3eda..c9d29c10 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -22,6 +22,7 @@ final class PostEntity extends AbstractEntity implements IValidatable { $this->setSafety(new PostSafety(PostSafety::Safe)); $this->setHidden(false); + $this->setUploader(null); $this->setCreationTime(time()); do { @@ -36,13 +37,13 @@ final class PostEntity extends AbstractEntity implements IValidatable $this->name = $row['name']; $this->origName = $row['orig_name']; $this->fileHash = $row['file_hash']; - $this->fileSize = (int) $row['file_size']; + $this->fileSize = TextHelper::toIntegerOrNull($row['file_size']); $this->mimeType = $row['mime_type']; - $this->hidden = (bool) $row['hidden']; + $this->hidden = TextHelper::toBooleanOrNull($row['hidden']); $this->uploadDate = $row['upload_date']; - $this->imageWidth = (int) $row['image_width']; - $this->imageHeight = (int) $row['image_height']; - $this->uploaderId = (int) $row['uploader_id']; + $this->imageWidth = TextHelper::toIntegerOrNull($row['image_width']); + $this->imageHeight = TextHelper::toIntegerOrNull($row['image_height']); + $this->uploaderId = TextHelper::toIntegerOrNull($row['uploader_id']); $this->source = $row['source']; $this->setCache('comment_count', $row['comment_count']); $this->setCache('fav_count', $row['fav_count']); diff --git a/src/Models/Entities/TokenEntity.php b/src/Models/Entities/TokenEntity.php index 735cd97a..f99fe368 100644 --- a/src/Models/Entities/TokenEntity.php +++ b/src/Models/Entities/TokenEntity.php @@ -25,9 +25,9 @@ final class TokenEntity extends AbstractEntity implements IValidatable public function fillFromDatabase($row) { $this->id = (int) $row['id']; - $this->userId = (int) $row['user_id']; + $this->userId = TextHelper::toIntegerOrNull($row['user_id']); $this->token = $row['token']; - $this->used = (bool) $row['used']; + $this->used = TextHelper::toBooleanOrNull($row['used']); $this->expires = $row['expires']; } diff --git a/tests/Tests/JobTests/AddCommentJobTest.php b/tests/Tests/JobTests/AddCommentJobTest.php index 57de2799..226ecd7a 100644 --- a/tests/Tests/JobTests/AddCommentJobTest.php +++ b/tests/Tests/JobTests/AddCommentJobTest.php @@ -17,7 +17,7 @@ class AddCommentJobTest extends AbstractTest $this->assert->areEqual(1, CommentModel::getCount()); $this->assert->areEqual($text, $comment->getText()); $this->assert->areEqual(Auth::getCurrentUser()->getId(), $comment->getCommenter()->getId()); - $this->assert->areEqual(1, $post->getId()); + $this->assert->isNotNull($post->getId()); $this->assert->areEqual(1, $post->getCommentCount()); $this->assert->isNotNull($comment->getCreationTime()); $this->assert->doesNotThrow(function() use ($comment) diff --git a/tests/Tests/JobTests/EditCommentJobTest.php b/tests/Tests/JobTests/EditCommentJobTest.php index 897621a4..97e4ecad 100644 --- a/tests/Tests/JobTests/EditCommentJobTest.php +++ b/tests/Tests/JobTests/EditCommentJobTest.php @@ -14,7 +14,7 @@ class EditCommentJobTest extends AbstractTest $this->assert->areEqual($text, $comment->getText()); $this->assert->areEqual(Auth::getCurrentUser()->getId(), $comment->getCommenter()->getId()); - $this->assert->areEqual(1, $comment->getPost()->getId()); + $this->assert->isNotNull($comment->getPost()->getId()); $this->assert->isNotNull($comment->getCreationTime()); $this->assert->doesNotThrow(function() use ($comment) { diff --git a/tests/Tests/JobTests/EditPostContentJobTest.php b/tests/Tests/JobTests/EditPostContentJobTest.php index c9af9ecc..dc2e823d 100644 --- a/tests/Tests/JobTests/EditPostContentJobTest.php +++ b/tests/Tests/JobTests/EditPostContentJobTest.php @@ -135,7 +135,7 @@ class EditPostContentJobTest extends AbstractTest $this->assert->throws(function() { $this->uploadFromFile('image.png'); - }, 'Duplicate upload: @1'); + }, 'Duplicate upload: @' . $post->getId()); } public function testDuplicateUrl() @@ -147,7 +147,7 @@ class EditPostContentJobTest extends AbstractTest $this->assert->throws(function() { $this->uploadFromUrl('image.png'); - }, 'Duplicate upload: @1'); + }, 'Duplicate upload: @' . $post->getId()); } public function testDuplicateYoutube() @@ -165,16 +165,16 @@ class EditPostContentJobTest extends AbstractTest JobArgs::ARG_NEW_POST_CONTENT_URL => $url, ]); - $post = $this->postMocker->mockSingle(); - $this->assert->throws(function() use ($post, $url) + $anotherPost = $this->postMocker->mockSingle(); + $this->assert->throws(function() use ($anotherPost, $url) { Api::run( new EditPostContentJob(), [ - JobArgs::ARG_POST_ID => $post->getId(), + JobArgs::ARG_POST_ID => $anotherPost->getId(), JobArgs::ARG_NEW_POST_CONTENT_URL => $url, ]); - }, 'Duplicate upload: @1'); + }, 'Duplicate upload: @' . $post->getId()); } protected function prepare() diff --git a/tests/Tests/JobTests/ListPostsJobTest.php b/tests/Tests/JobTests/ListPostsJobTest.php index 6b47c043..d76d3d45 100644 --- a/tests/Tests/JobTests/ListPostsJobTest.php +++ b/tests/Tests/JobTests/ListPostsJobTest.php @@ -203,9 +203,13 @@ class ListPostsJobTest extends AbstractTest foreach (['id', 'ids'] as $alias) { - $ret = $this->assert->doesNotThrow(function() use ($alias) + $ret = $this->assert->doesNotThrow(function() use ($alias, $posts) { - return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':1,3,5']); + $query = sprintf('%s:9999,%d,%d', + $alias, + $posts[0]->getId(), + $posts[2]->getId()); + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $query]); }); $this->assert->areEqual(2, $ret->entityCount); @@ -311,9 +315,9 @@ class ListPostsJobTest extends AbstractTest foreach (['idmin', 'id_min'] as $alias) { - $ret = $this->assert->doesNotThrow(function() use ($alias) + $ret = $this->assert->doesNotThrow(function() use ($alias, $posts) { - return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':2']); + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':' . $posts[1]->getId()]); }); $this->assert->areEqual(2, $ret->entityCount); @@ -323,9 +327,9 @@ class ListPostsJobTest extends AbstractTest foreach (['idmax', 'id_max'] as $alias) { - $ret = $this->assert->doesNotThrow(function() use ($alias) + $ret = $this->assert->doesNotThrow(function() use ($alias, $posts) { - return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':2']); + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':' . $posts[1]->getId()]); }); $this->assert->areEqual(2, $ret->entityCount); @@ -680,7 +684,8 @@ class ListPostsJobTest extends AbstractTest $this->grantAccess('listPosts'); $num = 15; $posts = $this->postMocker->mockMultiple($num); - $expectedPostIdsSorted = range(1, $num); + $expectedPostIdsSorted = array_map(function($post) { return $post->getId(); }, $posts); + sort($expectedPostIdsSorted); $ret = $this->assert->doesNotThrow(function() {