diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..78567a0 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +/tests export-ignore +/preview export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.travis.yml export-ignore +.scrutinizer.yml export-ignore +phpcs.xml export-ignore diff --git a/.gitignore b/.gitignore index 7579f74..fa36fe5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor composer.lock +.idea diff --git a/.travis.yml b/.travis.yml index cf0c888..f8b6089 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,15 @@ language: php php: - - 5.5 - - 5.4 + - 5.6 + - 7.0 before_script: - composer self-update - composer update script: + - php ./vendor/bin/phpcs - php ./vendor/bin/phpunit -c ./tests/ --coverage-clover=coverage.xml after_success: diff --git a/LICENSE.md b/LICENSE.md index 8afeff3..e72c6e5 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2013-2014 Robert Boloc +Copyright (c) 2013-2017 Robert Boloc Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/Module.php b/Module.php index 3b4f60f..a9ee1e0 100644 --- a/Module.php +++ b/Module.php @@ -1,82 +1,28 @@ array( - __DIR__ . '/autoload_classmap.php', - ), - 'Zend\Loader\StandardAutoloader' => array( - 'namespaces' => array( - __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, - ), - ), - ); - } - - public function getServiceConfig() - { - return array( - 'factories' => array( - 'RbComment\Model\CommentTable' => function($sm) { - $tableGateway = $sm->get('RbCommentTableGateway'); - $table = new RbCommentTable($tableGateway); - return $table; - }, - 'RbCommentTableGateway' => function ($sm) { - $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); - $resultSetPrototype = new ResultSet(); - $resultSetPrototype->setArrayObjectPrototype(new RbComment()); - return new TableGateway('rb_comments', $dbAdapter, null, $resultSetPrototype); - }, - /** - * Placeholder transport config. Do not use this in production. - * Replace with smtp. - */ - 'RbComment\Mailer' => function () { - return new Sendmail(); - }, - /** - * Akismet service instance factory. Uses the config down below. - */ - 'RbComment\Akismet' => function ($serviceManager) { - - $config = $serviceManager->get('Config'); - $viewHelperManager = $serviceManager->get('viewhelpermanager'); - - $akismetConfig = $config['rb_comment']['akismet']; - - return new Akismet( - $akismetConfig['api_key'], - $viewHelperManager->get('serverUrl')->__invoke() - ); - } - ), - ); - } - + /** + * @param Console $console + * + * @return array + */ public function getConsoleUsage(Console $console) { - return array( + return [ 'delete spam' => 'Delete all comments marked as spam from the database', - ); + ]; } } diff --git a/README.md b/README.md index 30fdb12..2068e04 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ email notifications, Akismet, ZfcUser and Gravatar integration. ```json { "require": { - "robertboloc/rbcomment": "1.4.0" + "robertboloc/rbcomment": "^2.0" } } ``` @@ -222,4 +222,4 @@ pick one of this tasks : * If you are using the BjyAuthorize module (or any other route restricting module) make sure the route `rbcomment` is publicly accessible. -* This module assumes you have a database adapter configured. \ No newline at end of file +* This module assumes you have a database adapter configured. diff --git a/composer.json b/composer.json index 912741d..a25530d 100644 --- a/composer.json +++ b/composer.json @@ -7,38 +7,41 @@ "comments", "zf2" ], - "homepage": "http://github.com/robertboloc/RbComment", + "homepage": "https://github.com/robertboloc/RbComment", "license": "MIT", "authors": [ { "name": "Robert Boloc", "email": "robertboloc@gmail.com", - "homepage": "http://robertboloc.eu", + "homepage": "https://robertboloc.eu", "role": "Developer" } ], "require": { - "php": ">=5.3.0", - "zendframework/zend-mvc": "2.*", - "zendframework/zend-form": "2.*", - "zendframework/zend-db": "2.*", - "zendframework/zend-inputfilter": "2.*", - "zendframework/zend-view": "2.*", - "zendframework/zend-servicemanager": "2.*", - "zendframework/zend-loader": "2.*", - "zendframework/zend-stdlib": "2.*", - "zendframework/zend-modulemanager": "2.*", - "zendframework/zend-serializer": "2.*", - "zendframework/zend-mail": "2.*", - "zendframework/zend-mime": "2.*", - "zendframework/zend-i18n": "2.*", - "zendframework/zendservice-akismet": "2.0.2", - "zendframework/zend-http": "2.*", - "zendframework/zend-console": "2.*" + "php": "^5.6 || ^7.0", + "zendframework/zend-console": "^2.6", + "zendframework/zend-db": "^2.8.1", + "zendframework/zend-form": "^2.9", + "zendframework/zend-http": "^2.5.4", + "zendframework/zend-i18n": "^2.7.3", + "zendframework/zend-inputfilter": "^2.7.2", + "zendframework/zend-loader": "^2.5.1", + "zendframework/zend-mail": "^2.7.1", + "zendframework/zend-math": "^3.0", + "zendframework/zend-mime": "^2.6", + "zendframework/zend-modulemanager": "^2.7.2", + "zendframework/zend-mvc": "^3.0.1", + "zendframework/zend-mvc-plugin-flashmessenger": "^1.0", + "zendframework/zend-serializer": "^2.8", + "zendframework/zend-servicemanager": "^3.1", + "zendframework/zend-stdlib": "^3.0.1", + "zendframework/zend-view": "^2.8", + "zendframework/zendservice-akismet": "2.0.2" }, "require-dev": { "robertboloc/zf2-components-list-generator": "dev-master", - "phpunit/phpunit": "3.7.*" + "phpunit/phpunit": "^5.7", + "squizlabs/php_codesniffer": "^2.7" }, "autoload": { "psr-0": { @@ -50,7 +53,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.5.x-dev" + "dev-master": "2.0.x-dev" } } -} \ No newline at end of file +} diff --git a/config/module.config.php b/config/module.config.php index ae9e733..1c86149 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,78 +1,97 @@ array( - 'routes' => array( - 'rbcomment' => array( +use RbComment\Factory; +use RbComment\Model\CommentTable; +use Zend\Mail\Transport\Sendmail; + +return [ + 'router' => [ + 'routes' => [ + 'rbcomment' => [ 'type' => 'segment', - 'options' => array( + 'options' => [ 'route' => '/rbcomment/:action', - 'constraints' => array( + 'constraints' => [ 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', - ), - 'defaults' => array( + ], + 'defaults' => [ 'controller' => 'RbComment\Controller\Comment', - ), - ), - ), - ) - ), - 'console' => array( - 'router' => array( - 'routes' => array( - 'delete-spam' => array( - 'options' => array( + ], + ], + ], + ] + ], + 'console' => [ + 'router' => [ + 'routes' => [ + 'delete-spam' => [ + 'options' => [ 'route' => 'delete spam', - 'defaults' => array( + 'defaults' => [ 'controller' => 'RbComment\Controller\Console', - 'action' => 'delete-spam', - ), - ), - ), - ), - ), - ), - 'controllers' => array( - 'invokables' => array( - 'RbComment\Controller\Comment' => 'RbComment\Controller\CommentController', - 'RbComment\Controller\Console' => 'RbComment\Controller\ConsoleController', - ), - ), - 'controller_plugins' => array( - 'invokables' => array( - 'rbMailer' => 'RbComment\Mvc\Controller\Plugin\Mailer', - ) - ), - 'view_helpers' => array( - 'invokables' => array( - 'rbComment' => 'RbComment\View\Helper\Comment', - ) - ), - 'view_manager' => array( - 'template_map' => array( + 'action' => 'delete-spam', + ], + ], + ], + ], + ], + ], + 'controllers' => [ + 'factories' => [ + 'RbComment\Controller\Comment' => Factory\Controller\CommentControllerFactory::class, + 'RbComment\Controller\Console' => Factory\Controller\ConsoleControllerFactory::class, + ], + ], + 'controller_plugins' => [ + 'factories' => [ + 'rbMailer' => Factory\Mvc\Controller\Plugin\MailerFactory::class, + ] + ], + 'service_manager' => [ + 'factories' => [ + CommentTable::class => Factory\Model\CommentTableFactory::class, + 'RbCommentTableGateway' => Factory\Model\RbCommentTableGatewayFactory::class, + 'RbComment\Akismet' => Factory\Service\AkismetServiceFactory::class, + ], + 'invokables' => [ + /** + * Placeholder transport config. Do not use this in production. + * Replace with SMTP. + */ + 'RbComment\Mailer' => Sendmail::class, + ], + ], + 'view_helpers' => [ + 'factories' => [ + 'rbComment' => Factory\View\Helper\CommentFactory::class, + ] + ], + 'view_manager' => [ + 'template_map' => [ 'rbcomment/theme/uikit' => __DIR__ . '/../view/theme/uikit.phtml', 'rbcomment/theme/bootstrap3' => __DIR__ . '/../view/theme/bootstrap3.phtml', 'rbcomment/theme/default' => __DIR__ . '/../view/theme/default.phtml', - ), - ), - 'rb_comment' => array( + ], + ], + 'rb_comment' => [ /** * Default visibility of the comments. */ 'default_visibility' => 1, - 'strings' => array( - 'author' => 'Author', - 'contact' => 'Email', - 'content' => 'Comment', - 'submit' => 'Post', - 'comments' => 'Comments', - 'required' => 'All fields are required. Contact info will not be published.', - 'signout' => 'Sign Out', - 'signin' => 'Sign In', - 'signedinas' => 'You are signed in as', + 'strings' => [ + 'author' => 'Author', + 'contact' => 'Email', + 'content' => 'Comment', + 'submit' => 'Post', + 'comments' => 'Comments', + 'required' => 'All fields are required. Contact info will not be published.', + 'signout' => 'Sign Out', + 'signin' => 'Sign In', + 'signedinas' => 'You are signed in as', 'notsignedin' => 'You are not signed in. To be able to comment, please ', - ), - 'email' => array( + ], + 'email' => [ /** * Send email notifications. */ @@ -80,7 +99,7 @@ /** * Email addresses where to send the notification. */ - 'to' => array(), + 'to' => [], /** * From header. Usually something like noreply@myserver.com */ @@ -93,8 +112,8 @@ * Text of the comment link. */ 'context_link_text' => 'See this comment in context', - ), - 'akismet' => array( + ], + 'akismet' => [ /** * If this is true, the comment will be checked for spam. */ @@ -108,7 +127,7 @@ * be configured to avoid false positives. * Uses the class \Zend\Http\PhpEnvironment\RemoteAddress */ - 'proxy' => array( + 'proxy' => [ /** * Use proxy addresses or not. */ @@ -116,25 +135,25 @@ /** * List of trusted proxy IP addresses. */ - 'trusted' => array( - ), + 'trusted' => [ + ], /** * HTTP header to introspect for proxies. */ 'header' => 'X-Forwarded-For', - ), - ), - 'zfc_user' => array( + ], + ], + 'zfc_user' => [ /** * This enables the ZfcUser integration. */ 'enabled' => false, - ), - 'gravatar' => array( + ], + 'gravatar' => [ /** * This enables the Gravatar integration. */ 'enabled' => false, - ), - ), -); \ No newline at end of file + ], + ], +]; diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..255744c --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,23 @@ + + + RbComment Coding Standard + + + + + + + + + + + + + + + + config + src + tests + Module.php + diff --git a/src/RbComment/Controller/CommentController.php b/src/RbComment/Controller/CommentController.php index 6a81d0c..e686cd8 100644 --- a/src/RbComment/Controller/CommentController.php +++ b/src/RbComment/Controller/CommentController.php @@ -1,28 +1,43 @@ configService = $configService; + $this->commentTable = $commentTable; + $this->akismetService = $akismetService; + } public function addAction() { - $config = $this->getServiceLocator()->get('Config'); - $rbCommentConfig = (object) $config['rb_comment']; + $rbCommentConfig = (object) $this->configService['rb_comment']; $form = new CommentForm($rbCommentConfig->strings); @@ -46,7 +61,7 @@ public function addAction() } // We need the id for the mailer - $comment->id = $this->getCommentTable()->saveComment($comment); + $comment->id = $this->commentTable->saveComment($comment); // Send email if active and not spam if (($rbCommentConfig->email['notify'] === true) && @@ -67,8 +82,8 @@ public function addAction() /** * Checks if a comment is spam using the akismet service. * - * @param \RbComment\Model\Comment $comment - * @param mixed $rbCommentConfig + * @param Comment $comment + * @param mixed $rbCommentConfig * @return boolean */ protected function isSpam($comment, $rbCommentConfig) @@ -78,39 +93,15 @@ protected function isSpam($comment, $rbCommentConfig) $remote->setTrustedProxies($rbCommentConfig->akismet['proxy']['trusted']); $remote->setProxyHeader($rbCommentConfig->akismet['proxy']['header']); - return $this->getAkismetService()->isSpam(array( - 'user_ip' => $remote->getIpAddress(), - 'user_agent' => filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), - 'comment_type' => 'comment', - 'comment_author' => $comment->author, + $content = [ + 'user_ip' => $remote->getIpAddress(), + 'user_agent' => filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), + 'comment_type' => 'comment', + 'comment_author' => $comment->author, 'comment_author_email' => $comment->contact, - 'comment_content' => $comment->content, - )); - } - - /** - * @return \RbComment\Model\CommentTable - */ - public function getCommentTable() - { - if (!$this->commentTable) { - $sm = $this->getServiceLocator(); - $this->commentTable = $sm->get('RbComment\Model\CommentTable'); - } - - return $this->commentTable; - } - - /** - * @return \ZendService\Akismet\Akismet - */ - public function getAkismetService() - { - if (!$this->akismetService) { - $sm = $this->getServiceLocator(); - $this->akismetService = $sm->get('RbComment\Akismet'); - } + 'comment_content' => $comment->content, + ]; - return $this->akismetService; + return $this->akismetService->isSpam($content); } } diff --git a/src/RbComment/Controller/ConsoleController.php b/src/RbComment/Controller/ConsoleController.php index 9da1cb7..2f858dd 100644 --- a/src/RbComment/Controller/ConsoleController.php +++ b/src/RbComment/Controller/ConsoleController.php @@ -1,35 +1,24 @@ getCommentTable()->deleteSpam(); - - $console = Console::getInstance(); - $console->writeLine($deleted . ' spam comments removed'); + $this->commentTable = $commentTable; } - /** - * @return \RbComment\Model\CommentTable - */ - public function getCommentTable() + public function deleteSpamAction() { - if (!$this->commentTable) { - $sm = $this->getServiceLocator(); - $this->commentTable = $sm->get('RbComment\Model\CommentTable'); - } + $deleted = $this->commentTable->deleteSpam(); - return $this->commentTable; + $console = Console::getInstance(); + $console->writeLine($deleted . ' spam comments removed'); } -} \ No newline at end of file +} diff --git a/src/RbComment/Factory/Controller/CommentControllerFactory.php b/src/RbComment/Factory/Controller/CommentControllerFactory.php new file mode 100644 index 0000000..597ab38 --- /dev/null +++ b/src/RbComment/Factory/Controller/CommentControllerFactory.php @@ -0,0 +1,28 @@ +get('Config'), + $container->get(CommentTable::class), + $container->get('RbComment\Akismet') + ); + + return $controller; + } +} diff --git a/src/RbComment/Factory/Controller/ConsoleControllerFactory.php b/src/RbComment/Factory/Controller/ConsoleControllerFactory.php new file mode 100644 index 0000000..fa6ded9 --- /dev/null +++ b/src/RbComment/Factory/Controller/ConsoleControllerFactory.php @@ -0,0 +1,26 @@ +get(CommentTable::class) + ); + + return $controller; + } +} diff --git a/src/RbComment/Factory/Model/CommentTableFactory.php b/src/RbComment/Factory/Model/CommentTableFactory.php new file mode 100644 index 0000000..2be32d5 --- /dev/null +++ b/src/RbComment/Factory/Model/CommentTableFactory.php @@ -0,0 +1,23 @@ +get('RbCommentTableGateway') + ); + } +} diff --git a/src/RbComment/Factory/Model/RbCommentTableGatewayFactory.php b/src/RbComment/Factory/Model/RbCommentTableGatewayFactory.php new file mode 100644 index 0000000..a431167 --- /dev/null +++ b/src/RbComment/Factory/Model/RbCommentTableGatewayFactory.php @@ -0,0 +1,30 @@ +get(Adapter::class); + + $resultSetPrototype = new ResultSet(); + $resultSetPrototype->setArrayObjectPrototype(new Comment()); + + return new TableGateway('rb_comments', $databaseAdapter, null, $resultSetPrototype); + } +} diff --git a/src/RbComment/Factory/Mvc/Controller/Plugin/MailerFactory.php b/src/RbComment/Factory/Mvc/Controller/Plugin/MailerFactory.php new file mode 100644 index 0000000..bfaf531 --- /dev/null +++ b/src/RbComment/Factory/Mvc/Controller/Plugin/MailerFactory.php @@ -0,0 +1,25 @@ +get('ViewHelperManager')->get('serverUrl'), + $container->get('RbComment\Mailer'), + $container->get('Config') + ); + } +} diff --git a/src/RbComment/Factory/Service/AkismetServiceFactory.php b/src/RbComment/Factory/Service/AkismetServiceFactory.php new file mode 100644 index 0000000..a28d426 --- /dev/null +++ b/src/RbComment/Factory/Service/AkismetServiceFactory.php @@ -0,0 +1,29 @@ +get('Config'); + $viewHelperManager = $container->get('ViewHelperManager'); + + $akismetConfig = $configService['rb_comment']['akismet']; + + return new Akismet( + $akismetConfig['api_key'], + $viewHelperManager->get('serverUrl')->__invoke() + ); + } +} diff --git a/src/RbComment/Factory/View/Helper/CommentFactory.php b/src/RbComment/Factory/View/Helper/CommentFactory.php new file mode 100644 index 0000000..4a1ca7a --- /dev/null +++ b/src/RbComment/Factory/View/Helper/CommentFactory.php @@ -0,0 +1,27 @@ +get('ViewHelperManager'), + $container->get('Router'), + $container->get('Config'), + $container->get(CommentTable::class) + ); + } +} diff --git a/src/RbComment/Form/CommentForm.php b/src/RbComment/Form/CommentForm.php index a5bccec..5abe7df 100644 --- a/src/RbComment/Form/CommentForm.php +++ b/src/RbComment/Form/CommentForm.php @@ -1,5 +1,4 @@ setAttributes(array( + $this->setAttributes([ 'method' => 'post', 'action' => '/rbcomment/add', - )); + ]); - $this->add(array( + $this->add([ 'type' => 'Csrf', 'name' => 'csrf', - )); + ]); - $this->add(array( + $this->add([ 'name' => 'id', - 'attributes' => array( + 'attributes' => [ 'type' => 'hidden', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'thread', - 'attributes' => array( + 'attributes' => [ 'type' => 'hidden', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'uri', - 'attributes' => array( + 'attributes' => [ 'type' => 'hidden', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'author', - 'attributes' => array( - 'type' => 'text', + 'attributes' => [ + 'type' => 'text', 'placeholder' => $strings['author'], - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'contact', - 'attributes' => array( - 'type' => 'text', + 'attributes' => [ + 'type' => 'text', 'placeholder' => $strings['contact'], - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'type' => 'Textarea', 'name' => 'content', - 'attributes' => array( + 'attributes' => [ 'placeholder' => $strings['content'], - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'submit', - 'attributes' => array( + 'attributes' => [ 'type' => 'submit', 'value' => $strings['submit'], - 'id' => 'submitbutton', - ), - )); + 'id' => 'submitbutton', + ], + ]); } } diff --git a/src/RbComment/Model/Comment.php b/src/RbComment/Model/Comment.php index 17d9871..a48f779 100644 --- a/src/RbComment/Model/Comment.php +++ b/src/RbComment/Model/Comment.php @@ -1,5 +1,4 @@ add($factory->createInput(array( + $inputFilter->add($factory->createInput([ 'name' => 'id', 'required' => true, - 'filters' => array( - array('name' => 'Int'), - ), - ))); + 'filters' => [ + ['name' => 'Int'], + ], + ])); - $inputFilter->add($factory->createInput(array( + $inputFilter->add($factory->createInput([ 'name' => 'thread', 'required' => true, - 'filters' => array( - array('name' => 'Alnum'), - ), - ))); + 'filters' => [ + ['name' => 'Alnum'], + ], + ])); - $inputFilter->add($factory->createInput(array( + $inputFilter->add($factory->createInput([ 'name' => 'author', 'required' => true, - 'filters' => array( - array('name' => 'StripTags'), - array('name' => 'StringTrim'), - ), - 'validators' => array( - array( + 'filters' => [ + ['name' => 'StripTags'], + ['name' => 'StringTrim'], + ], + 'validators' => [ + [ 'name' => 'StringLength', - 'options' => array( + 'options' => [ 'encoding' => 'UTF-8', 'min' => 1, 'max' => 150, - ), - ), - ), - ))); + ], + ], + ], + ])); - $inputFilter->add($factory->createInput(array( + $inputFilter->add($factory->createInput([ 'name' => 'contact', 'required' => true, - 'filters' => array( - array('name' => 'StripTags'), - array('name' => 'StringTrim'), - ), - 'validators' => array( - array( + 'filters' => [ + ['name' => 'StripTags'], + ['name' => 'StringTrim'], + ], + 'validators' => [ + [ 'name' => 'EmailAddress', - ), - array( + ], + [ 'name' => 'StringLength', - 'options' => array( + 'options' => [ 'encoding' => 'UTF-8', 'min' => 1, 'max' => 200, - ), - ), - ), - ))); + ], + ], + ], + ])); - $inputFilter->add($factory->createInput(array( + $inputFilter->add($factory->createInput([ 'name' => 'content', 'required' => true, - 'filters' => array( - array('name' => 'StripTags'), - array('name' => 'StringTrim'), - ), - ))); + 'filters' => [ + ['name' => 'StripTags'], + ['name' => 'StringTrim'], + ], + ])); $this->inputFilter = $inputFilter; } diff --git a/src/RbComment/Model/CommentTable.php b/src/RbComment/Model/CommentTable.php index cfdfba8..faee674 100644 --- a/src/RbComment/Model/CommentTable.php +++ b/src/RbComment/Model/CommentTable.php @@ -1,5 +1,4 @@ tableGateway->select(); - - return $resultSet; + return $this->tableGateway->select(); } /** * Returns all the comments of a thread. * - * @param string $thread + * @param string $thread + * * @return ResultSet */ public function fetchAllForThread($thread) { + $columns = [ + 'id', + 'author', + 'content', + 'contact', + 'published_on_raw' => 'published_on', + 'published_on' => new Expression("DATE_FORMAT(published_on, '%M %d, %Y %H:%i')"), + ]; + $select = new Select($this->tableGateway->getTable()); - $select->columns(array('id', 'author', 'content', 'contact', - 'published_on_raw' => 'published_on', - 'published_on' => new Expression("DATE_FORMAT(published_on, '%M %d, %Y %H:%i')"))) - ->where(array('thread' => $thread, 'visible' => 1)) + $select->columns($columns) + ->where(['thread' => $thread, 'visible' => 1]) ->order('published_on_raw ASC'); $resultSet = $this->tableGateway->selectWith($select); @@ -54,15 +59,23 @@ public function fetchAllForThread($thread) * Returns all the comments pending approval for a thread. * * @param string $thread + * * @return ResultSet */ public function fetchAllPendingForThread($thread) { + $columns = [ + 'id', + 'author', + 'content', + 'contact', + 'published_on_raw' => 'published_on', + 'published_on' => new Expression("DATE_FORMAT(published_on, '%M %d, %Y %H:%i')"), + ]; + $select = new Select($this->tableGateway->getTable()); - $select->columns(array('id', 'author', 'content', 'contact', - 'published_on_raw' => 'published_on', - 'published_on' => new Expression("DATE_FORMAT(published_on, '%M %d, %Y %H:%i')"))) - ->where(array('thread' => $thread, 'visible' => 0)) + $select->columns($columns) + ->where(['thread' => $thread, 'visible' => 0]) ->order('published_on_raw ASC'); $resultSet = $this->tableGateway->selectWith($select); @@ -73,10 +86,11 @@ public function fetchAllPendingForThread($thread) /** * Allow custom comments selection. * - * @param \Zend\Db\Sql\Select $select + * @param Select $select + * * @return ResultSet */ - public function fetchAllUsingSelect(\Zend\Db\Sql\Select $select) + public function fetchAllUsingSelect(Select $select) { return $this->tableGateway->selectWith($select); } @@ -84,14 +98,15 @@ public function fetchAllUsingSelect(\Zend\Db\Sql\Select $select) /** * Returns a comment by id. * - * @param int $id - * @return \RbComment\Model\Comment + * @param int $id + * + * @return Comment */ public function getComment($id) { - $id = (int) $id; - $rowset = $this->tableGateway->select(array('id' => $id)); - $row = $rowset->current(); + $id = (int)$id; + $rowset = $this->tableGateway->select(['id' => $id]); + $row = $rowset->current(); return $row; } @@ -99,28 +114,29 @@ public function getComment($id) /** * Saves a comment into the database. * - * @param \RbComment\Model\Comment $comment - * @return int The id of the inserted/updated comment + * @param Comment $comment + * + * @return int The id of the inserted/updated comment */ public function saveComment(Comment $comment) { - $data = array( - 'thread' => $comment->thread, - 'uri' => $comment->uri, - 'author' => $comment->author, + $data = [ + 'thread' => $comment->thread, + 'uri' => $comment->uri, + 'author' => $comment->author, 'contact' => $comment->contact, 'content' => $comment->content, 'visible' => $comment->visible, - 'spam' => $comment->spam, - ); + 'spam' => $comment->spam, + ]; - $id = (int) $comment->id; + $id = (int)$comment->id; if ($id === 0) { $this->tableGateway->insert($data); $id = $this->tableGateway->lastInsertValue; } else { if ($this->getComment($id)) { - $this->tableGateway->update($data, array('id' => $id)); + $this->tableGateway->update($data, ['id' => $id]); } } @@ -134,7 +150,7 @@ public function saveComment(Comment $comment) */ public function deleteComment($id) { - $this->tableGateway->delete(array('id' => $id)); + $this->tableGateway->delete(['id' => $id]); } @@ -145,6 +161,6 @@ public function deleteComment($id) */ public function deleteSpam() { - return $this->tableGateway->delete(array('spam' => 1)); + return $this->tableGateway->delete(['spam' => 1]); } } diff --git a/src/RbComment/Mvc/Controller/Plugin/Mailer.php b/src/RbComment/Mvc/Controller/Plugin/Mailer.php index cf7b5d7..092d97e 100755 --- a/src/RbComment/Mvc/Controller/Plugin/Mailer.php +++ b/src/RbComment/Mvc/Controller/Plugin/Mailer.php @@ -1,52 +1,43 @@ serviceLocator = $serviceLocator; - - return $this; - } - - public function getServiceLocator() - { - return $this->serviceLocator; + private $serverUrlHelper; + private $mailerService; + private $configService; + + public function __construct( + $serverUrlHelper, + $mailerService, + array $configService + ) { + $this->serverUrlHelper = $serverUrlHelper; + $this->mailerService = $mailerService; + $this->configService = $configService; } public function __invoke($comment) { - $serviceManager = $this->getServiceLocator()->getServiceLocator(); - $viewHelperManager = $serviceManager->get('viewhelpermanager'); - $serverUrlHelper = $viewHelperManager->get('serverUrl'); - - $mailerService = $serviceManager->get('RbComment\Mailer'); - - $config = $serviceManager->get('Config'); - $mailerConfig = $config['rb_comment']['email']; + $mailerConfig = $this->configService['rb_comment']['email']; $htmlContent = $comment->content; $htmlContent .= '

'; - $htmlContent .= '' . - $mailerConfig['context_link_text'] . - ''; + $htmlContent .= + '' . + $mailerConfig['context_link_text'] . + ''; $html = new MimePart($htmlContent); $html->type = "text/html"; $body = new MimeMessage(); - $body->setParts(array($html)); + $body->setParts([$html]); $message = new Message(); $message->addFrom($mailerConfig['from']) @@ -57,6 +48,6 @@ public function __invoke($comment) $message->addTo($mConfig); } - $mailerService->send($message); + $this->mailerService->send($message); } } diff --git a/src/RbComment/View/Helper/Comment.php b/src/RbComment/View/Helper/Comment.php index 5cf1b61..311ebfa 100644 --- a/src/RbComment/View/Helper/Comment.php +++ b/src/RbComment/View/Helper/Comment.php @@ -1,31 +1,33 @@ true, 'uikit' => true, 'bootstrap3' => true, - ); - - public function setServiceLocator(ServiceLocatorInterface $serviceLocator) - { - $this->serviceLocator = $serviceLocator; - - return $this; - } - - public function getServiceLocator() - { - return $this->serviceLocator; + ]; + + private $viewHelperManager; + private $routerService; + private $configService; + private $commentTable; + + public function __construct( + $viewHelperManager, + $routerService, + array $configService, + CommentTable $commentTable + ) { + $this->viewHelperManager = $viewHelperManager; + $this->routerService = $routerService; + $this->configService = $configService; + $this->commentTable = $commentTable; } public function __invoke($theme = 'default') @@ -35,29 +37,24 @@ public function __invoke($theme = 'default') ? 'rbcomment/theme/' . $theme : $theme; - $serviceManager = $this->getServiceLocator()->getServiceLocator(); - $viewHelperManager = $serviceManager->get('viewhelpermanager'); - - $uri = $serviceManager->get('router')->getRequestUri()->getPath(); + $uri = $this->routerService->getRequestUri()->getPath(); $thread = sha1($uri); - $validationMessages = $viewHelperManager->get('flashMessenger') - ->getMessagesFromNamespace('RbComment'); + $validationMessages = $this->viewHelperManager->get('flashMessenger') + ->getMessagesFromNamespace('RbComment'); - $config = $serviceManager->get('Config'); - $strings = $config['rb_comment']['strings']; + $strings = $this->configService['rb_comment']['strings']; - echo $viewHelperManager->get('partial')->__invoke($invokablePartial, array( - 'comments' => $serviceManager->get('RbComment\Model\CommentTable') - ->fetchAllForThread($thread), - 'form' => new \RbComment\Form\CommentForm($strings), - 'thread' => $thread, + echo $this->viewHelperManager->get('partial')->__invoke($invokablePartial, [ + 'comments' => $this->commentTable->fetchAllForThread($thread), + 'form' => new CommentForm($strings), + 'thread' => $thread, 'validationResults' => count($validationMessages) > 0 ? json_decode(array_shift($validationMessages)) - : array(), - 'uri' => $uri, - 'strings' => $strings, - 'zfc_user'=> $config['rb_comment']['zfc_user']['enabled'], - 'gravatar'=> $config['rb_comment']['gravatar']['enabled'], - )); + : [], + 'uri' => $uri, + 'strings' => $strings, + 'zfc_user' => $this->configService['rb_comment']['zfc_user']['enabled'], + 'gravatar' => $this->configService['rb_comment']['gravatar']['enabled'], + ]); } } diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 9a4041e..e5de7f0 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -1,5 +1,4 @@ array( - 'strings' => array( + protected $configMock = [ + 'rb_comment' => [ + 'strings' => [ 'author' => 'author', 'contact' => 'contact', 'content' => 'content', 'submit' => 'submit', - ), - ), - ); + ], + ], + ]; protected $requestMock; - protected $serviceLocatorMock; + protected $commentTableMock; + protected $akismetServiceMock; public function setUp() { - $this->serviceLocatorMock = $this->getMock( - 'Zend\ServiceManager\ServiceLocatorInterface', - array('get', 'has'), - array(), - 'ServiceLocatorInterface' - ); - - $this->requestMock = $this->getMock( - 'Zend\Http\Request', - array('isPost', 'getPost'), - array(), - '', - false - ); + $this->serviceLocatorMock = $this->createMock(ServiceLocatorInterface::class); + $this->requestMock = $this->createMock(Request::class); + $this->commentTableMock = $this->createMock(CommentTable::class); + $this->akismetServiceMock = $this->createMock(Akismet::class); // Global values $_SERVER['HTTP_USER_AGENT'] = 'RbComment Testing Suite'; @@ -48,33 +42,24 @@ public function setUp() public function testAddActionOnlyWorksWithPostMethod() { - // ServiceLocator Mock Setup - $this->serviceLocatorMock->expects($this->once()) - ->method('get') - ->with('Config') - ->will($this->returnValue($this->configMock)); - - // Request Mock Setup + // Mocks $this->requestMock->expects($this->once()) - ->method('isPost') - ->will($this->returnValue(false)); - - // CommentController Mock - $commentControllerMock = $this->getMock( - 'RbComment\Controller\CommentController', - array('getRequest', 'getServiceLocator'), - array(), - '', - false - ); - - $commentControllerMock->expects($this->once()) - ->method('getServiceLocator') - ->will($this->returnValue($this->serviceLocatorMock)); + ->method('isPost') + ->will($this->returnValue(false)); + + $commentControllerMock = + $this->getMockBuilder(CommentController::class) + ->setConstructorArgs([ + $this->configMock, + $this->commentTableMock, + $this->akismetServiceMock + ]) + ->setMethods(['getRequest']) + ->getMock(); $commentControllerMock->expects($this->once()) ->method('getRequest') - ->will($this->returnValue($this->requestMock)); + ->willReturn($this->requestMock); $commentControllerMock->addAction(); } @@ -82,65 +67,45 @@ public function testAddActionOnlyWorksWithPostMethod() public function testAddActionLogsFormErrorsIntoTheRbCommentNamespace() { //'contact' key is missing on purpose - $postMock = array( - 'author' => 'Tester', + $postMock = [ + 'author' => 'Tester', 'content' => 'test', - 'uri' => '/test', - ); - - // ServiceLocator Mock Setup - $this->serviceLocatorMock->expects($this->once()) - ->method('get') - ->with('Config') - ->will($this->returnValue($this->configMock)); + 'uri' => '/test', + ]; // Request Mock Setup $this->requestMock->expects($this->once()) - ->method('isPost') - ->will($this->returnValue(true)); + ->method('isPost') + ->will($this->returnValue(true)); $this->requestMock->expects($this->once()) - ->method('getPost') - ->will($this->returnValue($postMock)); + ->method('getPost') + ->will($this->returnValue($postMock)); // FlashMessenger Mock - $flashMessengerMock = $this->getMock( - 'Zend\Mvc\Controller\Plugin\FlashMessenger', - array('setNamespace', 'addMessage'), - array(), - '', - false - ); + $flashMessengerMock = $this->createMock(FlashMessenger::class); $flashMessengerMock->expects($this->once()) ->method('setNamespace') ->with('RbComment'); // Redirect Mock - $redirectMock = $this->getMock( - 'Zend\Mvc\Controller\Plugin\Redirect', - array('toUrl'), - array(), - '', - false - ); + $redirectMock = $this->createMock(Redirect::class); $redirectMock->expects($this->once()) ->method('toUrl') ->with($postMock['uri'] . '#rbcomment'); // CommentController Mock - $commentControllerMock = $this->getMock( - 'RbComment\Controller\CommentController', - array('getRequest', 'getServiceLocator', 'flashMessenger', 'redirect'), - array(), - '', - false - ); - - $commentControllerMock->expects($this->once()) - ->method('getServiceLocator') - ->will($this->returnValue($this->serviceLocatorMock)); + $commentControllerMock = + $this->getMockBuilder(CommentController::class) + ->setConstructorArgs([ + $this->configMock, + $this->commentTableMock, + $this->akismetServiceMock + ]) + ->setMethods(['getRequest', 'flashMessenger', 'redirect']) + ->getMock(); $commentControllerMock->expects($this->once()) ->method('getRequest') @@ -162,107 +127,66 @@ public function testAddActionLogsFormErrorsIntoTheRbCommentNamespace() */ public function testIsSpam($comment, $isSpam) { - $rbCommentConfig = (object) array( - 'akismet' => array( - 'proxy' => array( - 'use' => false, - 'trusted' => array(), - 'header' => '', - ), - ), - ); - - $akismetServiceMock = $this->getMock( - 'ZendService\Akismet\Akismet', - array('isSpam'), - array(), - '', - false - ); + $rbCommentConfig = (object) [ + 'akismet' => [ + 'proxy' => [ + 'use' => false, + 'trusted' => [], + 'header' => '', + ], + ], + ]; + + $akismetServiceMock = + $this->getMockBuilder(Akismet::class) + ->disableOriginalConstructor() + ->setMethods(['isSpam']) + ->getMock(); $akismetServiceMock->expects($this->once()) ->method('isSpam') ->will($this->returnValue($isSpam)); - $this->serviceLocatorMock->expects($this->once()) - ->method('get') - ->with('RbComment\Akismet') - ->will($this->returnValue($akismetServiceMock)); - $commentControllerReflection = new ReflectionClass('RbComment\Controller\CommentController'); $isSpamReflection = $commentControllerReflection->getMethod('isSpam'); $isSpamReflection->setAccessible(true); - $commentController = new CommentController(); - $commentController->setServiceLocator($this->serviceLocatorMock); + $commentController = new CommentController( + $this->configMock, + $this->commentTableMock, + $akismetServiceMock + ); $this->assertEquals($isSpam, $isSpamReflection->invoke($commentController, $comment, $rbCommentConfig)); } + /** + * @return array + */ public static function isSpamDataProvider() { - return array( - array( + return [ + [ // comment - (object) array( - 'author' => 'not a spammer', + (object) [ + 'author' => 'not a spammer', 'contact' => 'me@me.com', 'content' => 'test', - ), + ], // isSpam false, - ), - array( + ], + [ // comment - (object) array( - 'author' => 'spammer', + (object) [ + 'author' => 'spammer', 'contact' => 'spam@spamfactory.com', 'content' => 'spam', - ), + ], // isSpam true, - ), - ); - } - - public function testGetCommentTableReturnsAnInstanceOfCommentTable() - { - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - array(), - array(), - '', - false - ); - - $commentTable = new CommentTable($tableGatewayMock); - - $this->serviceLocatorMock->expects($this->once()) - ->method('get') - ->with('RbComment\Model\CommentTable') - ->will($this->returnValue($commentTable)); - - $commentController = new CommentController(); - $commentController->setServiceLocator($this->serviceLocatorMock); - - $this->assertEquals($commentTable, $commentController->getCommentTable()); - $this->assertInstanceOf('RbComment\Model\CommentTable', $commentController->getCommentTable()); - } - - public function testGetAkismetServiceReturnsAnInstanceOfAkismet() - { - $akismetService = new Akismet('test', 'test'); - - $this->serviceLocatorMock->expects($this->once()) - ->method('get') - ->with('RbComment\Akismet') - ->will($this->returnValue($akismetService)); - - $commentController = new CommentController(); - $commentController->setServiceLocator($this->serviceLocatorMock); - - $this->assertEquals($akismetService, $commentController->getAkismetService()); - $this->assertInstanceOf('ZendService\Akismet\Akismet', $commentController->getAkismetService()); + ], + ]; } } diff --git a/tests/RbCommentTest/Controller/ConsoleControllerTest.php b/tests/RbCommentTest/Controller/ConsoleControllerTest.php index 97523f9..312609f 100644 --- a/tests/RbCommentTest/Controller/ConsoleControllerTest.php +++ b/tests/RbCommentTest/Controller/ConsoleControllerTest.php @@ -1,56 +1,33 @@ serviceLocatorMock = $this->getMock( - 'Zend\ServiceManager\ServiceLocatorInterface', - array('get', 'has'), - array(), - 'ServiceLocatorInterface' - ); + $this->commentTableMock = $this->createMock(CommentTable::class); } + /** + * @group controller + */ public function testDeleteSpamAction() { $deletedCount = rand(1, 100); - $commentTableMock = $this->getMock('RbComment\Model\CommentTable', array( - 'deleteSpam' - ), array(), '', false); - // Expect this to be called once - $commentTableMock->expects($this->once()) - ->method('deleteSpam') - ->will($this->returnValue($deletedCount)); - - // ServiceLocator Mock Setup - $this->serviceLocatorMock->expects($this->once()) - ->method('get') - ->with('RbComment\Model\CommentTable') - ->will($this->returnValue($commentTableMock)); + $this->commentTableMock->expects($this->once()) + ->method('deleteSpam') + ->will($this->returnValue($deletedCount)); // CommentController Mock - $consoleControllerMock = $this->getMock( - 'RbComment\Controller\ConsoleController', - array('getServiceLocator'), - array(), - '', - false - ); - - $consoleControllerMock->expects($this->once()) - ->method('getServiceLocator') - ->will($this->returnValue($this->serviceLocatorMock)); + $consoleControllerMock = new ConsoleController($this->commentTableMock); // Capture output ob_start(); @@ -59,28 +36,4 @@ public function testDeleteSpamAction() $this->assertEquals($output, $deletedCount . ' spam comments removed' . PHP_EOL); } - - public function testGetCommentTableReturnsAnInstanceOfCommentTable() - { - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - array(), - array(), - '', - false - ); - - $commentTable = new CommentTable($tableGatewayMock); - - $this->serviceLocatorMock->expects($this->once()) - ->method('get') - ->with('RbComment\Model\CommentTable') - ->will($this->returnValue($commentTable)); - - $commentController = new CommentController(); - $commentController->setServiceLocator($this->serviceLocatorMock); - - $this->assertEquals($commentTable, $commentController->getCommentTable()); - $this->assertInstanceOf('RbComment\Model\CommentTable', $commentController->getCommentTable()); - } -} \ No newline at end of file +} diff --git a/tests/RbCommentTest/Factory/Controller/CommentControllerFactoryTest.php b/tests/RbCommentTest/Factory/Controller/CommentControllerFactoryTest.php new file mode 100644 index 0000000..cb97dbe --- /dev/null +++ b/tests/RbCommentTest/Factory/Controller/CommentControllerFactoryTest.php @@ -0,0 +1,45 @@ +createMock(ContainerInterface::class); + + $configServiceMock = [uniqid()]; + $commentTableMock = $this->createMock(CommentTable::class); + $akismetServiceMock = $this->createMock(Akismet::class); + + $commentControllerFactory = new CommentControllerFactory(); + + // Expectations + $containerMock->expects($this->exactly(3)) + ->method('get') + ->withConsecutive( + ['Config'], + [CommentTable::class], + ['RbComment\Akismet'] + ) + ->willReturnOnConsecutiveCalls( + $configServiceMock, + $commentTableMock, + $akismetServiceMock + ); + + $controller = $commentControllerFactory($containerMock, CommentController::class); + + // Assertions + $this->assertInstanceOf(CommentController::class, $controller); + } +} diff --git a/tests/RbCommentTest/Factory/Controller/ConsoleControllerFactoryTest.php b/tests/RbCommentTest/Factory/Controller/ConsoleControllerFactoryTest.php new file mode 100644 index 0000000..50ebb9d --- /dev/null +++ b/tests/RbCommentTest/Factory/Controller/ConsoleControllerFactoryTest.php @@ -0,0 +1,33 @@ +createMock(ContainerInterface::class); + $commentTableMock = $this->createMock(CommentTable::class); + + $consoleControllerFactory = new ConsoleControllerFactory(); + + // Expectations + $containerMock->expects($this->once()) + ->method('get') + ->with(CommentTable::class) + ->willReturn($commentTableMock); + + $controller = $consoleControllerFactory($containerMock, CommentController::class); + + // Assertions + $this->assertInstanceOf(ConsoleController::class, $controller); + } +} diff --git a/tests/RbCommentTest/Factory/Model/CommentTableFactoryTest.php b/tests/RbCommentTest/Factory/Model/CommentTableFactoryTest.php new file mode 100644 index 0000000..6ef103f --- /dev/null +++ b/tests/RbCommentTest/Factory/Model/CommentTableFactoryTest.php @@ -0,0 +1,33 @@ +createMock(ContainerInterface::class); + $rbCommentTableGatewayMock = $this->createMock(TableGateway::class); + + $commentTableFactory = new CommentTableFactory(); + + // Expectations + $containerMock->expects($this->once()) + ->method('get') + ->with('RbCommentTableGateway') + ->willReturn($rbCommentTableGatewayMock); + + $table = $commentTableFactory($containerMock, CommentController::class); + + // Assertions + $this->assertInstanceOf(CommentTable::class, $table); + } +} diff --git a/tests/RbCommentTest/Factory/Model/RbCommentTableGatewayFactoryTest.php b/tests/RbCommentTest/Factory/Model/RbCommentTableGatewayFactoryTest.php new file mode 100644 index 0000000..4d1310a --- /dev/null +++ b/tests/RbCommentTest/Factory/Model/RbCommentTableGatewayFactoryTest.php @@ -0,0 +1,33 @@ +createMock(ContainerInterface::class); + $adapterMock = $this->createMock(Adapter::class); + + $factory = new RbCommentTableGatewayFactory(); + + // Expectations + $containerMock->expects($this->once()) + ->method('get') + ->with(Adapter::class) + ->willReturn($adapterMock); + + $table = $factory($containerMock, 'RbCommentTableGateway'); + + // Assertions + $this->assertInstanceOf(TableGateway::class, $table); + } +} diff --git a/tests/RbCommentTest/Factory/Mvc/Controller/Plugin/MailerFactoryTest.php b/tests/RbCommentTest/Factory/Mvc/Controller/Plugin/MailerFactoryTest.php new file mode 100644 index 0000000..56f6ab4 --- /dev/null +++ b/tests/RbCommentTest/Factory/Mvc/Controller/Plugin/MailerFactoryTest.php @@ -0,0 +1,54 @@ +createMock(ContainerInterface::class); + + $viewHelperManagerMock = + $this->getMockBuilder('ViewHelperManager') + ->setMethods(['get']) + ->getMock(); + $mailerMock = $this->createMock(Sendmail::class); + $configMock = [uniqid()]; + + $serverUrlMock = uniqid(); + + $factory = new MailerFactory(); + + // Expectations + $containerMock->expects($this->exactly(3)) + ->method('get') + ->withConsecutive( + ['ViewHelperManager'], + ['RbComment\Mailer'], + ['Config'] + ) + ->willReturnOnConsecutiveCalls( + $viewHelperManagerMock, + $mailerMock, + $configMock + ); + + $viewHelperManagerMock->expects($this->once()) + ->method('get') + ->with('serverUrl') + ->willReturn($serverUrlMock); + + $table = $factory($containerMock, Mailer::class); + + // Assertions + $this->assertInstanceOf(Mailer::class, $table); + } +} diff --git a/tests/RbCommentTest/Factory/Service/AkismetServiceFactoryTest.php b/tests/RbCommentTest/Factory/Service/AkismetServiceFactoryTest.php new file mode 100644 index 0000000..594f258 --- /dev/null +++ b/tests/RbCommentTest/Factory/Service/AkismetServiceFactoryTest.php @@ -0,0 +1,65 @@ +createMock(ContainerInterface::class); + + $viewHelperManagerMock = + $this->getMockBuilder('ViewHelperManager') + ->setMethods(['get']) + ->getMock(); + + $serverUrlMock = + $this->getMockBuilder(ServerUrl::class) + ->setMethods(['__invoke']) + ->getMock(); + + $configMock = [ + 'rb_comment' => [ + 'akismet' => [ + 'api_key' => uniqid(), + ], + ], + ]; + + $factory = new AkismetServiceFactory(); + + // Expectations + $containerMock->expects($this->exactly(2)) + ->method('get') + ->withConsecutive( + ['Config'], + ['ViewHelperManager'] + ) + ->willReturnOnConsecutiveCalls( + $configMock, + $viewHelperManagerMock + ); + + $viewHelperManagerMock->expects($this->once()) + ->method('get') + ->with('serverUrl') + ->willReturn($serverUrlMock); + + $serverUrlMock->expects($this->once()) + ->method('__invoke') + ->willReturn('http://test.com'); + + $table = $factory($containerMock, Akismet::class); + + // Assertions + $this->assertInstanceOf(Akismet::class, $table); + } +} diff --git a/tests/RbCommentTest/Factory/View/Helper/CommentFactoryTest.php b/tests/RbCommentTest/Factory/View/Helper/CommentFactoryTest.php new file mode 100644 index 0000000..03cb059 --- /dev/null +++ b/tests/RbCommentTest/Factory/View/Helper/CommentFactoryTest.php @@ -0,0 +1,53 @@ +createMock(ContainerInterface::class); + + $viewHelperManagerMock = + $this->getMockBuilder('ViewHelperManager') + ->setMethods(['get']) + ->getMock(); + + $routerMock = $this->getMockBuilder('Router')->getMock(); + + $configMock = [uniqid()]; + + $commentTableMock = $this->createMock(CommentTable::class); + + $factory = new CommentFactory(); + + // Expectations + $containerMock->expects($this->exactly(4)) + ->method('get') + ->withConsecutive( + ['ViewHelperManager'], + ['Router'], + ['Config'], + [CommentTable::class] + ) + ->willReturnOnConsecutiveCalls( + $viewHelperManagerMock, + $routerMock, + $configMock, + $commentTableMock + ); + + $table = $factory($containerMock, Comment::class); + + // Assertions + $this->assertInstanceOf(Comment::class, $table); + } +} diff --git a/tests/RbCommentTest/Form/CommentFormTest.php b/tests/RbCommentTest/Form/CommentFormTest.php index 5505636..2e7ecf7 100644 --- a/tests/RbCommentTest/Form/CommentFormTest.php +++ b/tests/RbCommentTest/Form/CommentFormTest.php @@ -1,5 +1,4 @@ 'author', 'contact' => 'contact', 'content' => 'content', 'submit' => 'submit', - ); + ]; public function testFormConstructorCreatesAllItems() { diff --git a/tests/RbCommentTest/Model/CommentTableTest.php b/tests/RbCommentTest/Model/CommentTableTest.php index 502e862..7ba2308 100644 --- a/tests/RbCommentTest/Model/CommentTableTest.php +++ b/tests/RbCommentTest/Model/CommentTableTest.php @@ -1,5 +1,4 @@ getMock( - 'Zend\Db\TableGateway\TableGateway', - array(), - array(), - 'TableGateway', - false - ); + private $tableGatewayMock; - $commentTable = new CommentTable($tableGatewayMock); + public function setUp() + { + $this->tableGatewayMock = $this->createMock(TableGateway::class); + } - $reflectedCommentTable = new ReflectionClass($commentTable); - $tableGatewayReflectionProperty = $reflectedCommentTable->getProperty('tableGateway'); - $tableGatewayReflectionProperty->setAccessible(true); + /** + * @group table + */ + public function testConstructorStoresDependencies() + { + $commentTable = new CommentTable($this->tableGatewayMock); - $this->assertInstanceOf( - 'Zend\Db\TableGateway\TableGateway', - $tableGatewayReflectionProperty->getValue($commentTable) - ); + // Assertions + $this->assertAttributeEquals($this->tableGatewayMock, 'tableGateway', $commentTable); } + /** + * @group table + */ public function testFetchAllReturnsAllComments() { - $resultSet = new ResultSet(); - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - array('select'), - array(), - '', - false - ); - $tableGatewayMock->expects($this->once()) - ->method('select') - ->will($this->returnValue($resultSet)); + $resultSet = new ResultSet(); + + $this->tableGatewayMock->expects($this->once()) + ->method('select') + ->willReturn($resultSet); - $commentTable = new CommentTable($tableGatewayMock); + $commentTable = new CommentTable($this->tableGatewayMock); $this->assertSame($resultSet, $commentTable->fetchAll()); } + /** + * @group table + */ public function testFetchAllForThreadReturnsAllTheCommentsInThread() { - $resultSet = new ResultSet(); - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - array('selectWith'), - array(), - '', - false - ); + $resultSet = new ResultSet(); - $tableGatewayMock->expects($this->once()) - ->method('selectWith') - ->will($this->returnValue($resultSet)); + $this->tableGatewayMock->expects($this->once()) + ->method('selectWith') + ->willReturn($resultSet); - $commentTable = new CommentTable($tableGatewayMock); + $commentTable = new CommentTable($this->tableGatewayMock); $this->assertSame($resultSet, $commentTable->fetchAllForThread('test')); } + /** + * @group table + */ public function testFetchAllPendingForThreadReturnsAllThePendingCommentsInAThread() { - $resultSet = new ResultSet(); - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - array('selectWith'), - array(), - '', - false - ); + $resultSet = new ResultSet(); - $tableGatewayMock->expects($this->once()) - ->method('selectWith') - ->will($this->returnValue($resultSet)); + $this->tableGatewayMock->expects($this->once()) + ->method('selectWith') + ->willReturn($resultSet); - $commentTable = new CommentTable($tableGatewayMock); + $commentTable = new CommentTable($this->tableGatewayMock); $this->assertSame($resultSet, $commentTable->fetchAllPendingForThread('test')); } + /** + * @group table + */ public function testFetchAllUsingSelectUsesTheCustomSelectAndReturnsTheResult() { - $tableGatewayMock = - $this->getMockBuilder('Zend\Db\TableGateway\TableGateway') - ->setMethods(array('selectWith')) - ->disableOriginalConstructor() - ->getMock(); - $resultSetMock = new ResultSet(); - $selectMock = new Select(); + $selectMock = new Select(); - $tableGatewayMock->expects($this->once()) - ->method('selectWith') - ->with($selectMock) - ->will($this->returnValue($resultSetMock)); + $this->tableGatewayMock->expects($this->once()) + ->method('selectWith') + ->with($selectMock) + ->willReturn($resultSetMock); - $commentTableMock = new CommentTable($tableGatewayMock); + $commentTableMock = new CommentTable($this->tableGatewayMock); $this->assertSame( $resultSetMock, @@ -116,154 +97,134 @@ public function testFetchAllUsingSelectUsesTheCustomSelectAndReturnsTheResult() ); } + /** + * @group table + */ public function testCanRetrieveACommentByItsId() { - $comment = new Comment(); - $comment->exchangeArray(array( - 'id' => 12345, - 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', - 'uri' => '/test', + $commentData = [ + 'id' => 12345, + 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', + 'uri' => '/test', 'author' => 'Robert Boloc', - 'contact' => 'robertboloc@gmail.com', - 'content' => 'Bla bla bla', - 'visible' => 1, - 'spam' => 0, - )); + 'contact' => 'robertboloc@gmail.com', + 'content' => 'Bla bla bla', + 'visible' => 1, + 'spam' => 0, + ]; + + $comment = new Comment(); + $comment->exchangeArray($commentData); $resultSet = new ResultSet(); $resultSet->setArrayObjectPrototype(new Comment()); - $resultSet->initialize(array($comment)); - - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - array('select'), - array(), - '', - false - ); + $resultSet->initialize([$commentData]); - $tableGatewayMock->expects($this->once()) - ->method('select') - ->with(array('id' => 12345)) - ->will($this->returnValue($resultSet)); + $this->tableGatewayMock->expects($this->once()) + ->method('select') + ->with(['id' => 12345]) + ->will($this->returnValue($resultSet)); - $commentTable = new CommentTable($tableGatewayMock); + $commentTable = new CommentTable($this->tableGatewayMock); - $this->assertSame($comment, $commentTable->getComment(12345)); + $this->assertEquals($comment, $commentTable->getComment(12345)); } + /** + * @group table + */ public function testSaveCommentWillInsertNewCommentIfDoesNotAlreadyHaveAnId() { $comment = new Comment(); - $commentData = array( - 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', - 'uri' => '/test', + $commentData = [ + 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', + 'uri' => '/test', 'author' => 'Robert Boloc', - 'contact' => 'robertboloc@gmail.com', - 'content' => 'Bla bla bla', - 'visible' => 1, - 'spam' => 0, - ); + 'contact' => 'robertboloc@gmail.com', + 'content' => 'Bla bla bla', + 'visible' => 1, + 'spam' => 0, + ]; $comment->exchangeArray($commentData); - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - array('insert'), - array(), - '', - false - ); - - $tableGatewayMock->expects($this->once()) - ->method('insert') - ->with($commentData); + $this->tableGatewayMock->expects($this->once()) + ->method('insert') + ->with($commentData); - $commentTable = new CommentTable($tableGatewayMock); + $commentTable = new CommentTable($this->tableGatewayMock); $commentTable->saveComment($comment); } + /** + * @group table + */ public function testSaveCommentWillUpdateExistingCommentIfItAlreadyHasAnId() { $comment = new Comment(); - $commentData = array( - 'id' => 12345, - 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', - 'uri' => '/test', + $commentData = [ + 'id' => 12345, + 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', + 'uri' => '/test', 'author' => 'Robert Boloc', - 'contact' => 'robertboloc@gmail.com', - 'content' => 'Bla bla bla', - 'visible' => 1, - 'spam' => 0, - ); + 'contact' => 'robertboloc@gmail.com', + 'content' => 'Bla bla bla', + 'visible' => 1, + 'spam' => 0, + ]; $comment->exchangeArray($commentData); $resultSet = new ResultSet(); $resultSet->setArrayObjectPrototype(new Comment()); - $resultSet->initialize(array($comment)); - - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - array('select', 'update'), - array(), - '', - false - ); + $resultSet->initialize([$commentData]); + + $this->tableGatewayMock->expects($this->once()) + ->method('select') + ->with(['id' => 12345]) + ->will($this->returnValue($resultSet)); - $tableGatewayMock->expects($this->once()) - ->method('select') - ->with(array('id' => 12345)) - ->will($this->returnValue($resultSet)); - $tableGatewayMock->expects($this->once()) - ->method('update') - ->with(array( - 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', - 'uri' => '/test', + $updateWith = [ + 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', + 'uri' => '/test', 'author' => 'Robert Boloc', - 'contact' => 'robertboloc@gmail.com', - 'content' => 'Bla bla bla', - 'visible' => 1, - 'spam' => 0, - ), array('id' => 12345)); + 'contact' => 'robertboloc@gmail.com', + 'content' => 'Bla bla bla', + 'visible' => 1, + 'spam' => 0, + ]; - $commentTable = new CommentTable($tableGatewayMock); + $this->tableGatewayMock->expects($this->once()) + ->method('update') + ->with($updateWith, ['id' => 12345]); + + $commentTable = new CommentTable($this->tableGatewayMock); $commentTable->saveComment($comment); } + /** + * @group table + */ public function testCanDeleteACommentByItsId() { - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - array('delete'), - array(), - '', - false - ); + $this->tableGatewayMock->expects($this->once()) + ->method('delete') + ->with(['id' => 12345]); - $tableGatewayMock->expects($this->once()) - ->method('delete') - ->with(array('id' => 12345)); - - $commentTable = new CommentTable($tableGatewayMock); + $commentTable = new CommentTable($this->tableGatewayMock); $commentTable->deleteComment(12345); } + /** + * @group table + */ public function testCanDeleteSpam() { - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - array('delete'), - array(), - '', - false - ); - - $tableGatewayMock->expects($this->once()) - ->method('delete') - ->with(array('spam' => 1)); + $this->tableGatewayMock->expects($this->once()) + ->method('delete') + ->with(['spam' => 1]); - $commentTable = new CommentTable($tableGatewayMock); + $commentTable = new CommentTable($this->tableGatewayMock); $commentTable->deleteSpam(); } } diff --git a/tests/RbCommentTest/Model/CommentTest.php b/tests/RbCommentTest/Model/CommentTest.php index ae6f52e..aedfdfe 100644 --- a/tests/RbCommentTest/Model/CommentTest.php +++ b/tests/RbCommentTest/Model/CommentTest.php @@ -1,5 +1,4 @@ testComment = new Comment(); - $this->testArray = array( - 'id' => 1, - 'thread' => 12345, - 'uri' => 'test-uri', - 'author' => 'Robert Boloc', - 'contact' => 'contact@robertboloc.eu', - 'content' => 'This is a test comment', - 'visible' => 1, - 'spam' => 0, + $this->testArray = [ + 'id' => 1, + 'thread' => 12345, + 'uri' => 'test-uri', + 'author' => 'Robert Boloc', + 'contact' => 'contact@robertboloc.eu', + 'content' => 'This is a test comment', + 'visible' => 1, + 'spam' => 0, 'published_on' => 12345678, - ); + ]; } + /** + * @group model + */ public function testCommentInitialState() { $this->assertNull($this->testComment->id); @@ -41,6 +43,9 @@ public function testCommentInitialState() $this->assertNull($this->testComment->published_on); } + /** + * @group model + */ public function testExchangeArrayExchangesTheValues() { $this->testComment->exchangeArray($this->testArray); @@ -56,6 +61,9 @@ public function testExchangeArrayExchangesTheValues() $this->assertEquals($this->testArray['published_on'], $this->testComment->published_on); } + /** + * @group model + */ public function testToArrayReturnsAllTheValues() { $this->testComment->exchangeArray($this->testArray); @@ -74,6 +82,7 @@ public function testToArrayReturnsAllTheValues() } /** + * @group model * @expectedException Exception * @expectedExceptionMessage Not used */ @@ -83,6 +92,7 @@ public function testSetInputFilterThrowsExceptionWhenInvoked() } /** + * @group model * @dataProvider inputFilterDataProvider */ public function testGetInputFilterValidation($data, $validity) @@ -93,25 +103,28 @@ public function testGetInputFilterValidation($data, $validity) $this->assertEquals($inputFilter->isValid(), $validity); } + /** + * @return array + */ public static function inputFilterDataProvider() { - return array( + return [ // False because no data is sent - array( - array(), + [ + [], false, - ), + ], // True because everything is ok - array( - array( - 'id' => 1, - 'thread' => md5('test'), - 'author' => 'Robert Boloc', + [ + [ + 'id' => 1, + 'thread' => md5('test'), + 'author' => 'Robert Boloc', 'contact' => 'robert@test.com', 'content' => 'bla bla bla', - ), + ], true, - ), - ); + ], + ]; } } diff --git a/tests/RbCommentTest/Mvc/Controller/Plugin/MailerTest.php b/tests/RbCommentTest/Mvc/Controller/Plugin/MailerTest.php index 69f5271..e22c425 100644 --- a/tests/RbCommentTest/Mvc/Controller/Plugin/MailerTest.php +++ b/tests/RbCommentTest/Mvc/Controller/Plugin/MailerTest.php @@ -1,24 +1,31 @@ getMock( - 'Zend\ServiceManager\ServiceLocatorInterface', - array(), - array(), - 'ServiceLocatorInterface' - ); + $serverUrlHelperMock = $this->createMock(ServerUrl::class); + $mailerServiceMock = $this->createMock(Sendmail::class); + $configServiceMock = [uniqid()]; - $mailer = new Mailer(); - $mailer->setServiceLocator($serviceLocatorMock); + $mailer = new Mailer( + $serverUrlHelperMock, + $mailerServiceMock, + $configServiceMock + ); - $this->assertEquals($serviceLocatorMock, $mailer->getServiceLocator()); + // Assertions + $this->assertAttributeEquals($serverUrlHelperMock, 'serverUrlHelper', $mailer); + $this->assertAttributeEquals($mailerServiceMock, 'mailerService', $mailer); + $this->assertAttributeEquals($configServiceMock, 'configService', $mailer); } } diff --git a/tests/RbCommentTest/View/Helper/CommentTest.php b/tests/RbCommentTest/View/Helper/CommentTest.php index f693f7d..0533f5c 100644 --- a/tests/RbCommentTest/View/Helper/CommentTest.php +++ b/tests/RbCommentTest/View/Helper/CommentTest.php @@ -1,24 +1,32 @@ getMock( - 'Zend\ServiceManager\ServiceLocatorInterface', - array(), - array(), - 'ServiceLocatorInterface' - ); + $viewHelperManagerMock = $this->getMockBuilder('ViewHelperManager')->getMock(); + $routerServiceMock = $this->getMockBuilder('Router')->getMock(); + $configServiceMock = [uniqid()]; + $commentTableMock = $this->createMock(CommentTable::class); - $commentViewHelper = new RbCommentViewHelper(); - $commentViewHelper->setServiceLocator($serviceLocatorMock); + $commentViewHelper = new RbCommentViewHelper( + $viewHelperManagerMock, + $routerServiceMock, + $configServiceMock, + $commentTableMock + ); - $this->assertEquals($serviceLocatorMock, $commentViewHelper->getServiceLocator()); + $this->assertAttributeEquals($viewHelperManagerMock, 'viewHelperManager', $commentViewHelper); + $this->assertAttributeEquals($routerServiceMock, 'routerService', $commentViewHelper); + $this->assertAttributeEquals($configServiceMock, 'configService', $commentViewHelper); + $this->assertAttributeEquals($commentTableMock, 'commentTable', $commentViewHelper); } } diff --git a/tests/TestConfig.php.dist b/tests/TestConfig.php.dist index eed6d54..0b67a5f 100644 --- a/tests/TestConfig.php.dist +++ b/tests/TestConfig.php.dist @@ -1,3 +1,3 @@