@@ -21,14 +21,14 @@ discard block |
||
| 21 | 21 | * @return boolean |
| 22 | 22 | */ |
| 23 | 23 | $buildFeed = function (Feed $feed, $folder, $name = 'rss') { |
| 24 | - $tempFeed = __DIR__ . "/../../public/{$folder}/{$name}-new.xml"; |
|
| 25 | - $finalFeed = __DIR__ . "/../../public/{$folder}/{$name}.xml"; |
|
| 24 | + $tempFeed = __DIR__ . "/../../public/{$folder}/{$name}-new.xml"; |
|
| 25 | + $finalFeed = __DIR__ . "/../../public/{$folder}/{$name}.xml"; |
|
| 26 | 26 | |
| 27 | - $feedHandle = fopen($tempFeed, 'w'); |
|
| 28 | - fwrite($feedHandle, $feed->render()); |
|
| 29 | - fclose($feedHandle); |
|
| 27 | + $feedHandle = fopen($tempFeed, 'w'); |
|
| 28 | + fwrite($feedHandle, $feed->render()); |
|
| 29 | + fclose($feedHandle); |
|
| 30 | 30 | |
| 31 | - rename($tempFeed, $finalFeed); |
|
| 31 | + rename($tempFeed, $finalFeed); |
|
| 32 | 32 | }; |
| 33 | 33 | |
| 34 | 34 | |
@@ -47,56 +47,56 @@ discard block |
||
| 47 | 47 | $activeBlogPosts = $blogPostRepository->getActivePosts(); |
| 48 | 48 | |
| 49 | 49 | foreach ($activeBlogPosts as $blogPost) { |
| 50 | - $blogPostItem = new Item(); |
|
| 51 | - |
|
| 52 | - $blogPostItem->title($blogPost['title']); |
|
| 53 | - |
|
| 54 | - $url = "https://blog.jacobemerick.com/{$blogPost['category']}/{$blogPost['path']}/"; |
|
| 55 | - $blogPostItem->url($url); |
|
| 56 | - $blogPostItem->guid($url, true); |
|
| 57 | - |
|
| 58 | - $description = $blogPost['body']; |
|
| 59 | - $description = strip_tags($description); |
|
| 60 | - $description = strtok($description, "\n"); |
|
| 61 | - if (strlen($description) > 250) { |
|
| 62 | - $description = wordwrap($description, 250); |
|
| 63 | - $description = strtok($description, "\n"); |
|
| 64 | - if (substr($description, -1) != '.') { |
|
| 65 | - $description .= '…'; |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - $description = html_entity_decode($description); |
|
| 69 | - $blogPostItem->description($description); |
|
| 70 | - |
|
| 71 | - $categoryUrl = "https://blog.jacobemerick.com/{$blogPost['category']}/"; |
|
| 72 | - $blogPostItem->category($blogPost['category'], $categoryUrl); |
|
| 73 | - |
|
| 74 | - $pubDate = new DateTime($blogPost['date']); |
|
| 75 | - $blogPostItem->pubDate($pubDate->getTimestamp()); |
|
| 76 | - |
|
| 77 | - $firstPhoto = Content::instance('FetchFirstPhoto', $blogPost['body'])->activate(true, 'large'); |
|
| 78 | - if (!empty($firstPhoto)) { |
|
| 79 | - $firstPhotoPieces = sscanf($firstPhoto, '<img src="%s" height="%d" width="%d" alt="%s" />'); |
|
| 80 | - $firstPhotoPath = current($firstPhotoPieces); |
|
| 81 | - $firstPhotoPath = trim($firstPhotoPath, '"'); |
|
| 82 | - |
|
| 83 | - $firstPhotoInternalPath = __DIR__ . '/../../public' . $firstPhotoPath; |
|
| 84 | - |
|
| 85 | - $firstPhotoSize = filesize($firstPhotoInternalPath); |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * ugh, remote host does not have pecl fileinfo |
|
| 89 | - * |
|
| 90 | - * $fInfo = new finfo(FILEINFO_MIME_TYPE); |
|
| 91 | - * $firstPhotoType = $fInfo->file($firstPhotoInternalPath); |
|
| 92 | - * unset($fInfo); |
|
| 93 | - **/ |
|
| 94 | - $firstPhotoType = 'image/jpeg'; |
|
| 95 | - |
|
| 96 | - $blogPostItem->enclosure("https://blog.jacobemerick.com{$firstPhotoPath}", $firstPhotoSize, $firstPhotoType); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $blogPostItem->appendTo($blogPostChannel); |
|
| 50 | + $blogPostItem = new Item(); |
|
| 51 | + |
|
| 52 | + $blogPostItem->title($blogPost['title']); |
|
| 53 | + |
|
| 54 | + $url = "https://blog.jacobemerick.com/{$blogPost['category']}/{$blogPost['path']}/"; |
|
| 55 | + $blogPostItem->url($url); |
|
| 56 | + $blogPostItem->guid($url, true); |
|
| 57 | + |
|
| 58 | + $description = $blogPost['body']; |
|
| 59 | + $description = strip_tags($description); |
|
| 60 | + $description = strtok($description, "\n"); |
|
| 61 | + if (strlen($description) > 250) { |
|
| 62 | + $description = wordwrap($description, 250); |
|
| 63 | + $description = strtok($description, "\n"); |
|
| 64 | + if (substr($description, -1) != '.') { |
|
| 65 | + $description .= '…'; |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + $description = html_entity_decode($description); |
|
| 69 | + $blogPostItem->description($description); |
|
| 70 | + |
|
| 71 | + $categoryUrl = "https://blog.jacobemerick.com/{$blogPost['category']}/"; |
|
| 72 | + $blogPostItem->category($blogPost['category'], $categoryUrl); |
|
| 73 | + |
|
| 74 | + $pubDate = new DateTime($blogPost['date']); |
|
| 75 | + $blogPostItem->pubDate($pubDate->getTimestamp()); |
|
| 76 | + |
|
| 77 | + $firstPhoto = Content::instance('FetchFirstPhoto', $blogPost['body'])->activate(true, 'large'); |
|
| 78 | + if (!empty($firstPhoto)) { |
|
| 79 | + $firstPhotoPieces = sscanf($firstPhoto, '<img src="%s" height="%d" width="%d" alt="%s" />'); |
|
| 80 | + $firstPhotoPath = current($firstPhotoPieces); |
|
| 81 | + $firstPhotoPath = trim($firstPhotoPath, '"'); |
|
| 82 | + |
|
| 83 | + $firstPhotoInternalPath = __DIR__ . '/../../public' . $firstPhotoPath; |
|
| 84 | + |
|
| 85 | + $firstPhotoSize = filesize($firstPhotoInternalPath); |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * ugh, remote host does not have pecl fileinfo |
|
| 89 | + * |
|
| 90 | + * $fInfo = new finfo(FILEINFO_MIME_TYPE); |
|
| 91 | + * $firstPhotoType = $fInfo->file($firstPhotoInternalPath); |
|
| 92 | + * unset($fInfo); |
|
| 93 | + **/ |
|
| 94 | + $firstPhotoType = 'image/jpeg'; |
|
| 95 | + |
|
| 96 | + $blogPostItem->enclosure("https://blog.jacobemerick.com{$firstPhotoPath}", $firstPhotoSize, $firstPhotoType); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $blogPostItem->appendTo($blogPostChannel); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | $buildFeed($blogPostFeed, 'blog'); |
@@ -114,33 +114,33 @@ discard block |
||
| 114 | 114 | $activeBlogComments = $commentRepository->getActiveCommentsBySite('blog'); |
| 115 | 115 | |
| 116 | 116 | foreach ($activeBlogComments as $blogComment) { |
| 117 | - $blogCommentItem = new Item(); |
|
| 118 | - |
|
| 119 | - $blogCommentItem->title("Comment on '{$blogComment['title']}' from {$blogComment['name']}"); |
|
| 120 | - |
|
| 121 | - $url = "https://blog.jacobemerick.com/{$blogComment['category']}/{$blogComment['path']}/"; |
|
| 122 | - $url .= "#comment-{$blogComment['id']}"; |
|
| 123 | - $blogCommentItem->url($url); |
|
| 124 | - $blogCommentItem->guid($url, true); |
|
| 125 | - |
|
| 126 | - $description = $blogComment['body']; |
|
| 127 | - $description = strip_tags($description); |
|
| 128 | - $description = strtok($description, "\n"); |
|
| 129 | - if (strlen($description) > 250) { |
|
| 130 | - $description = wordwrap($description, 250); |
|
| 131 | - $description = strtok($description, "\n"); |
|
| 132 | - if (substr($description, -1) != '.') { |
|
| 133 | - $description .= '…'; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - $description = html_entity_decode($description); |
|
| 137 | - $description = trim($description); |
|
| 138 | - $blogCommentItem->description($description); |
|
| 139 | - |
|
| 140 | - $pubDate = new DateTime($blogComment['date']); |
|
| 141 | - $blogCommentItem->pubDate($pubDate->getTimestamp()); |
|
| 142 | - |
|
| 143 | - $blogCommentItem->appendTo($blogCommentChannel); |
|
| 117 | + $blogCommentItem = new Item(); |
|
| 118 | + |
|
| 119 | + $blogCommentItem->title("Comment on '{$blogComment['title']}' from {$blogComment['name']}"); |
|
| 120 | + |
|
| 121 | + $url = "https://blog.jacobemerick.com/{$blogComment['category']}/{$blogComment['path']}/"; |
|
| 122 | + $url .= "#comment-{$blogComment['id']}"; |
|
| 123 | + $blogCommentItem->url($url); |
|
| 124 | + $blogCommentItem->guid($url, true); |
|
| 125 | + |
|
| 126 | + $description = $blogComment['body']; |
|
| 127 | + $description = strip_tags($description); |
|
| 128 | + $description = strtok($description, "\n"); |
|
| 129 | + if (strlen($description) > 250) { |
|
| 130 | + $description = wordwrap($description, 250); |
|
| 131 | + $description = strtok($description, "\n"); |
|
| 132 | + if (substr($description, -1) != '.') { |
|
| 133 | + $description .= '…'; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + $description = html_entity_decode($description); |
|
| 137 | + $description = trim($description); |
|
| 138 | + $blogCommentItem->description($description); |
|
| 139 | + |
|
| 140 | + $pubDate = new DateTime($blogComment['date']); |
|
| 141 | + $blogCommentItem->pubDate($pubDate->getTimestamp()); |
|
| 142 | + |
|
| 143 | + $blogCommentItem->appendTo($blogCommentChannel); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | $buildFeed($blogCommentFeed, 'blog', 'rss-comments'); |
@@ -173,17 +173,17 @@ |
||
| 173 | 173 | return self::instance()->is_live; |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | - public static function getRootURL($site = '') |
|
| 177 | - { |
|
| 178 | - if (strlen($site) > 0) { |
|
| 179 | - $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
|
| 180 | - if ($site == 'waterfalls' && self::instance()->is_live) { |
|
| 181 | - return "{$protocol}://www.waterfallsofthekeweenaw.com/"; |
|
| 182 | - } else { |
|
| 183 | - return $protocol . '://' . (self::instance()->is_live ? '' : 'dev.') . $site . '.jacobemerick.com/'; |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - return '/'; |
|
| 187 | - } |
|
| 176 | + public static function getRootURL($site = '') |
|
| 177 | + { |
|
| 178 | + if (strlen($site) > 0) { |
|
| 179 | + $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
|
| 180 | + if ($site == 'waterfalls' && self::instance()->is_live) { |
|
| 181 | + return "{$protocol}://www.waterfallsofthekeweenaw.com/"; |
|
| 182 | + } else { |
|
| 183 | + return $protocol . '://' . (self::instance()->is_live ? '' : 'dev.') . $site . '.jacobemerick.com/'; |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + return '/'; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | 189 | } |
@@ -24,21 +24,21 @@ |
||
| 24 | 24 | |
| 25 | 25 | self::$array['host'] = $host; |
| 26 | 26 | |
| 27 | - if ( |
|
| 28 | - $host == 'www.waterfallsofthekeweenaw.com' || |
|
| 29 | - $host == 'waterfallsofthekeweenaw.com' |
|
| 30 | - ) { |
|
| 27 | + if ( |
|
| 28 | + $host == 'www.waterfallsofthekeweenaw.com' || |
|
| 29 | + $host == 'waterfallsofthekeweenaw.com' |
|
| 30 | + ) { |
|
| 31 | 31 | self::$array['site'] = 'waterfalls'; |
| 32 | 32 | } else { |
| 33 | 33 | self::$array['site'] = substr($host, 0, strpos($host, '.')); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - $base = ''; |
|
| 37 | - $base .= (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
|
| 38 | - $base .= '://'; |
|
| 39 | - $base .= (!Loader::isLive()) ? 'dev' : ''; |
|
| 40 | - $base .= $host; |
|
| 41 | - $base .= '/'; |
|
| 36 | + $base = ''; |
|
| 37 | + $base .= (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
|
| 38 | + $base .= '://'; |
|
| 39 | + $base .= (!Loader::isLive()) ? 'dev' : ''; |
|
| 40 | + $base .= $host; |
|
| 41 | + $base .= '/'; |
|
| 42 | 42 | |
| 43 | 43 | self::$array['base'] = $base; |
| 44 | 44 | self::$array['uri'] = '/' . implode('/', $uri_array); |
@@ -75,19 +75,19 @@ discard block |
||
| 75 | 75 | if($this->requires_trailing_slash() && substr($redirect_uri, -1) != '/') |
| 76 | 76 | $redirect_uri .= '/'; |
| 77 | 77 | |
| 78 | - if (URLDecode::getHost() == 'waterfalls.jacobemerick.com') { |
|
| 79 | - $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
|
| 80 | - $redirect_uri = $protocol . '://' . (!Loader::isLive() ? 'dev' : 'www') . '.waterfallsofthekeweenaw.com' . $redirect_uri; |
|
| 81 | - } |
|
| 78 | + if (URLDecode::getHost() == 'waterfalls.jacobemerick.com') { |
|
| 79 | + $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
|
| 80 | + $redirect_uri = $protocol . '://' . (!Loader::isLive() ? 'dev' : 'www') . '.waterfallsofthekeweenaw.com' . $redirect_uri; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | 83 | if($redirect_uri == URLDecode::getURI()) |
| 84 | 84 | return; |
| 85 | 85 | |
| 86 | 86 | $controller_check = $redirect_uri; |
| 87 | 87 | if(substr($redirect_uri, 0, 4) == 'http') { |
| 88 | - $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
|
| 88 | + $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
|
| 89 | 89 | $controller_check = preg_replace('@^' . $protocol . '://([a-z\.]+)@', '', $redirect_uri); |
| 90 | - } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | $controller = $this->get_controller($controller_check); |
| 93 | 93 | if($controller == '/Error404Controller') |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | { |
| 139 | 139 | return ( |
| 140 | 140 | URLDecode::getExtension() != 'json' && |
| 141 | - strstr(URLDecode::getURI(), '#') === false); |
|
| 141 | + strstr(URLDecode::getURI(), '#') === false); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | } |
@@ -7,108 +7,108 @@ |
||
| 7 | 7 | class MysqlActivityRepository implements ActivityRepositoryInterface |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - /** @var ConnectionLocator */ |
|
| 11 | - protected $connections; |
|
| 10 | + /** @var ConnectionLocator */ |
|
| 11 | + protected $connections; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @param ConnectonLocator $connections |
|
| 15 | - */ |
|
| 16 | - public function __construct(ConnectionLocator $connections) |
|
| 17 | - { |
|
| 18 | - $this->connections = $connections; |
|
| 19 | - } |
|
| 13 | + /** |
|
| 14 | + * @param ConnectonLocator $connections |
|
| 15 | + */ |
|
| 16 | + public function __construct(ConnectionLocator $connections) |
|
| 17 | + { |
|
| 18 | + $this->connections = $connections; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @param integer $id |
|
| 23 | - * |
|
| 24 | - * @return array|false |
|
| 25 | - */ |
|
| 26 | - public function getActivityById($id) |
|
| 27 | - { |
|
| 28 | - $query = " |
|
| 21 | + /** |
|
| 22 | + * @param integer $id |
|
| 23 | + * |
|
| 24 | + * @return array|false |
|
| 25 | + */ |
|
| 26 | + public function getActivityById($id) |
|
| 27 | + { |
|
| 28 | + $query = " |
|
| 29 | 29 | SELECT * |
| 30 | 30 | FROM `jpemeric_stream`.`activity` |
| 31 | 31 | WHERE `id` = :id |
| 32 | 32 | LIMIT 1"; |
| 33 | - $bindings = [ |
|
| 34 | - 'id' => $id, |
|
| 35 | - ]; |
|
| 33 | + $bindings = [ |
|
| 34 | + 'id' => $id, |
|
| 35 | + ]; |
|
| 36 | 36 | |
| 37 | - return $this |
|
| 38 | - ->connections |
|
| 39 | - ->getRead() |
|
| 40 | - ->fetchOne($query, $bindings); |
|
| 41 | - } |
|
| 37 | + return $this |
|
| 38 | + ->connections |
|
| 39 | + ->getRead() |
|
| 40 | + ->fetchOne($query, $bindings); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param integer $limit |
|
| 45 | - * @param integer $offset |
|
| 46 | - * |
|
| 47 | - * @return array|false |
|
| 48 | - */ |
|
| 49 | - public function getActivities($limit = null, $offset = 0) |
|
| 50 | - { |
|
| 51 | - $query = " |
|
| 43 | + /** |
|
| 44 | + * @param integer $limit |
|
| 45 | + * @param integer $offset |
|
| 46 | + * |
|
| 47 | + * @return array|false |
|
| 48 | + */ |
|
| 49 | + public function getActivities($limit = null, $offset = 0) |
|
| 50 | + { |
|
| 51 | + $query = " |
|
| 52 | 52 | SELECT * |
| 53 | 53 | FROM `jpemeric_stream`.`activity` |
| 54 | 54 | ORDER BY `datetime` DESC"; |
| 55 | - if (!is_null($limit)) { |
|
| 56 | - $query .= " |
|
| 55 | + if (!is_null($limit)) { |
|
| 56 | + $query .= " |
|
| 57 | 57 | LIMIT {$offset}, {$limit}"; |
| 58 | - } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - return $this |
|
| 61 | - ->connections |
|
| 62 | - ->getRead() |
|
| 63 | - ->fetchAll($query); |
|
| 64 | - } |
|
| 60 | + return $this |
|
| 61 | + ->connections |
|
| 62 | + ->getRead() |
|
| 63 | + ->fetchAll($query); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - public function getActivitiesCount() |
|
| 67 | - { |
|
| 68 | - $query = " |
|
| 66 | + public function getActivitiesCount() |
|
| 67 | + { |
|
| 68 | + $query = " |
|
| 69 | 69 | SELECT COUNT(1) AS `count` |
| 70 | 70 | FROM `jpemeric_stream`.`activity`"; |
| 71 | 71 | |
| 72 | - return $this |
|
| 73 | - ->connections |
|
| 74 | - ->getRead() |
|
| 75 | - ->fetchValue($query); |
|
| 76 | - } |
|
| 72 | + return $this |
|
| 73 | + ->connections |
|
| 74 | + ->getRead() |
|
| 75 | + ->fetchValue($query); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - public function getActivitiesByType($type, $limit = null, $offset = 0) |
|
| 79 | - { |
|
| 80 | - $query = " |
|
| 78 | + public function getActivitiesByType($type, $limit = null, $offset = 0) |
|
| 79 | + { |
|
| 80 | + $query = " |
|
| 81 | 81 | SELECT * |
| 82 | 82 | FROM `jpemeric_stream`.`activity` |
| 83 | 83 | WHERE `type` = :type |
| 84 | 84 | ORDER BY `datetime` DESC"; |
| 85 | - if (!is_null($limit)) { |
|
| 86 | - $query .= " |
|
| 85 | + if (!is_null($limit)) { |
|
| 86 | + $query .= " |
|
| 87 | 87 | LIMIT {$offset}, {$limit}"; |
| 88 | - } |
|
| 89 | - $bindings = [ |
|
| 90 | - 'type' => $type, |
|
| 91 | - ]; |
|
| 88 | + } |
|
| 89 | + $bindings = [ |
|
| 90 | + 'type' => $type, |
|
| 91 | + ]; |
|
| 92 | 92 | |
| 93 | - return $this |
|
| 94 | - ->connections |
|
| 95 | - ->getRead() |
|
| 96 | - ->fetchAll($query, $bindings); |
|
| 97 | - } |
|
| 93 | + return $this |
|
| 94 | + ->connections |
|
| 95 | + ->getRead() |
|
| 96 | + ->fetchAll($query, $bindings); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - public function getActivitiesByTypeCount($type) |
|
| 100 | - { |
|
| 101 | - $query = " |
|
| 99 | + public function getActivitiesByTypeCount($type) |
|
| 100 | + { |
|
| 101 | + $query = " |
|
| 102 | 102 | SELECT COUNT(1) AS `count` |
| 103 | 103 | FROM `jpemeric_stream`.`activity` |
| 104 | 104 | WHERE `type` = :type"; |
| 105 | - $bindings = [ |
|
| 106 | - 'type' => $type, |
|
| 107 | - ]; |
|
| 105 | + $bindings = [ |
|
| 106 | + 'type' => $type, |
|
| 107 | + ]; |
|
| 108 | 108 | |
| 109 | - return $this |
|
| 110 | - ->connections |
|
| 111 | - ->getRead() |
|
| 112 | - ->fetchValue($query, $bindings); |
|
| 113 | - } |
|
| 109 | + return $this |
|
| 110 | + ->connections |
|
| 111 | + ->getRead() |
|
| 112 | + ->fetchValue($query, $bindings); |
|
| 113 | + } |
|
| 114 | 114 | } |
@@ -7,37 +7,37 @@ |
||
| 7 | 7 | class MysqlChangelogRepository implements ChangelogRepositoryInterface |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - /** @var ConnectionLocator */ |
|
| 11 | - protected $connections; |
|
| 10 | + /** @var ConnectionLocator */ |
|
| 11 | + protected $connections; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @param ConnectonLocator $connections |
|
| 15 | - */ |
|
| 16 | - public function __construct(ConnectionLocator $connections) |
|
| 17 | - { |
|
| 18 | - $this->connections = $connections; |
|
| 19 | - } |
|
| 13 | + /** |
|
| 14 | + * @param ConnectonLocator $connections |
|
| 15 | + */ |
|
| 16 | + public function __construct(ConnectionLocator $connections) |
|
| 17 | + { |
|
| 18 | + $this->connections = $connections; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @param integer $limit |
|
| 23 | - * @param integer $offset |
|
| 24 | - * |
|
| 25 | - * @return array|false |
|
| 26 | - */ |
|
| 27 | - public function getChanges($limit = null, $offset = 0) |
|
| 28 | - { |
|
| 29 | - $query = " |
|
| 21 | + /** |
|
| 22 | + * @param integer $limit |
|
| 23 | + * @param integer $offset |
|
| 24 | + * |
|
| 25 | + * @return array|false |
|
| 26 | + */ |
|
| 27 | + public function getChanges($limit = null, $offset = 0) |
|
| 28 | + { |
|
| 29 | + $query = " |
|
| 30 | 30 | SELECT `message`, `message_short`, `datetime`, `commit_link` |
| 31 | 31 | FROM `jpemeric_stream`.`changelog` |
| 32 | 32 | ORDER BY `datetime` DESC"; |
| 33 | - if (!is_null($limit)) { |
|
| 34 | - $query .= " |
|
| 33 | + if (!is_null($limit)) { |
|
| 34 | + $query .= " |
|
| 35 | 35 | LIMIT {$offset}, {$limit}"; |
| 36 | - } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - return $this |
|
| 39 | - ->connections |
|
| 40 | - ->getRead() |
|
| 41 | - ->fetchAll($query); |
|
| 42 | - } |
|
| 38 | + return $this |
|
| 39 | + ->connections |
|
| 40 | + ->getRead() |
|
| 41 | + ->fetchAll($query); |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -9,14 +9,14 @@ discard block |
||
| 9 | 9 | class MysqlSeriesRepositoryTest extends PHPUnit_Framework_TestCase |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - protected static $connection; |
|
| 12 | + protected static $connection; |
|
| 13 | 13 | |
| 14 | - public static function setUpBeforeClass() |
|
| 15 | - { |
|
| 16 | - $extendedPdo = new ExtendedPdo('sqlite::memory:'); |
|
| 17 | - $extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`"); |
|
| 14 | + public static function setUpBeforeClass() |
|
| 15 | + { |
|
| 16 | + $extendedPdo = new ExtendedPdo('sqlite::memory:'); |
|
| 17 | + $extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`"); |
|
| 18 | 18 | |
| 19 | - $extendedPdo->exec(" |
|
| 19 | + $extendedPdo->exec(" |
|
| 20 | 20 | CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`post` ( |
| 21 | 21 | `id` integer PRIMARY KEY AUTOINCREMENT, |
| 22 | 22 | `title` varchar(60) NOT NULL, |
@@ -26,380 +26,380 @@ discard block |
||
| 26 | 26 | `body` text, |
| 27 | 27 | `display` integer(1) NOT NULL |
| 28 | 28 | )" |
| 29 | - ); |
|
| 30 | - $extendedPdo->exec(" |
|
| 29 | + ); |
|
| 30 | + $extendedPdo->exec(" |
|
| 31 | 31 | CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`series` ( |
| 32 | 32 | `id` integer PRIMARY KEY AUTOINCREMENT, |
| 33 | 33 | `title` text NOT NULL, |
| 34 | 34 | `description` text NOT NULL |
| 35 | 35 | )" |
| 36 | - ); |
|
| 37 | - $extendedPdo->exec(" |
|
| 36 | + ); |
|
| 37 | + $extendedPdo->exec(" |
|
| 38 | 38 | CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`series_post` ( |
| 39 | 39 | `series` integer NOT NULL, |
| 40 | 40 | `post` integer NOT NULL, |
| 41 | 41 | `order` integer(1) NOT NULL |
| 42 | 42 | )" |
| 43 | - ); |
|
| 44 | - |
|
| 45 | - self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
|
| 46 | - return $extendedPdo; |
|
| 47 | - }); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function testIsInstanceOfSeriesRepository() |
|
| 51 | - { |
|
| 52 | - $repository = new MysqlSeriesRepository(self::$connection); |
|
| 53 | - |
|
| 54 | - $this->assertInstanceOf( |
|
| 55 | - 'Jacobemerick\Web\Domain\Blog\Series\MysqlSeriesRepository', |
|
| 56 | - $repository |
|
| 57 | - ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public function testImplementsSeriesInterface() |
|
| 61 | - { |
|
| 62 | - $repository = new MysqlSeriesRepository(self::$connection); |
|
| 63 | - |
|
| 64 | - $this->assertInstanceOf( |
|
| 65 | - 'Jacobemerick\Web\Domain\Blog\Series\SeriesRepositoryInterface', |
|
| 66 | - $repository |
|
| 67 | - ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - public function testConstructSetsConnections() |
|
| 71 | - { |
|
| 72 | - $respository = new MysqlSeriesRepository(self::$connection); |
|
| 73 | - |
|
| 74 | - $this->assertAttributeSame( |
|
| 75 | - self::$connection, |
|
| 76 | - 'connections', |
|
| 77 | - $respository |
|
| 78 | - ); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function testGetSeriesForPost() |
|
| 82 | - { |
|
| 83 | - $testPostData = [ |
|
| 84 | - [ |
|
| 85 | - 'id' => rand(1, 100), |
|
| 86 | - 'title' => 'test one', |
|
| 87 | - 'category' => 'test category', |
|
| 88 | - 'path' => 'test-one', |
|
| 89 | - 'display' => 1, |
|
| 90 | - ], |
|
| 91 | - [ |
|
| 92 | - 'id' => rand(101, 200), |
|
| 93 | - 'title' => 'test two', |
|
| 94 | - 'category' => 'test category', |
|
| 95 | - 'path' => 'test-two', |
|
| 96 | - 'display' => 1, |
|
| 97 | - ], |
|
| 98 | - ]; |
|
| 99 | - |
|
| 100 | - $testSeriesData = [ |
|
| 101 | - [ |
|
| 102 | - 'id' => rand(1, 100), |
|
| 103 | - 'title' => 'test one', |
|
| 104 | - 'description' => 'test description', |
|
| 105 | - ], |
|
| 106 | - ]; |
|
| 107 | - |
|
| 108 | - $testSeriesPostData = []; |
|
| 109 | - foreach ($testPostData as $testPostDataRow) { |
|
| 110 | - array_push($testSeriesPostData, [ |
|
| 111 | - 'series' => reset($testSeriesData)['id'], |
|
| 112 | - 'post' => $testPostDataRow['id'], |
|
| 113 | - ]); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - array_walk($testPostData, [$this, 'insertPostData']); |
|
| 117 | - array_walk($testSeriesData, [$this, 'insertSeriesData']); |
|
| 118 | - array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']); |
|
| 119 | - |
|
| 120 | - $repository = new MysqlSeriesRepository(self::$connection); |
|
| 121 | - $data = $repository->getSeriesForPost(reset($testPostData)['id']); |
|
| 122 | - |
|
| 123 | - $this->assertNotFalse($data); |
|
| 124 | - $this->assertInternalType('array', $data); |
|
| 125 | - foreach ($testPostData as $key => $testPostRow) { |
|
| 126 | - $this->assertInternalType('array', $data[$key]); |
|
| 127 | - $this->assertArrayHasKey('series_title', $data[$key]); |
|
| 128 | - $this->assertEquals(reset($testSeriesData)['title'], $data[$key]['series_title']); |
|
| 129 | - $this->assertArrayHasKey('series_description', $data[$key]); |
|
| 130 | - $this->assertEquals(reset($testSeriesData)['description'], $data[$key]['series_description']); |
|
| 131 | - $this->assertArrayHasKey('post', $data[$key]); |
|
| 132 | - $this->assertEquals($testPostRow['id'], $data[$key]['post']); |
|
| 133 | - $this->assertArrayHasKey('title', $data[$key]); |
|
| 134 | - $this->assertEquals($testPostRow['title'], $data[$key]['title']); |
|
| 135 | - $this->assertArrayHasKey('category', $data[$key]); |
|
| 136 | - $this->assertEquals($testPostRow['category'], $data[$key]['category']); |
|
| 137 | - $this->assertArrayHasKey('path', $data[$key]); |
|
| 138 | - $this->assertEquals($testPostRow['path'], $data[$key]['path']); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - public function testGetSeriesForPostOrder() |
|
| 143 | - { |
|
| 144 | - $testPostData = [ |
|
| 145 | - [ |
|
| 146 | - 'id' => rand(1, 100), |
|
| 147 | - 'display' => 1, |
|
| 148 | - ], |
|
| 149 | - [ |
|
| 150 | - 'id' => rand(101, 200), |
|
| 151 | - 'display' => 1, |
|
| 152 | - ], |
|
| 153 | - [ |
|
| 154 | - 'id' => rand(201, 300), |
|
| 155 | - 'display' => 1, |
|
| 156 | - ], |
|
| 157 | - ]; |
|
| 158 | - |
|
| 159 | - $testSeriesData = [ |
|
| 160 | - [ |
|
| 161 | - 'id' => rand(1, 100), |
|
| 162 | - ], |
|
| 163 | - ]; |
|
| 164 | - |
|
| 165 | - $order = [1, 2, 3]; |
|
| 166 | - shuffle($order); |
|
| 167 | - $testSeriesPostData = []; |
|
| 168 | - foreach ($testPostData as $key => $testPostDataRow) { |
|
| 169 | - array_push($testSeriesPostData, [ |
|
| 170 | - 'series' => reset($testSeriesData)['id'], |
|
| 171 | - 'post' => $testPostDataRow['id'], |
|
| 172 | - 'order' => $order[$key], |
|
| 173 | - ]); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - array_walk($testPostData, [$this, 'insertPostData']); |
|
| 177 | - array_walk($testSeriesData, [$this, 'insertSeriesData']); |
|
| 178 | - array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']); |
|
| 179 | - |
|
| 180 | - $repository = new MysqlSeriesRepository(self::$connection); |
|
| 181 | - $data = $repository->getSeriesForPost(reset($testPostData)['id']); |
|
| 182 | - |
|
| 183 | - usort($testPostData, function ($rowA, $rowB) use ($testSeriesPostData) { |
|
| 184 | - $seriesA = array_filter($testSeriesPostData, function ($row) use ($rowA) { |
|
| 185 | - return ($rowA['id'] == $row['post']); |
|
| 186 | - }); |
|
| 187 | - $seriesB = array_filter($testSeriesPostData, function ($row) use ($rowB) { |
|
| 188 | - return ($rowB['id'] == $row['post']); |
|
| 189 | - }); |
|
| 190 | - |
|
| 191 | - return (reset($seriesA)['order'] > reset($seriesB)['order']); |
|
| 192 | - }); |
|
| 193 | - |
|
| 194 | - $this->assertNotFalse($data); |
|
| 195 | - $this->assertInternalType('array', $data); |
|
| 196 | - foreach ($testPostData as $key => $testPostRow) { |
|
| 197 | - $this->assertInternalType('array', $data[$key]); |
|
| 198 | - $this->assertArrayHasKey('post', $data[$key]); |
|
| 199 | - $this->assertEquals($testPostRow['id'], $data[$key]['post']); |
|
| 200 | - } |
|
| 201 | - } |
|
| 43 | + ); |
|
| 44 | + |
|
| 45 | + self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
|
| 46 | + return $extendedPdo; |
|
| 47 | + }); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function testIsInstanceOfSeriesRepository() |
|
| 51 | + { |
|
| 52 | + $repository = new MysqlSeriesRepository(self::$connection); |
|
| 53 | + |
|
| 54 | + $this->assertInstanceOf( |
|
| 55 | + 'Jacobemerick\Web\Domain\Blog\Series\MysqlSeriesRepository', |
|
| 56 | + $repository |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public function testImplementsSeriesInterface() |
|
| 61 | + { |
|
| 62 | + $repository = new MysqlSeriesRepository(self::$connection); |
|
| 63 | + |
|
| 64 | + $this->assertInstanceOf( |
|
| 65 | + 'Jacobemerick\Web\Domain\Blog\Series\SeriesRepositoryInterface', |
|
| 66 | + $repository |
|
| 67 | + ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + public function testConstructSetsConnections() |
|
| 71 | + { |
|
| 72 | + $respository = new MysqlSeriesRepository(self::$connection); |
|
| 73 | + |
|
| 74 | + $this->assertAttributeSame( |
|
| 75 | + self::$connection, |
|
| 76 | + 'connections', |
|
| 77 | + $respository |
|
| 78 | + ); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function testGetSeriesForPost() |
|
| 82 | + { |
|
| 83 | + $testPostData = [ |
|
| 84 | + [ |
|
| 85 | + 'id' => rand(1, 100), |
|
| 86 | + 'title' => 'test one', |
|
| 87 | + 'category' => 'test category', |
|
| 88 | + 'path' => 'test-one', |
|
| 89 | + 'display' => 1, |
|
| 90 | + ], |
|
| 91 | + [ |
|
| 92 | + 'id' => rand(101, 200), |
|
| 93 | + 'title' => 'test two', |
|
| 94 | + 'category' => 'test category', |
|
| 95 | + 'path' => 'test-two', |
|
| 96 | + 'display' => 1, |
|
| 97 | + ], |
|
| 98 | + ]; |
|
| 99 | + |
|
| 100 | + $testSeriesData = [ |
|
| 101 | + [ |
|
| 102 | + 'id' => rand(1, 100), |
|
| 103 | + 'title' => 'test one', |
|
| 104 | + 'description' => 'test description', |
|
| 105 | + ], |
|
| 106 | + ]; |
|
| 107 | + |
|
| 108 | + $testSeriesPostData = []; |
|
| 109 | + foreach ($testPostData as $testPostDataRow) { |
|
| 110 | + array_push($testSeriesPostData, [ |
|
| 111 | + 'series' => reset($testSeriesData)['id'], |
|
| 112 | + 'post' => $testPostDataRow['id'], |
|
| 113 | + ]); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + array_walk($testPostData, [$this, 'insertPostData']); |
|
| 117 | + array_walk($testSeriesData, [$this, 'insertSeriesData']); |
|
| 118 | + array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']); |
|
| 119 | + |
|
| 120 | + $repository = new MysqlSeriesRepository(self::$connection); |
|
| 121 | + $data = $repository->getSeriesForPost(reset($testPostData)['id']); |
|
| 122 | + |
|
| 123 | + $this->assertNotFalse($data); |
|
| 124 | + $this->assertInternalType('array', $data); |
|
| 125 | + foreach ($testPostData as $key => $testPostRow) { |
|
| 126 | + $this->assertInternalType('array', $data[$key]); |
|
| 127 | + $this->assertArrayHasKey('series_title', $data[$key]); |
|
| 128 | + $this->assertEquals(reset($testSeriesData)['title'], $data[$key]['series_title']); |
|
| 129 | + $this->assertArrayHasKey('series_description', $data[$key]); |
|
| 130 | + $this->assertEquals(reset($testSeriesData)['description'], $data[$key]['series_description']); |
|
| 131 | + $this->assertArrayHasKey('post', $data[$key]); |
|
| 132 | + $this->assertEquals($testPostRow['id'], $data[$key]['post']); |
|
| 133 | + $this->assertArrayHasKey('title', $data[$key]); |
|
| 134 | + $this->assertEquals($testPostRow['title'], $data[$key]['title']); |
|
| 135 | + $this->assertArrayHasKey('category', $data[$key]); |
|
| 136 | + $this->assertEquals($testPostRow['category'], $data[$key]['category']); |
|
| 137 | + $this->assertArrayHasKey('path', $data[$key]); |
|
| 138 | + $this->assertEquals($testPostRow['path'], $data[$key]['path']); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + public function testGetSeriesForPostOrder() |
|
| 143 | + { |
|
| 144 | + $testPostData = [ |
|
| 145 | + [ |
|
| 146 | + 'id' => rand(1, 100), |
|
| 147 | + 'display' => 1, |
|
| 148 | + ], |
|
| 149 | + [ |
|
| 150 | + 'id' => rand(101, 200), |
|
| 151 | + 'display' => 1, |
|
| 152 | + ], |
|
| 153 | + [ |
|
| 154 | + 'id' => rand(201, 300), |
|
| 155 | + 'display' => 1, |
|
| 156 | + ], |
|
| 157 | + ]; |
|
| 158 | + |
|
| 159 | + $testSeriesData = [ |
|
| 160 | + [ |
|
| 161 | + 'id' => rand(1, 100), |
|
| 162 | + ], |
|
| 163 | + ]; |
|
| 164 | + |
|
| 165 | + $order = [1, 2, 3]; |
|
| 166 | + shuffle($order); |
|
| 167 | + $testSeriesPostData = []; |
|
| 168 | + foreach ($testPostData as $key => $testPostDataRow) { |
|
| 169 | + array_push($testSeriesPostData, [ |
|
| 170 | + 'series' => reset($testSeriesData)['id'], |
|
| 171 | + 'post' => $testPostDataRow['id'], |
|
| 172 | + 'order' => $order[$key], |
|
| 173 | + ]); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + array_walk($testPostData, [$this, 'insertPostData']); |
|
| 177 | + array_walk($testSeriesData, [$this, 'insertSeriesData']); |
|
| 178 | + array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']); |
|
| 179 | + |
|
| 180 | + $repository = new MysqlSeriesRepository(self::$connection); |
|
| 181 | + $data = $repository->getSeriesForPost(reset($testPostData)['id']); |
|
| 182 | + |
|
| 183 | + usort($testPostData, function ($rowA, $rowB) use ($testSeriesPostData) { |
|
| 184 | + $seriesA = array_filter($testSeriesPostData, function ($row) use ($rowA) { |
|
| 185 | + return ($rowA['id'] == $row['post']); |
|
| 186 | + }); |
|
| 187 | + $seriesB = array_filter($testSeriesPostData, function ($row) use ($rowB) { |
|
| 188 | + return ($rowB['id'] == $row['post']); |
|
| 189 | + }); |
|
| 190 | + |
|
| 191 | + return (reset($seriesA)['order'] > reset($seriesB)['order']); |
|
| 192 | + }); |
|
| 193 | + |
|
| 194 | + $this->assertNotFalse($data); |
|
| 195 | + $this->assertInternalType('array', $data); |
|
| 196 | + foreach ($testPostData as $key => $testPostRow) { |
|
| 197 | + $this->assertInternalType('array', $data[$key]); |
|
| 198 | + $this->assertArrayHasKey('post', $data[$key]); |
|
| 199 | + $this->assertEquals($testPostRow['id'], $data[$key]['post']); |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - public function testGetSeriesForPostSeriesFilter() |
|
| 204 | - { |
|
| 205 | - $testPostAData = [ |
|
| 206 | - [ |
|
| 207 | - 'id' => rand(1, 100), |
|
| 208 | - 'display' => 1, |
|
| 209 | - ], |
|
| 210 | - [ |
|
| 211 | - 'id' => rand(101, 200), |
|
| 212 | - 'display' => 1, |
|
| 213 | - ], |
|
| 214 | - ]; |
|
| 215 | - |
|
| 216 | - $testPostBData = [ |
|
| 217 | - [ |
|
| 218 | - 'id' => rand(501, 600), |
|
| 219 | - 'display' => 1, |
|
| 220 | - ], |
|
| 221 | - [ |
|
| 222 | - 'id' => rand(601, 700), |
|
| 223 | - 'display' => 1, |
|
| 224 | - ], |
|
| 225 | - ]; |
|
| 226 | - |
|
| 227 | - $testSeriesData = [ |
|
| 228 | - [ |
|
| 229 | - 'id' => rand(1, 100), |
|
| 230 | - ], |
|
| 231 | - [ |
|
| 232 | - 'id' => rand(101, 200), |
|
| 233 | - ], |
|
| 234 | - ]; |
|
| 235 | - |
|
| 236 | - $testSeriesPostData = []; |
|
| 237 | - foreach ($testPostAData as $testPostDataRow) { |
|
| 238 | - array_push($testSeriesPostData, [ |
|
| 239 | - 'series' => $testSeriesData[0]['id'], |
|
| 240 | - 'post' => $testPostDataRow['id'], |
|
| 241 | - ]); |
|
| 242 | - } |
|
| 243 | - foreach ($testPostBData as $testPostDataRow) { |
|
| 244 | - array_push($testSeriesPostData, [ |
|
| 245 | - 'series' => $testSeriesData[1]['id'], |
|
| 246 | - 'post' => $testPostDataRow['id'], |
|
| 247 | - ]); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - $testPostData = array_merge($testPostAData, $testPostBData); |
|
| 251 | - array_walk($testPostData, [$this, 'insertPostData']); |
|
| 252 | - array_walk($testSeriesData, [$this, 'insertSeriesData']); |
|
| 253 | - array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']); |
|
| 254 | - |
|
| 255 | - $repository = new MysqlSeriesRepository(self::$connection); |
|
| 256 | - $data = $repository->getSeriesForPost(reset($testPostAData)['id']); |
|
| 257 | - |
|
| 258 | - $this->assertNotFalse($data); |
|
| 259 | - $this->assertInternalType('array', $data); |
|
| 260 | - |
|
| 261 | - $testPosts = array_column($testPostAData, 'id'); |
|
| 262 | - $dataPosts = array_column($data, 'post'); |
|
| 263 | - |
|
| 264 | - $this->assertEmpty(array_merge( |
|
| 265 | - array_diff($testPosts, $dataPosts), |
|
| 266 | - array_diff($dataPosts, $testPosts) |
|
| 267 | - )); |
|
| 268 | - } |
|
| 203 | + public function testGetSeriesForPostSeriesFilter() |
|
| 204 | + { |
|
| 205 | + $testPostAData = [ |
|
| 206 | + [ |
|
| 207 | + 'id' => rand(1, 100), |
|
| 208 | + 'display' => 1, |
|
| 209 | + ], |
|
| 210 | + [ |
|
| 211 | + 'id' => rand(101, 200), |
|
| 212 | + 'display' => 1, |
|
| 213 | + ], |
|
| 214 | + ]; |
|
| 215 | + |
|
| 216 | + $testPostBData = [ |
|
| 217 | + [ |
|
| 218 | + 'id' => rand(501, 600), |
|
| 219 | + 'display' => 1, |
|
| 220 | + ], |
|
| 221 | + [ |
|
| 222 | + 'id' => rand(601, 700), |
|
| 223 | + 'display' => 1, |
|
| 224 | + ], |
|
| 225 | + ]; |
|
| 226 | + |
|
| 227 | + $testSeriesData = [ |
|
| 228 | + [ |
|
| 229 | + 'id' => rand(1, 100), |
|
| 230 | + ], |
|
| 231 | + [ |
|
| 232 | + 'id' => rand(101, 200), |
|
| 233 | + ], |
|
| 234 | + ]; |
|
| 235 | + |
|
| 236 | + $testSeriesPostData = []; |
|
| 237 | + foreach ($testPostAData as $testPostDataRow) { |
|
| 238 | + array_push($testSeriesPostData, [ |
|
| 239 | + 'series' => $testSeriesData[0]['id'], |
|
| 240 | + 'post' => $testPostDataRow['id'], |
|
| 241 | + ]); |
|
| 242 | + } |
|
| 243 | + foreach ($testPostBData as $testPostDataRow) { |
|
| 244 | + array_push($testSeriesPostData, [ |
|
| 245 | + 'series' => $testSeriesData[1]['id'], |
|
| 246 | + 'post' => $testPostDataRow['id'], |
|
| 247 | + ]); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + $testPostData = array_merge($testPostAData, $testPostBData); |
|
| 251 | + array_walk($testPostData, [$this, 'insertPostData']); |
|
| 252 | + array_walk($testSeriesData, [$this, 'insertSeriesData']); |
|
| 253 | + array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']); |
|
| 254 | + |
|
| 255 | + $repository = new MysqlSeriesRepository(self::$connection); |
|
| 256 | + $data = $repository->getSeriesForPost(reset($testPostAData)['id']); |
|
| 257 | + |
|
| 258 | + $this->assertNotFalse($data); |
|
| 259 | + $this->assertInternalType('array', $data); |
|
| 260 | + |
|
| 261 | + $testPosts = array_column($testPostAData, 'id'); |
|
| 262 | + $dataPosts = array_column($data, 'post'); |
|
| 263 | + |
|
| 264 | + $this->assertEmpty(array_merge( |
|
| 265 | + array_diff($testPosts, $dataPosts), |
|
| 266 | + array_diff($dataPosts, $testPosts) |
|
| 267 | + )); |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - public function testGetSeriesForPostInactive() |
|
| 271 | - { |
|
| 272 | - $testPostData = [ |
|
| 273 | - [ |
|
| 274 | - 'id' => rand(1, 100), |
|
| 275 | - 'display' => 1, |
|
| 276 | - ], |
|
| 277 | - [ |
|
| 278 | - 'id' => rand(101, 200), |
|
| 279 | - 'display' => 0, |
|
| 280 | - ], |
|
| 281 | - [ |
|
| 282 | - 'id' => rand(201, 300), |
|
| 283 | - 'display' => 0, |
|
| 284 | - ], |
|
| 285 | - ]; |
|
| 286 | - |
|
| 287 | - $testSeriesData = [ |
|
| 288 | - [ |
|
| 289 | - 'id' => rand(1, 100), |
|
| 290 | - ], |
|
| 291 | - ]; |
|
| 292 | - |
|
| 293 | - $testSeriesPostData = []; |
|
| 294 | - foreach ($testPostData as $testPostDataRow) { |
|
| 295 | - array_push($testSeriesPostData, [ |
|
| 296 | - 'series' => reset($testSeriesData)['id'], |
|
| 297 | - 'post' => $testPostDataRow['id'], |
|
| 298 | - ]); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - array_walk($testPostData, [$this, 'insertPostData']); |
|
| 302 | - array_walk($testSeriesData, [$this, 'insertSeriesData']); |
|
| 303 | - array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']); |
|
| 304 | - |
|
| 305 | - $repository = new MysqlSeriesRepository(self::$connection); |
|
| 306 | - $data = $repository->getSeriesForPost(reset($testPostData)['id']); |
|
| 307 | - |
|
| 308 | - $this->assertNotFalse($data); |
|
| 309 | - $this->assertInternalType('array', $data); |
|
| 310 | - |
|
| 311 | - $testPostData = array_filter($testPostData, function ($row) { |
|
| 312 | - return ($row['display'] == 1); |
|
| 313 | - }); |
|
| 314 | - $testPosts = array_column($testPostData, 'id'); |
|
| 315 | - $dataPosts = array_column($data, 'post'); |
|
| 316 | - |
|
| 317 | - $this->assertEmpty(array_merge( |
|
| 318 | - array_diff($testPosts, $dataPosts), |
|
| 319 | - array_diff($dataPosts, $testPosts) |
|
| 320 | - )); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - public function testGetSeriesForPostFailure() |
|
| 324 | - { |
|
| 325 | - $repository = new MysqlSeriesRepository(self::$connection); |
|
| 326 | - $data = $repository->getSeriesForPost(rand(1, 100)); |
|
| 327 | - |
|
| 328 | - $this->assertEmpty($data); |
|
| 329 | - $this->assertInternalType('array', $data); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - protected function insertPostData(array $data) |
|
| 333 | - { |
|
| 334 | - $defaultData = [ |
|
| 335 | - 'id' => null, |
|
| 336 | - 'title' => '', |
|
| 337 | - 'path' => '', |
|
| 338 | - 'category' => '', |
|
| 339 | - 'date' => '', |
|
| 340 | - 'body' => '', |
|
| 341 | - 'display' => 0, |
|
| 342 | - ]; |
|
| 343 | - |
|
| 344 | - $data = array_merge($defaultData, $data); |
|
| 345 | - |
|
| 346 | - return self::$connection->getDefault()->perform(" |
|
| 270 | + public function testGetSeriesForPostInactive() |
|
| 271 | + { |
|
| 272 | + $testPostData = [ |
|
| 273 | + [ |
|
| 274 | + 'id' => rand(1, 100), |
|
| 275 | + 'display' => 1, |
|
| 276 | + ], |
|
| 277 | + [ |
|
| 278 | + 'id' => rand(101, 200), |
|
| 279 | + 'display' => 0, |
|
| 280 | + ], |
|
| 281 | + [ |
|
| 282 | + 'id' => rand(201, 300), |
|
| 283 | + 'display' => 0, |
|
| 284 | + ], |
|
| 285 | + ]; |
|
| 286 | + |
|
| 287 | + $testSeriesData = [ |
|
| 288 | + [ |
|
| 289 | + 'id' => rand(1, 100), |
|
| 290 | + ], |
|
| 291 | + ]; |
|
| 292 | + |
|
| 293 | + $testSeriesPostData = []; |
|
| 294 | + foreach ($testPostData as $testPostDataRow) { |
|
| 295 | + array_push($testSeriesPostData, [ |
|
| 296 | + 'series' => reset($testSeriesData)['id'], |
|
| 297 | + 'post' => $testPostDataRow['id'], |
|
| 298 | + ]); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + array_walk($testPostData, [$this, 'insertPostData']); |
|
| 302 | + array_walk($testSeriesData, [$this, 'insertSeriesData']); |
|
| 303 | + array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']); |
|
| 304 | + |
|
| 305 | + $repository = new MysqlSeriesRepository(self::$connection); |
|
| 306 | + $data = $repository->getSeriesForPost(reset($testPostData)['id']); |
|
| 307 | + |
|
| 308 | + $this->assertNotFalse($data); |
|
| 309 | + $this->assertInternalType('array', $data); |
|
| 310 | + |
|
| 311 | + $testPostData = array_filter($testPostData, function ($row) { |
|
| 312 | + return ($row['display'] == 1); |
|
| 313 | + }); |
|
| 314 | + $testPosts = array_column($testPostData, 'id'); |
|
| 315 | + $dataPosts = array_column($data, 'post'); |
|
| 316 | + |
|
| 317 | + $this->assertEmpty(array_merge( |
|
| 318 | + array_diff($testPosts, $dataPosts), |
|
| 319 | + array_diff($dataPosts, $testPosts) |
|
| 320 | + )); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + public function testGetSeriesForPostFailure() |
|
| 324 | + { |
|
| 325 | + $repository = new MysqlSeriesRepository(self::$connection); |
|
| 326 | + $data = $repository->getSeriesForPost(rand(1, 100)); |
|
| 327 | + |
|
| 328 | + $this->assertEmpty($data); |
|
| 329 | + $this->assertInternalType('array', $data); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + protected function insertPostData(array $data) |
|
| 333 | + { |
|
| 334 | + $defaultData = [ |
|
| 335 | + 'id' => null, |
|
| 336 | + 'title' => '', |
|
| 337 | + 'path' => '', |
|
| 338 | + 'category' => '', |
|
| 339 | + 'date' => '', |
|
| 340 | + 'body' => '', |
|
| 341 | + 'display' => 0, |
|
| 342 | + ]; |
|
| 343 | + |
|
| 344 | + $data = array_merge($defaultData, $data); |
|
| 345 | + |
|
| 346 | + return self::$connection->getDefault()->perform(" |
|
| 347 | 347 | INSERT INTO `jpemeric_blog`.`post` |
| 348 | 348 | (id, title, path, category, date, body, display) |
| 349 | 349 | VALUES |
| 350 | 350 | (:id, :title, :path, :category, :date, :body, :display)", |
| 351 | - $data |
|
| 352 | - ); |
|
| 353 | - } |
|
| 351 | + $data |
|
| 352 | + ); |
|
| 353 | + } |
|
| 354 | 354 | |
| 355 | - protected function insertSeriesData(array $data) |
|
| 356 | - { |
|
| 357 | - $defaultData = [ |
|
| 358 | - 'id' => null, |
|
| 359 | - 'title' => '', |
|
| 360 | - 'description' => '', |
|
| 361 | - ]; |
|
| 355 | + protected function insertSeriesData(array $data) |
|
| 356 | + { |
|
| 357 | + $defaultData = [ |
|
| 358 | + 'id' => null, |
|
| 359 | + 'title' => '', |
|
| 360 | + 'description' => '', |
|
| 361 | + ]; |
|
| 362 | 362 | |
| 363 | - $data = array_merge($defaultData, $data); |
|
| 363 | + $data = array_merge($defaultData, $data); |
|
| 364 | 364 | |
| 365 | - return self::$connection->getDefault()->perform(" |
|
| 365 | + return self::$connection->getDefault()->perform(" |
|
| 366 | 366 | INSERT INTO `jpemeric_blog`.`series` |
| 367 | 367 | (id, title, description) |
| 368 | 368 | VALUES |
| 369 | 369 | (:id, :title, :description)", |
| 370 | - $data |
|
| 371 | - ); |
|
| 372 | - } |
|
| 370 | + $data |
|
| 371 | + ); |
|
| 372 | + } |
|
| 373 | 373 | |
| 374 | - protected function insertSeriesPostData(array $data) |
|
| 375 | - { |
|
| 376 | - $defaultData = [ |
|
| 377 | - 'series' => '', |
|
| 378 | - 'post' => '', |
|
| 379 | - 'order' => 0, |
|
| 380 | - ]; |
|
| 374 | + protected function insertSeriesPostData(array $data) |
|
| 375 | + { |
|
| 376 | + $defaultData = [ |
|
| 377 | + 'series' => '', |
|
| 378 | + 'post' => '', |
|
| 379 | + 'order' => 0, |
|
| 380 | + ]; |
|
| 381 | 381 | |
| 382 | - $data = array_merge($defaultData, $data); |
|
| 382 | + $data = array_merge($defaultData, $data); |
|
| 383 | 383 | |
| 384 | - return self::$connection->getDefault()->perform(" |
|
| 384 | + return self::$connection->getDefault()->perform(" |
|
| 385 | 385 | INSERT INTO `jpemeric_blog`.`series_post` |
| 386 | 386 | (series, post, `order`) |
| 387 | 387 | VALUES |
| 388 | 388 | (:series, :post, :order)", |
| 389 | - $data |
|
| 390 | - ); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - protected function tearDown() |
|
| 394 | - { |
|
| 395 | - self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`post`"); |
|
| 396 | - self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`series`"); |
|
| 397 | - self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`series_post`"); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - public static function tearDownAfterClass() |
|
| 401 | - { |
|
| 402 | - self::$connection->getDefault()->disconnect(); |
|
| 403 | - unlink('jpemeric_blog.db'); |
|
| 404 | - } |
|
| 389 | + $data |
|
| 390 | + ); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + protected function tearDown() |
|
| 394 | + { |
|
| 395 | + self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`post`"); |
|
| 396 | + self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`series`"); |
|
| 397 | + self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`series_post`"); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + public static function tearDownAfterClass() |
|
| 401 | + { |
|
| 402 | + self::$connection->getDefault()->disconnect(); |
|
| 403 | + unlink('jpemeric_blog.db'); |
|
| 404 | + } |
|
| 405 | 405 | } |
@@ -9,14 +9,14 @@ discard block |
||
| 9 | 9 | class MysqlTagRepositoryTest extends PHPUnit_Framework_TestCase |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - protected static $connection; |
|
| 12 | + protected static $connection; |
|
| 13 | 13 | |
| 14 | - public static function setUpBeforeClass() |
|
| 15 | - { |
|
| 16 | - $extendedPdo = new ExtendedPdo('sqlite::memory:'); |
|
| 17 | - $extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`"); |
|
| 14 | + public static function setUpBeforeClass() |
|
| 15 | + { |
|
| 16 | + $extendedPdo = new ExtendedPdo('sqlite::memory:'); |
|
| 17 | + $extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`"); |
|
| 18 | 18 | |
| 19 | - $extendedPdo->exec(" |
|
| 19 | + $extendedPdo->exec(" |
|
| 20 | 20 | CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`post` ( |
| 21 | 21 | `id` integer PRIMARY KEY AUTOINCREMENT, |
| 22 | 22 | `title` varchar(60) NOT NULL, |
@@ -26,497 +26,497 @@ discard block |
||
| 26 | 26 | `body` text, |
| 27 | 27 | `display` integer(1) NOT NULL |
| 28 | 28 | )" |
| 29 | - ); |
|
| 30 | - $extendedPdo->exec(" |
|
| 29 | + ); |
|
| 30 | + $extendedPdo->exec(" |
|
| 31 | 31 | CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`ptlink` ( |
| 32 | 32 | `post_id` integer NOT NULL, |
| 33 | 33 | `tag_id` integer NOT NULL |
| 34 | 34 | )" |
| 35 | - ); |
|
| 36 | - $extendedPdo->exec(" |
|
| 35 | + ); |
|
| 36 | + $extendedPdo->exec(" |
|
| 37 | 37 | CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`tag` ( |
| 38 | 38 | `id` integer PRIMARY KEY AUTOINCREMENT, |
| 39 | 39 | `tag` varchar(25) NOT NULL |
| 40 | 40 | )" |
| 41 | - ); |
|
| 42 | - |
|
| 43 | - self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
|
| 44 | - return $extendedPdo; |
|
| 45 | - }); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function testIsInstanceOfTagRepository() |
|
| 49 | - { |
|
| 50 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 51 | - |
|
| 52 | - $this->assertInstanceOf( |
|
| 53 | - 'Jacobemerick\Web\Domain\Blog\Tag\MysqlTagRepository', |
|
| 54 | - $repository |
|
| 55 | - ); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - public function testImplementsTagInterface() |
|
| 59 | - { |
|
| 60 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 61 | - |
|
| 62 | - $this->assertInstanceOf( |
|
| 63 | - 'Jacobemerick\Web\Domain\Blog\Tag\TagRepositoryInterface', |
|
| 64 | - $repository |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public function testConstructSetsConnections() |
|
| 69 | - { |
|
| 70 | - $respository = new MysqlTagRepository(self::$connection); |
|
| 71 | - |
|
| 72 | - $this->assertAttributeSame( |
|
| 73 | - self::$connection, |
|
| 74 | - 'connections', |
|
| 75 | - $respository |
|
| 76 | - ); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - public function testFindTagByTitle() |
|
| 80 | - { |
|
| 81 | - $testData = [ |
|
| 82 | - [ |
|
| 83 | - 'id' => rand(1, 100), |
|
| 84 | - 'tag' => 'test one', |
|
| 85 | - ], |
|
| 86 | - [ |
|
| 87 | - 'id' => rand(101, 200), |
|
| 88 | - 'tag' => 'test two', |
|
| 89 | - ], |
|
| 90 | - ]; |
|
| 91 | - |
|
| 92 | - array_walk($testData, [$this, 'insertTagData']); |
|
| 93 | - |
|
| 94 | - shuffle($testData); |
|
| 95 | - |
|
| 96 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 97 | - $data = $repository->findTagByTitle(reset($testData)['tag']); |
|
| 98 | - |
|
| 99 | - $this->assertNotFalse($data); |
|
| 100 | - $this->assertInternalType('array', $data); |
|
| 101 | - $this->assertArrayHasKey('id', $data); |
|
| 102 | - $this->assertEquals(reset($testData)['id'], $data['id']); |
|
| 103 | - $this->assertArrayHasKey('tag', $data); |
|
| 104 | - $this->assertEquals(reset($testData)['tag'], $data['tag']); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - public function testFindTagByTitleFailure() |
|
| 108 | - { |
|
| 109 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 110 | - $data = $repository->findTagByTitle('empty tag'); |
|
| 111 | - |
|
| 112 | - $this->assertFalse($data); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - public function testGetAllTags() |
|
| 116 | - { |
|
| 117 | - $testData = [ |
|
| 118 | - [ |
|
| 119 | - 'id' => rand(1, 100), |
|
| 120 | - 'tag' => 'test one', |
|
| 121 | - ], |
|
| 122 | - [ |
|
| 123 | - 'id' => rand(101, 200), |
|
| 124 | - 'tag' => 'test two', |
|
| 125 | - ], |
|
| 126 | - ]; |
|
| 127 | - |
|
| 128 | - array_walk($testData, [$this, 'insertTagData']); |
|
| 129 | - |
|
| 130 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 131 | - $data = $repository->getAllTags(); |
|
| 132 | - |
|
| 133 | - $this->assertNotFalse($data); |
|
| 134 | - $this->assertInternalType('array', $data); |
|
| 135 | - $this->assertCount(count($testData), $data); |
|
| 136 | - foreach ($testData as $key => $testDataRow) { |
|
| 137 | - $this->assertArrayHasKey('id', $data[$key]); |
|
| 138 | - $this->assertEquals($testDataRow['id'], $data[$key]['id']); |
|
| 139 | - $this->assertArrayHasKey('tag', $data[$key]); |
|
| 140 | - $this->assertEquals($testDataRow['tag'], $data[$key]['tag']); |
|
| 141 | - } |
|
| 142 | - } |
|
| 41 | + ); |
|
| 42 | + |
|
| 43 | + self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
|
| 44 | + return $extendedPdo; |
|
| 45 | + }); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function testIsInstanceOfTagRepository() |
|
| 49 | + { |
|
| 50 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 51 | + |
|
| 52 | + $this->assertInstanceOf( |
|
| 53 | + 'Jacobemerick\Web\Domain\Blog\Tag\MysqlTagRepository', |
|
| 54 | + $repository |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + public function testImplementsTagInterface() |
|
| 59 | + { |
|
| 60 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 61 | + |
|
| 62 | + $this->assertInstanceOf( |
|
| 63 | + 'Jacobemerick\Web\Domain\Blog\Tag\TagRepositoryInterface', |
|
| 64 | + $repository |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public function testConstructSetsConnections() |
|
| 69 | + { |
|
| 70 | + $respository = new MysqlTagRepository(self::$connection); |
|
| 71 | + |
|
| 72 | + $this->assertAttributeSame( |
|
| 73 | + self::$connection, |
|
| 74 | + 'connections', |
|
| 75 | + $respository |
|
| 76 | + ); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + public function testFindTagByTitle() |
|
| 80 | + { |
|
| 81 | + $testData = [ |
|
| 82 | + [ |
|
| 83 | + 'id' => rand(1, 100), |
|
| 84 | + 'tag' => 'test one', |
|
| 85 | + ], |
|
| 86 | + [ |
|
| 87 | + 'id' => rand(101, 200), |
|
| 88 | + 'tag' => 'test two', |
|
| 89 | + ], |
|
| 90 | + ]; |
|
| 91 | + |
|
| 92 | + array_walk($testData, [$this, 'insertTagData']); |
|
| 93 | + |
|
| 94 | + shuffle($testData); |
|
| 95 | + |
|
| 96 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 97 | + $data = $repository->findTagByTitle(reset($testData)['tag']); |
|
| 98 | + |
|
| 99 | + $this->assertNotFalse($data); |
|
| 100 | + $this->assertInternalType('array', $data); |
|
| 101 | + $this->assertArrayHasKey('id', $data); |
|
| 102 | + $this->assertEquals(reset($testData)['id'], $data['id']); |
|
| 103 | + $this->assertArrayHasKey('tag', $data); |
|
| 104 | + $this->assertEquals(reset($testData)['tag'], $data['tag']); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + public function testFindTagByTitleFailure() |
|
| 108 | + { |
|
| 109 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 110 | + $data = $repository->findTagByTitle('empty tag'); |
|
| 111 | + |
|
| 112 | + $this->assertFalse($data); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + public function testGetAllTags() |
|
| 116 | + { |
|
| 117 | + $testData = [ |
|
| 118 | + [ |
|
| 119 | + 'id' => rand(1, 100), |
|
| 120 | + 'tag' => 'test one', |
|
| 121 | + ], |
|
| 122 | + [ |
|
| 123 | + 'id' => rand(101, 200), |
|
| 124 | + 'tag' => 'test two', |
|
| 125 | + ], |
|
| 126 | + ]; |
|
| 127 | + |
|
| 128 | + array_walk($testData, [$this, 'insertTagData']); |
|
| 129 | + |
|
| 130 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 131 | + $data = $repository->getAllTags(); |
|
| 132 | + |
|
| 133 | + $this->assertNotFalse($data); |
|
| 134 | + $this->assertInternalType('array', $data); |
|
| 135 | + $this->assertCount(count($testData), $data); |
|
| 136 | + foreach ($testData as $key => $testDataRow) { |
|
| 137 | + $this->assertArrayHasKey('id', $data[$key]); |
|
| 138 | + $this->assertEquals($testDataRow['id'], $data[$key]['id']); |
|
| 139 | + $this->assertArrayHasKey('tag', $data[$key]); |
|
| 140 | + $this->assertEquals($testDataRow['tag'], $data[$key]['tag']); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - public function testGetAllTagsFailure() |
|
| 145 | - { |
|
| 146 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 147 | - $data = $repository->getAllTags(); |
|
| 148 | - |
|
| 149 | - $this->assertEmpty($data); |
|
| 150 | - $this->assertInternalType('array', $data); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public function testGetTagCloud() |
|
| 154 | - { |
|
| 155 | - $testPostData = [ |
|
| 156 | - [ |
|
| 157 | - 'id' => rand(1, 100), |
|
| 158 | - 'display' => 1, |
|
| 159 | - ], |
|
| 160 | - [ |
|
| 161 | - 'id' => rand(101, 200), |
|
| 162 | - 'display' => 1, |
|
| 163 | - ], |
|
| 164 | - ]; |
|
| 165 | - |
|
| 166 | - $testTagData = [ |
|
| 167 | - [ |
|
| 168 | - 'id' => rand(1, 100), |
|
| 169 | - 'tag' => 'test one', |
|
| 170 | - ], |
|
| 171 | - [ |
|
| 172 | - 'id' => rand(101, 200), |
|
| 173 | - 'tag' => 'test two', |
|
| 174 | - ], |
|
| 175 | - [ |
|
| 176 | - 'id' => rand(201, 300), |
|
| 177 | - 'tag' => 'test three', |
|
| 178 | - ], |
|
| 179 | - ]; |
|
| 180 | - |
|
| 181 | - $testPTLinkData = []; |
|
| 182 | - foreach ($testPostData as $testPostRow) { |
|
| 183 | - $tempTagData = $testTagData; |
|
| 184 | - shuffle($tempTagData); |
|
| 185 | - for ($i = 0; $i < 2; $i++) { |
|
| 186 | - array_push($testPTLinkData, [ |
|
| 187 | - 'post_id' => $testPostRow['id'], |
|
| 188 | - 'tag_id' => $tempTagData[$i]['id'], |
|
| 189 | - ]); |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - array_walk($testPostData, [$this, 'insertPostData']); |
|
| 194 | - array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
|
| 195 | - array_walk($testTagData, [$this, 'insertTagData']); |
|
| 196 | - |
|
| 197 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 198 | - $data = $repository->getTagCloud(); |
|
| 199 | - |
|
| 200 | - $this->assertNotFalse($data); |
|
| 201 | - $this->assertInternalType('array', $data); |
|
| 202 | - |
|
| 203 | - $tagCountData = array_map(function ($row) use ($testTagData) { |
|
| 204 | - $tag = $row['tag_id']; |
|
| 205 | - $tag = array_filter($testTagData, function ($row) use ($tag) { |
|
| 206 | - return ($tag == $row['id']); |
|
| 207 | - }); |
|
| 208 | - $tag = current($tag)['tag']; |
|
| 209 | - return $tag; |
|
| 210 | - }, $testPTLinkData); |
|
| 211 | - |
|
| 212 | - $testCountData = []; |
|
| 213 | - foreach ($tagCountData as $tagCountRow) { |
|
| 214 | - $incremented = false; |
|
| 215 | - foreach ($testCountData as $key => $testCountRow) { |
|
| 216 | - if ($tagCountRow == $testCountRow['tag']) { |
|
| 217 | - $testCountData[$key]['count']++; |
|
| 218 | - $incremented = true; |
|
| 219 | - break; |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - if (!$incremented) { |
|
| 223 | - array_push($testCountData, [ |
|
| 224 | - 'count' => 1, |
|
| 225 | - 'tag' => $tagCountRow, |
|
| 226 | - ]); |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $this->assertCount(count($testCountData), $data); |
|
| 231 | - |
|
| 232 | - usort($testCountData, function ($rowA, $rowB) { |
|
| 233 | - return ($rowA['tag'] > $rowB['tag']); |
|
| 234 | - }); |
|
| 235 | - usort($data, function ($rowA, $rowB) { |
|
| 236 | - return ($rowA['tag'] > $rowB['tag']); |
|
| 237 | - }); |
|
| 238 | - |
|
| 239 | - foreach ($testCountData as $key => $testCountRow) { |
|
| 240 | - $this->assertArrayHasKey('count', $data[$key]); |
|
| 241 | - $this->assertEquals($testCountRow['count'], $data[$key]['count']); |
|
| 242 | - $this->assertArrayHasKey('tag', $data[$key]); |
|
| 243 | - $this->assertEquals($testCountRow['tag'], $data[$key]['tag']); |
|
| 244 | - } |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - public function testGetTagCloudInactive() |
|
| 248 | - { |
|
| 249 | - $testPostData = [ |
|
| 250 | - [ |
|
| 251 | - 'id' => rand(1, 100), |
|
| 252 | - 'display' => 1, |
|
| 253 | - ], |
|
| 254 | - [ |
|
| 255 | - 'id' => rand(101, 200), |
|
| 256 | - 'display' => 1, |
|
| 257 | - ], |
|
| 258 | - [ |
|
| 259 | - 'id' => rand(201, 300), |
|
| 260 | - 'display' => 0, |
|
| 261 | - ], |
|
| 262 | - ]; |
|
| 263 | - |
|
| 264 | - $testTagData = [ |
|
| 265 | - [ |
|
| 266 | - 'id' => rand(1, 100), |
|
| 267 | - 'tag' => 'test one', |
|
| 268 | - ], |
|
| 269 | - [ |
|
| 270 | - 'id' => rand(101, 200), |
|
| 271 | - 'tag' => 'test two', |
|
| 272 | - ], |
|
| 273 | - [ |
|
| 274 | - 'id' => rand(201, 300), |
|
| 275 | - 'tag' => 'test three', |
|
| 276 | - ], |
|
| 277 | - ]; |
|
| 278 | - |
|
| 279 | - $testPTLinkData = []; |
|
| 280 | - foreach ($testPostData as $testPostRow) { |
|
| 281 | - $tempTagData = $testTagData; |
|
| 282 | - shuffle($tempTagData); |
|
| 283 | - for ($i = 0; $i < 2; $i++) { |
|
| 284 | - array_push($testPTLinkData, [ |
|
| 285 | - 'post_id' => $testPostRow['id'], |
|
| 286 | - 'tag_id' => $tempTagData[$i]['id'], |
|
| 287 | - ]); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - array_walk($testPostData, [$this, 'insertPostData']); |
|
| 292 | - array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
|
| 293 | - array_walk($testTagData, [$this, 'insertTagData']); |
|
| 294 | - |
|
| 295 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 296 | - $data = $repository->getTagCloud(); |
|
| 297 | - |
|
| 298 | - $this->assertNotFalse($data); |
|
| 299 | - $this->assertInternalType('array', $data); |
|
| 300 | - |
|
| 301 | - $testPTLinkData = array_filter($testPTLinkData, function ($row) use ($testPostData) { |
|
| 302 | - $post = $row['post_id']; |
|
| 303 | - $post = array_filter($testPostData, function ($row) use ($post) { |
|
| 304 | - return ($post == $row['id']); |
|
| 305 | - }); |
|
| 306 | - $post = current($post); |
|
| 307 | - return ($post['display'] == 1); |
|
| 308 | - }); |
|
| 309 | - |
|
| 310 | - $tagCountData = array_map(function ($row) use ($testTagData) { |
|
| 311 | - $tag = $row['tag_id']; |
|
| 312 | - $tag = array_filter($testTagData, function ($row) use ($tag) { |
|
| 313 | - return ($tag == $row['id']); |
|
| 314 | - }); |
|
| 315 | - $tag = current($tag)['tag']; |
|
| 316 | - return $tag; |
|
| 317 | - }, $testPTLinkData); |
|
| 318 | - |
|
| 319 | - $testCountData = []; |
|
| 320 | - foreach ($tagCountData as $tagCountRow) { |
|
| 321 | - $incremented = false; |
|
| 322 | - foreach ($testCountData as $key => $testCountRow) { |
|
| 323 | - if ($tagCountRow == $testCountRow['tag']) { |
|
| 324 | - $testCountData[$key]['count']++; |
|
| 325 | - $incremented = true; |
|
| 326 | - break; |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - if (!$incremented) { |
|
| 330 | - array_push($testCountData, [ |
|
| 331 | - 'count' => 1, |
|
| 332 | - 'tag' => $tagCountRow, |
|
| 333 | - ]); |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - $this->assertCount(count($testCountData), $data); |
|
| 338 | - |
|
| 339 | - usort($testCountData, function ($rowA, $rowB) { |
|
| 340 | - return ($rowA['tag'] > $rowB['tag']); |
|
| 341 | - }); |
|
| 342 | - usort($data, function ($rowA, $rowB) { |
|
| 343 | - return ($rowA['tag'] > $rowB['tag']); |
|
| 344 | - }); |
|
| 345 | - |
|
| 346 | - foreach ($testCountData as $key => $testCountRow) { |
|
| 347 | - $this->assertArrayHasKey('count', $data[$key]); |
|
| 348 | - $this->assertEquals($testCountRow['count'], $data[$key]['count']); |
|
| 349 | - $this->assertArrayHasKey('tag', $data[$key]); |
|
| 350 | - $this->assertEquals($testCountRow['tag'], $data[$key]['tag']); |
|
| 351 | - } |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - public function testGetTagCloudFailure() |
|
| 355 | - { |
|
| 356 | - $testData = [ |
|
| 357 | - [ |
|
| 358 | - 'id' => rand(1, 100), |
|
| 359 | - 'tag' => 'test one', |
|
| 360 | - ], |
|
| 361 | - [ |
|
| 362 | - 'id' => rand(101, 200), |
|
| 363 | - 'tag' => 'test two', |
|
| 364 | - ], |
|
| 365 | - ]; |
|
| 366 | - |
|
| 367 | - array_walk($testData, [$this, 'insertTagData']); |
|
| 368 | - |
|
| 369 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 370 | - $data = $repository->getTagCloud(); |
|
| 371 | - |
|
| 372 | - $this->assertEmpty($data); |
|
| 373 | - $this->assertInternalType('array', $data); |
|
| 374 | - } |
|
| 144 | + public function testGetAllTagsFailure() |
|
| 145 | + { |
|
| 146 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 147 | + $data = $repository->getAllTags(); |
|
| 148 | + |
|
| 149 | + $this->assertEmpty($data); |
|
| 150 | + $this->assertInternalType('array', $data); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public function testGetTagCloud() |
|
| 154 | + { |
|
| 155 | + $testPostData = [ |
|
| 156 | + [ |
|
| 157 | + 'id' => rand(1, 100), |
|
| 158 | + 'display' => 1, |
|
| 159 | + ], |
|
| 160 | + [ |
|
| 161 | + 'id' => rand(101, 200), |
|
| 162 | + 'display' => 1, |
|
| 163 | + ], |
|
| 164 | + ]; |
|
| 165 | + |
|
| 166 | + $testTagData = [ |
|
| 167 | + [ |
|
| 168 | + 'id' => rand(1, 100), |
|
| 169 | + 'tag' => 'test one', |
|
| 170 | + ], |
|
| 171 | + [ |
|
| 172 | + 'id' => rand(101, 200), |
|
| 173 | + 'tag' => 'test two', |
|
| 174 | + ], |
|
| 175 | + [ |
|
| 176 | + 'id' => rand(201, 300), |
|
| 177 | + 'tag' => 'test three', |
|
| 178 | + ], |
|
| 179 | + ]; |
|
| 180 | + |
|
| 181 | + $testPTLinkData = []; |
|
| 182 | + foreach ($testPostData as $testPostRow) { |
|
| 183 | + $tempTagData = $testTagData; |
|
| 184 | + shuffle($tempTagData); |
|
| 185 | + for ($i = 0; $i < 2; $i++) { |
|
| 186 | + array_push($testPTLinkData, [ |
|
| 187 | + 'post_id' => $testPostRow['id'], |
|
| 188 | + 'tag_id' => $tempTagData[$i]['id'], |
|
| 189 | + ]); |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + array_walk($testPostData, [$this, 'insertPostData']); |
|
| 194 | + array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
|
| 195 | + array_walk($testTagData, [$this, 'insertTagData']); |
|
| 196 | + |
|
| 197 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 198 | + $data = $repository->getTagCloud(); |
|
| 199 | + |
|
| 200 | + $this->assertNotFalse($data); |
|
| 201 | + $this->assertInternalType('array', $data); |
|
| 202 | + |
|
| 203 | + $tagCountData = array_map(function ($row) use ($testTagData) { |
|
| 204 | + $tag = $row['tag_id']; |
|
| 205 | + $tag = array_filter($testTagData, function ($row) use ($tag) { |
|
| 206 | + return ($tag == $row['id']); |
|
| 207 | + }); |
|
| 208 | + $tag = current($tag)['tag']; |
|
| 209 | + return $tag; |
|
| 210 | + }, $testPTLinkData); |
|
| 211 | + |
|
| 212 | + $testCountData = []; |
|
| 213 | + foreach ($tagCountData as $tagCountRow) { |
|
| 214 | + $incremented = false; |
|
| 215 | + foreach ($testCountData as $key => $testCountRow) { |
|
| 216 | + if ($tagCountRow == $testCountRow['tag']) { |
|
| 217 | + $testCountData[$key]['count']++; |
|
| 218 | + $incremented = true; |
|
| 219 | + break; |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + if (!$incremented) { |
|
| 223 | + array_push($testCountData, [ |
|
| 224 | + 'count' => 1, |
|
| 225 | + 'tag' => $tagCountRow, |
|
| 226 | + ]); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $this->assertCount(count($testCountData), $data); |
|
| 231 | + |
|
| 232 | + usort($testCountData, function ($rowA, $rowB) { |
|
| 233 | + return ($rowA['tag'] > $rowB['tag']); |
|
| 234 | + }); |
|
| 235 | + usort($data, function ($rowA, $rowB) { |
|
| 236 | + return ($rowA['tag'] > $rowB['tag']); |
|
| 237 | + }); |
|
| 238 | + |
|
| 239 | + foreach ($testCountData as $key => $testCountRow) { |
|
| 240 | + $this->assertArrayHasKey('count', $data[$key]); |
|
| 241 | + $this->assertEquals($testCountRow['count'], $data[$key]['count']); |
|
| 242 | + $this->assertArrayHasKey('tag', $data[$key]); |
|
| 243 | + $this->assertEquals($testCountRow['tag'], $data[$key]['tag']); |
|
| 244 | + } |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + public function testGetTagCloudInactive() |
|
| 248 | + { |
|
| 249 | + $testPostData = [ |
|
| 250 | + [ |
|
| 251 | + 'id' => rand(1, 100), |
|
| 252 | + 'display' => 1, |
|
| 253 | + ], |
|
| 254 | + [ |
|
| 255 | + 'id' => rand(101, 200), |
|
| 256 | + 'display' => 1, |
|
| 257 | + ], |
|
| 258 | + [ |
|
| 259 | + 'id' => rand(201, 300), |
|
| 260 | + 'display' => 0, |
|
| 261 | + ], |
|
| 262 | + ]; |
|
| 263 | + |
|
| 264 | + $testTagData = [ |
|
| 265 | + [ |
|
| 266 | + 'id' => rand(1, 100), |
|
| 267 | + 'tag' => 'test one', |
|
| 268 | + ], |
|
| 269 | + [ |
|
| 270 | + 'id' => rand(101, 200), |
|
| 271 | + 'tag' => 'test two', |
|
| 272 | + ], |
|
| 273 | + [ |
|
| 274 | + 'id' => rand(201, 300), |
|
| 275 | + 'tag' => 'test three', |
|
| 276 | + ], |
|
| 277 | + ]; |
|
| 278 | + |
|
| 279 | + $testPTLinkData = []; |
|
| 280 | + foreach ($testPostData as $testPostRow) { |
|
| 281 | + $tempTagData = $testTagData; |
|
| 282 | + shuffle($tempTagData); |
|
| 283 | + for ($i = 0; $i < 2; $i++) { |
|
| 284 | + array_push($testPTLinkData, [ |
|
| 285 | + 'post_id' => $testPostRow['id'], |
|
| 286 | + 'tag_id' => $tempTagData[$i]['id'], |
|
| 287 | + ]); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + array_walk($testPostData, [$this, 'insertPostData']); |
|
| 292 | + array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
|
| 293 | + array_walk($testTagData, [$this, 'insertTagData']); |
|
| 294 | + |
|
| 295 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 296 | + $data = $repository->getTagCloud(); |
|
| 297 | + |
|
| 298 | + $this->assertNotFalse($data); |
|
| 299 | + $this->assertInternalType('array', $data); |
|
| 300 | + |
|
| 301 | + $testPTLinkData = array_filter($testPTLinkData, function ($row) use ($testPostData) { |
|
| 302 | + $post = $row['post_id']; |
|
| 303 | + $post = array_filter($testPostData, function ($row) use ($post) { |
|
| 304 | + return ($post == $row['id']); |
|
| 305 | + }); |
|
| 306 | + $post = current($post); |
|
| 307 | + return ($post['display'] == 1); |
|
| 308 | + }); |
|
| 309 | + |
|
| 310 | + $tagCountData = array_map(function ($row) use ($testTagData) { |
|
| 311 | + $tag = $row['tag_id']; |
|
| 312 | + $tag = array_filter($testTagData, function ($row) use ($tag) { |
|
| 313 | + return ($tag == $row['id']); |
|
| 314 | + }); |
|
| 315 | + $tag = current($tag)['tag']; |
|
| 316 | + return $tag; |
|
| 317 | + }, $testPTLinkData); |
|
| 318 | + |
|
| 319 | + $testCountData = []; |
|
| 320 | + foreach ($tagCountData as $tagCountRow) { |
|
| 321 | + $incremented = false; |
|
| 322 | + foreach ($testCountData as $key => $testCountRow) { |
|
| 323 | + if ($tagCountRow == $testCountRow['tag']) { |
|
| 324 | + $testCountData[$key]['count']++; |
|
| 325 | + $incremented = true; |
|
| 326 | + break; |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + if (!$incremented) { |
|
| 330 | + array_push($testCountData, [ |
|
| 331 | + 'count' => 1, |
|
| 332 | + 'tag' => $tagCountRow, |
|
| 333 | + ]); |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + $this->assertCount(count($testCountData), $data); |
|
| 338 | + |
|
| 339 | + usort($testCountData, function ($rowA, $rowB) { |
|
| 340 | + return ($rowA['tag'] > $rowB['tag']); |
|
| 341 | + }); |
|
| 342 | + usort($data, function ($rowA, $rowB) { |
|
| 343 | + return ($rowA['tag'] > $rowB['tag']); |
|
| 344 | + }); |
|
| 345 | + |
|
| 346 | + foreach ($testCountData as $key => $testCountRow) { |
|
| 347 | + $this->assertArrayHasKey('count', $data[$key]); |
|
| 348 | + $this->assertEquals($testCountRow['count'], $data[$key]['count']); |
|
| 349 | + $this->assertArrayHasKey('tag', $data[$key]); |
|
| 350 | + $this->assertEquals($testCountRow['tag'], $data[$key]['tag']); |
|
| 351 | + } |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + public function testGetTagCloudFailure() |
|
| 355 | + { |
|
| 356 | + $testData = [ |
|
| 357 | + [ |
|
| 358 | + 'id' => rand(1, 100), |
|
| 359 | + 'tag' => 'test one', |
|
| 360 | + ], |
|
| 361 | + [ |
|
| 362 | + 'id' => rand(101, 200), |
|
| 363 | + 'tag' => 'test two', |
|
| 364 | + ], |
|
| 365 | + ]; |
|
| 366 | + |
|
| 367 | + array_walk($testData, [$this, 'insertTagData']); |
|
| 368 | + |
|
| 369 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 370 | + $data = $repository->getTagCloud(); |
|
| 371 | + |
|
| 372 | + $this->assertEmpty($data); |
|
| 373 | + $this->assertInternalType('array', $data); |
|
| 374 | + } |
|
| 375 | 375 | |
| 376 | - public function testGetTagsForPost() |
|
| 377 | - { |
|
| 378 | - $testPostData = [ |
|
| 379 | - 'id' => rand(1, 100), |
|
| 380 | - 'display' => 1, |
|
| 381 | - ]; |
|
| 382 | - |
|
| 383 | - $testTagData = [ |
|
| 384 | - [ |
|
| 385 | - 'id' => rand(1, 100), |
|
| 386 | - 'tag' => 'test one', |
|
| 387 | - ], |
|
| 388 | - [ |
|
| 389 | - 'id' => rand(101, 200), |
|
| 390 | - 'tag' => 'test two', |
|
| 391 | - ], |
|
| 392 | - [ |
|
| 393 | - 'id' => rand(201, 300), |
|
| 394 | - 'tag' => 'test three', |
|
| 395 | - ], |
|
| 396 | - ]; |
|
| 397 | - |
|
| 398 | - $testPTLinkData = []; |
|
| 399 | - foreach ($testTagData as $testTagRow) { |
|
| 400 | - array_push($testPTLinkData, [ |
|
| 401 | - 'post_id' => $testPostData['id'], |
|
| 402 | - 'tag_id' => $testTagRow['id'], |
|
| 403 | - ]); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - $this->insertPostData($testPostData); |
|
| 407 | - array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
|
| 408 | - array_walk($testTagData, [$this, 'insertTagData']); |
|
| 409 | - |
|
| 410 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 411 | - $data = $repository->getTagsForPost($testPostData['id']); |
|
| 412 | - |
|
| 413 | - $this->assertNotFalse($data); |
|
| 414 | - $this->assertInternalType('array', $data); |
|
| 415 | - $this->assertCount(count($testTagData), $data); |
|
| 416 | - |
|
| 417 | - usort($testTagData, function ($rowA, $rowB) { |
|
| 418 | - return ($rowA['tag'] > $rowB['tag']); |
|
| 419 | - }); |
|
| 420 | - |
|
| 421 | - foreach ($testTagData as $key => $testTagRow) { |
|
| 422 | - $this->assertArrayHasKey('id', $data[$key]); |
|
| 423 | - $this->assertEquals($testTagRow['id'], $data[$key]['id']); |
|
| 424 | - $this->assertArrayHasKey('tag', $data[$key]); |
|
| 425 | - $this->assertEquals($testTagRow['tag'], $data[$key]['tag']); |
|
| 426 | - } |
|
| 427 | - } |
|
| 376 | + public function testGetTagsForPost() |
|
| 377 | + { |
|
| 378 | + $testPostData = [ |
|
| 379 | + 'id' => rand(1, 100), |
|
| 380 | + 'display' => 1, |
|
| 381 | + ]; |
|
| 382 | + |
|
| 383 | + $testTagData = [ |
|
| 384 | + [ |
|
| 385 | + 'id' => rand(1, 100), |
|
| 386 | + 'tag' => 'test one', |
|
| 387 | + ], |
|
| 388 | + [ |
|
| 389 | + 'id' => rand(101, 200), |
|
| 390 | + 'tag' => 'test two', |
|
| 391 | + ], |
|
| 392 | + [ |
|
| 393 | + 'id' => rand(201, 300), |
|
| 394 | + 'tag' => 'test three', |
|
| 395 | + ], |
|
| 396 | + ]; |
|
| 397 | + |
|
| 398 | + $testPTLinkData = []; |
|
| 399 | + foreach ($testTagData as $testTagRow) { |
|
| 400 | + array_push($testPTLinkData, [ |
|
| 401 | + 'post_id' => $testPostData['id'], |
|
| 402 | + 'tag_id' => $testTagRow['id'], |
|
| 403 | + ]); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + $this->insertPostData($testPostData); |
|
| 407 | + array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
|
| 408 | + array_walk($testTagData, [$this, 'insertTagData']); |
|
| 409 | + |
|
| 410 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 411 | + $data = $repository->getTagsForPost($testPostData['id']); |
|
| 412 | + |
|
| 413 | + $this->assertNotFalse($data); |
|
| 414 | + $this->assertInternalType('array', $data); |
|
| 415 | + $this->assertCount(count($testTagData), $data); |
|
| 416 | + |
|
| 417 | + usort($testTagData, function ($rowA, $rowB) { |
|
| 418 | + return ($rowA['tag'] > $rowB['tag']); |
|
| 419 | + }); |
|
| 420 | + |
|
| 421 | + foreach ($testTagData as $key => $testTagRow) { |
|
| 422 | + $this->assertArrayHasKey('id', $data[$key]); |
|
| 423 | + $this->assertEquals($testTagRow['id'], $data[$key]['id']); |
|
| 424 | + $this->assertArrayHasKey('tag', $data[$key]); |
|
| 425 | + $this->assertEquals($testTagRow['tag'], $data[$key]['tag']); |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | 428 | |
| 429 | - public function testGetTagsForPostFailure() |
|
| 430 | - { |
|
| 431 | - $testData = [ |
|
| 432 | - [ |
|
| 433 | - 'id' => rand(1, 100), |
|
| 434 | - 'tag' => 'test one', |
|
| 435 | - ], |
|
| 436 | - [ |
|
| 437 | - 'id' => rand(101, 200), |
|
| 438 | - 'tag' => 'test two', |
|
| 439 | - ], |
|
| 440 | - ]; |
|
| 441 | - |
|
| 442 | - array_walk($testData, [$this, 'insertTagData']); |
|
| 443 | - |
|
| 444 | - $repository = new MysqlTagRepository(self::$connection); |
|
| 445 | - $data = $repository->getTagsForPost(rand(1, 10)); |
|
| 446 | - |
|
| 447 | - $this->assertEmpty($data); |
|
| 448 | - $this->assertInternalType('array', $data); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - protected function insertPostData(array $data) |
|
| 452 | - { |
|
| 453 | - $defaultData = [ |
|
| 454 | - 'id' => null, |
|
| 455 | - 'title' => '', |
|
| 456 | - 'path' => '', |
|
| 457 | - 'category' => '', |
|
| 458 | - 'date' => '', |
|
| 459 | - 'body' => '', |
|
| 460 | - 'display' => 0, |
|
| 461 | - ]; |
|
| 462 | - |
|
| 463 | - $data = array_merge($defaultData, $data); |
|
| 464 | - |
|
| 465 | - return self::$connection->getDefault()->perform(" |
|
| 429 | + public function testGetTagsForPostFailure() |
|
| 430 | + { |
|
| 431 | + $testData = [ |
|
| 432 | + [ |
|
| 433 | + 'id' => rand(1, 100), |
|
| 434 | + 'tag' => 'test one', |
|
| 435 | + ], |
|
| 436 | + [ |
|
| 437 | + 'id' => rand(101, 200), |
|
| 438 | + 'tag' => 'test two', |
|
| 439 | + ], |
|
| 440 | + ]; |
|
| 441 | + |
|
| 442 | + array_walk($testData, [$this, 'insertTagData']); |
|
| 443 | + |
|
| 444 | + $repository = new MysqlTagRepository(self::$connection); |
|
| 445 | + $data = $repository->getTagsForPost(rand(1, 10)); |
|
| 446 | + |
|
| 447 | + $this->assertEmpty($data); |
|
| 448 | + $this->assertInternalType('array', $data); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + protected function insertPostData(array $data) |
|
| 452 | + { |
|
| 453 | + $defaultData = [ |
|
| 454 | + 'id' => null, |
|
| 455 | + 'title' => '', |
|
| 456 | + 'path' => '', |
|
| 457 | + 'category' => '', |
|
| 458 | + 'date' => '', |
|
| 459 | + 'body' => '', |
|
| 460 | + 'display' => 0, |
|
| 461 | + ]; |
|
| 462 | + |
|
| 463 | + $data = array_merge($defaultData, $data); |
|
| 464 | + |
|
| 465 | + return self::$connection->getDefault()->perform(" |
|
| 466 | 466 | INSERT INTO `jpemeric_blog`.`post` |
| 467 | 467 | (id, title, path, category, date, body, display) |
| 468 | 468 | VALUES |
| 469 | 469 | (:id, :title, :path, :category, :date, :body, :display)", |
| 470 | - $data |
|
| 471 | - ); |
|
| 472 | - } |
|
| 470 | + $data |
|
| 471 | + ); |
|
| 472 | + } |
|
| 473 | 473 | |
| 474 | - protected function insertPTLinkData(array $data) |
|
| 475 | - { |
|
| 476 | - $defaultData = [ |
|
| 477 | - 'post' => null, |
|
| 478 | - 'tag' => null, |
|
| 479 | - ]; |
|
| 474 | + protected function insertPTLinkData(array $data) |
|
| 475 | + { |
|
| 476 | + $defaultData = [ |
|
| 477 | + 'post' => null, |
|
| 478 | + 'tag' => null, |
|
| 479 | + ]; |
|
| 480 | 480 | |
| 481 | - $data = array_merge($defaultData, $data); |
|
| 481 | + $data = array_merge($defaultData, $data); |
|
| 482 | 482 | |
| 483 | - return self::$connection->getDefault()->perform(" |
|
| 483 | + return self::$connection->getDefault()->perform(" |
|
| 484 | 484 | INSERT INTO `jpemeric_blog`.`ptlink` |
| 485 | 485 | (post_id, tag_id) |
| 486 | 486 | VALUES |
| 487 | 487 | (:post_id, :tag_id)", |
| 488 | - $data |
|
| 489 | - ); |
|
| 490 | - } |
|
| 488 | + $data |
|
| 489 | + ); |
|
| 490 | + } |
|
| 491 | 491 | |
| 492 | - protected function insertTagData(array $data) |
|
| 493 | - { |
|
| 494 | - $defaultData = [ |
|
| 495 | - 'id' => null, |
|
| 496 | - 'tag' => '', |
|
| 497 | - ]; |
|
| 492 | + protected function insertTagData(array $data) |
|
| 493 | + { |
|
| 494 | + $defaultData = [ |
|
| 495 | + 'id' => null, |
|
| 496 | + 'tag' => '', |
|
| 497 | + ]; |
|
| 498 | 498 | |
| 499 | - $data = array_merge($defaultData, $data); |
|
| 499 | + $data = array_merge($defaultData, $data); |
|
| 500 | 500 | |
| 501 | - return self::$connection->getDefault()->perform(" |
|
| 501 | + return self::$connection->getDefault()->perform(" |
|
| 502 | 502 | INSERT INTO `jpemeric_blog`.`tag` |
| 503 | 503 | (id, tag) |
| 504 | 504 | VALUES |
| 505 | 505 | (:id, :tag)", |
| 506 | - $data |
|
| 507 | - ); |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - protected function tearDown() |
|
| 511 | - { |
|
| 512 | - self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`post`"); |
|
| 513 | - self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`ptlink`"); |
|
| 514 | - self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`tag`"); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - public static function tearDownAfterClass() |
|
| 518 | - { |
|
| 519 | - self::$connection->getDefault()->disconnect(); |
|
| 520 | - unlink('jpemeric_blog.db'); |
|
| 521 | - } |
|
| 506 | + $data |
|
| 507 | + ); |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + protected function tearDown() |
|
| 511 | + { |
|
| 512 | + self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`post`"); |
|
| 513 | + self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`ptlink`"); |
|
| 514 | + self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`tag`"); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + public static function tearDownAfterClass() |
|
| 518 | + { |
|
| 519 | + self::$connection->getDefault()->disconnect(); |
|
| 520 | + unlink('jpemeric_blog.db'); |
|
| 521 | + } |
|
| 522 | 522 | } |
@@ -9,14 +9,14 @@ discard block |
||
| 9 | 9 | class MysqlIntroductionRepositoryTest extends PHPUnit_Framework_TestCase |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - protected static $connection; |
|
| 12 | + protected static $connection; |
|
| 13 | 13 | |
| 14 | - public static function setUpBeforeClass() |
|
| 15 | - { |
|
| 16 | - $extendedPdo = new ExtendedPdo('sqlite::memory:'); |
|
| 17 | - $extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`"); |
|
| 14 | + public static function setUpBeforeClass() |
|
| 15 | + { |
|
| 16 | + $extendedPdo = new ExtendedPdo('sqlite::memory:'); |
|
| 17 | + $extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`"); |
|
| 18 | 18 | |
| 19 | - $extendedPdo->exec(" |
|
| 19 | + $extendedPdo->exec(" |
|
| 20 | 20 | CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`introduction` ( |
| 21 | 21 | `id` integer PRIMARY KEY AUTOINCREMENT, |
| 22 | 22 | `type` varchar(10) NOT NULL, |
@@ -25,134 +25,134 @@ discard block |
||
| 25 | 25 | `content` text NOT NULL, |
| 26 | 26 | `image` image |
| 27 | 27 | )" |
| 28 | - ); |
|
| 29 | - |
|
| 30 | - self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
|
| 31 | - return $extendedPdo; |
|
| 32 | - }); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public function testIsInstanceOfIntroductionRepository() |
|
| 36 | - { |
|
| 37 | - $repository = new MysqlIntroductionRepository(self::$connection); |
|
| 38 | - |
|
| 39 | - $this->assertInstanceOf( |
|
| 40 | - 'Jacobemerick\Web\Domain\Blog\Introduction\MysqlIntroductionRepository', |
|
| 41 | - $repository |
|
| 42 | - ); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function testImplementsIntroductionInterface() |
|
| 46 | - { |
|
| 47 | - $repository = new MysqlIntroductionRepository(self::$connection); |
|
| 48 | - |
|
| 49 | - $this->assertInstanceOf( |
|
| 50 | - 'Jacobemerick\Web\Domain\Blog\Introduction\IntroductionRepositoryInterface', |
|
| 51 | - $repository |
|
| 52 | - ); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function testConstructSetsConnections() |
|
| 56 | - { |
|
| 57 | - $respository = new MysqlIntroductionRepository(self::$connection); |
|
| 58 | - |
|
| 59 | - $this->assertAttributeSame( |
|
| 60 | - self::$connection, |
|
| 61 | - 'connections', |
|
| 62 | - $respository |
|
| 63 | - ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public function testFindByType() |
|
| 67 | - { |
|
| 68 | - $testData = [ |
|
| 69 | - 'id' => rand(1, 100), |
|
| 70 | - 'type' => 'test', |
|
| 71 | - 'title' => 'test title', |
|
| 72 | - 'content' => 'test content', |
|
| 73 | - 'image' => rand(1, 100), |
|
| 74 | - ]; |
|
| 75 | - |
|
| 76 | - $this->insertData($testData); |
|
| 77 | - |
|
| 78 | - $repository = new MysqlIntroductionRepository(self::$connection); |
|
| 79 | - $data = $repository->findByType('test'); |
|
| 80 | - |
|
| 81 | - $this->assertNotFalse($data); |
|
| 82 | - $this->assertInternalType('array', $data); |
|
| 83 | - $this->assertArrayHasKey('title', $data); |
|
| 84 | - $this->assertEquals($testData['title'], $data['title']); |
|
| 85 | - $this->assertArrayHasKey('content', $data); |
|
| 86 | - $this->assertEquals($testData['content'], $data['content']); |
|
| 87 | - $this->assertArrayHasKey('image', $data); |
|
| 88 | - $this->assertEquals($testData['image'], $data['image']); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - public function testFindByTypeFilterValue() |
|
| 92 | - { |
|
| 93 | - $testData = [ |
|
| 94 | - [ |
|
| 95 | - 'id' => rand(1, 100), |
|
| 96 | - 'type' => 'test', |
|
| 97 | - 'title' => 'title a', |
|
| 98 | - 'value' => 'value a', |
|
| 99 | - ], |
|
| 100 | - [ |
|
| 101 | - 'id' => rand(101, 200), |
|
| 102 | - 'type' => 'test', |
|
| 103 | - 'title' => 'title b', |
|
| 104 | - 'value' => 'value b', |
|
| 105 | - ], |
|
| 106 | - ]; |
|
| 107 | - |
|
| 108 | - array_walk($testData, [$this, 'insertData']); |
|
| 109 | - |
|
| 110 | - $repository = new MysqlIntroductionRepository(self::$connection); |
|
| 111 | - $data = $repository->findByType('test', reset($testData)['value']); |
|
| 112 | - |
|
| 113 | - $this->assertNotFalse($data); |
|
| 114 | - $this->assertInternalType('array', $data); |
|
| 115 | - $this->assertEquals(reset($testData)['title'], $data['title']); |
|
| 116 | - } |
|
| 28 | + ); |
|
| 29 | + |
|
| 30 | + self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
|
| 31 | + return $extendedPdo; |
|
| 32 | + }); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public function testIsInstanceOfIntroductionRepository() |
|
| 36 | + { |
|
| 37 | + $repository = new MysqlIntroductionRepository(self::$connection); |
|
| 38 | + |
|
| 39 | + $this->assertInstanceOf( |
|
| 40 | + 'Jacobemerick\Web\Domain\Blog\Introduction\MysqlIntroductionRepository', |
|
| 41 | + $repository |
|
| 42 | + ); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function testImplementsIntroductionInterface() |
|
| 46 | + { |
|
| 47 | + $repository = new MysqlIntroductionRepository(self::$connection); |
|
| 48 | + |
|
| 49 | + $this->assertInstanceOf( |
|
| 50 | + 'Jacobemerick\Web\Domain\Blog\Introduction\IntroductionRepositoryInterface', |
|
| 51 | + $repository |
|
| 52 | + ); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function testConstructSetsConnections() |
|
| 56 | + { |
|
| 57 | + $respository = new MysqlIntroductionRepository(self::$connection); |
|
| 58 | + |
|
| 59 | + $this->assertAttributeSame( |
|
| 60 | + self::$connection, |
|
| 61 | + 'connections', |
|
| 62 | + $respository |
|
| 63 | + ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public function testFindByType() |
|
| 67 | + { |
|
| 68 | + $testData = [ |
|
| 69 | + 'id' => rand(1, 100), |
|
| 70 | + 'type' => 'test', |
|
| 71 | + 'title' => 'test title', |
|
| 72 | + 'content' => 'test content', |
|
| 73 | + 'image' => rand(1, 100), |
|
| 74 | + ]; |
|
| 75 | + |
|
| 76 | + $this->insertData($testData); |
|
| 77 | + |
|
| 78 | + $repository = new MysqlIntroductionRepository(self::$connection); |
|
| 79 | + $data = $repository->findByType('test'); |
|
| 80 | + |
|
| 81 | + $this->assertNotFalse($data); |
|
| 82 | + $this->assertInternalType('array', $data); |
|
| 83 | + $this->assertArrayHasKey('title', $data); |
|
| 84 | + $this->assertEquals($testData['title'], $data['title']); |
|
| 85 | + $this->assertArrayHasKey('content', $data); |
|
| 86 | + $this->assertEquals($testData['content'], $data['content']); |
|
| 87 | + $this->assertArrayHasKey('image', $data); |
|
| 88 | + $this->assertEquals($testData['image'], $data['image']); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + public function testFindByTypeFilterValue() |
|
| 92 | + { |
|
| 93 | + $testData = [ |
|
| 94 | + [ |
|
| 95 | + 'id' => rand(1, 100), |
|
| 96 | + 'type' => 'test', |
|
| 97 | + 'title' => 'title a', |
|
| 98 | + 'value' => 'value a', |
|
| 99 | + ], |
|
| 100 | + [ |
|
| 101 | + 'id' => rand(101, 200), |
|
| 102 | + 'type' => 'test', |
|
| 103 | + 'title' => 'title b', |
|
| 104 | + 'value' => 'value b', |
|
| 105 | + ], |
|
| 106 | + ]; |
|
| 107 | + |
|
| 108 | + array_walk($testData, [$this, 'insertData']); |
|
| 109 | + |
|
| 110 | + $repository = new MysqlIntroductionRepository(self::$connection); |
|
| 111 | + $data = $repository->findByType('test', reset($testData)['value']); |
|
| 112 | + |
|
| 113 | + $this->assertNotFalse($data); |
|
| 114 | + $this->assertInternalType('array', $data); |
|
| 115 | + $this->assertEquals(reset($testData)['title'], $data['title']); |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - public function testFindByTypeFailure() |
|
| 119 | - { |
|
| 120 | - $repository = new MysqlIntroductionRepository(self::$connection); |
|
| 121 | - $data = $repository->findByType('empty type'); |
|
| 122 | - |
|
| 123 | - $this->assertFalse($data); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - protected function insertData(array $data) |
|
| 127 | - { |
|
| 128 | - $defaultData = [ |
|
| 129 | - 'id' => null, |
|
| 130 | - 'type' => '', |
|
| 131 | - 'value' => '', |
|
| 132 | - 'title' => '', |
|
| 133 | - 'content' => '', |
|
| 134 | - 'image' => null, |
|
| 135 | - ]; |
|
| 136 | - |
|
| 137 | - $data = array_merge($defaultData, $data); |
|
| 138 | - |
|
| 139 | - return self::$connection->getDefault()->perform(" |
|
| 118 | + public function testFindByTypeFailure() |
|
| 119 | + { |
|
| 120 | + $repository = new MysqlIntroductionRepository(self::$connection); |
|
| 121 | + $data = $repository->findByType('empty type'); |
|
| 122 | + |
|
| 123 | + $this->assertFalse($data); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + protected function insertData(array $data) |
|
| 127 | + { |
|
| 128 | + $defaultData = [ |
|
| 129 | + 'id' => null, |
|
| 130 | + 'type' => '', |
|
| 131 | + 'value' => '', |
|
| 132 | + 'title' => '', |
|
| 133 | + 'content' => '', |
|
| 134 | + 'image' => null, |
|
| 135 | + ]; |
|
| 136 | + |
|
| 137 | + $data = array_merge($defaultData, $data); |
|
| 138 | + |
|
| 139 | + return self::$connection->getDefault()->perform(" |
|
| 140 | 140 | INSERT INTO `jpemeric_blog`.`introduction` |
| 141 | 141 | (id, type, value, title, content, image) |
| 142 | 142 | VALUES |
| 143 | 143 | (:id, :type, :value, :title, :content, :image)", |
| 144 | - $data |
|
| 145 | - ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - protected function tearDown() |
|
| 149 | - { |
|
| 150 | - self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`introduction`"); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public static function tearDownAfterClass() |
|
| 154 | - { |
|
| 155 | - self::$connection->getDefault()->disconnect(); |
|
| 156 | - unlink('jpemeric_blog.db'); |
|
| 157 | - } |
|
| 144 | + $data |
|
| 145 | + ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + protected function tearDown() |
|
| 149 | + { |
|
| 150 | + self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`introduction`"); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public static function tearDownAfterClass() |
|
| 154 | + { |
|
| 155 | + self::$connection->getDefault()->disconnect(); |
|
| 156 | + unlink('jpemeric_blog.db'); |
|
| 157 | + } |
|
| 158 | 158 | } |