From defe8aade8cdfc9345901640e5c5d2a86d97e788 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Tue, 10 Jan 2017 15:30:15 +0000 Subject: [PATCH 01/17] Require same PHP version as the latest Zend Framework version --- .gitignore | 1 + .travis.yml | 4 ++-- composer.json | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) 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..eccc2ed 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: php php: - - 5.5 - - 5.4 + - 5.6 + - 7.0 before_script: - composer self-update diff --git a/composer.json b/composer.json index 912741d..0d7c5c5 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ } ], "require": { - "php": ">=5.3.0", + "php": "^5.6 || ^7.0", "zendframework/zend-mvc": "2.*", "zendframework/zend-form": "2.*", "zendframework/zend-db": "2.*", @@ -53,4 +53,4 @@ "dev-master": "1.5.x-dev" } } -} \ No newline at end of file +} From 9eba402e384d91c6e31b1bcf400d85ae82bbf3f6 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Tue, 10 Jan 2017 15:45:40 +0000 Subject: [PATCH 02/17] Converted to short array syntax --- .travis.yml | 1 + composer.json | 3 +- config/module.config.php | 118 +++++++++--------- phpcs.xml | 22 ++++ .../Controller/CommentController.php | 4 +- .../Controller/ConsoleController.php | 2 +- src/RbComment/Form/CommentForm.php | 64 +++++----- src/RbComment/Model/Comment.php | 84 ++++++------- src/RbComment/Model/CommentTable.php | 24 ++-- .../Mvc/Controller/Plugin/Mailer.php | 2 +- src/RbComment/View/Helper/Comment.php | 10 +- .../Controller/CommentControllerTest.php | 82 ++++++------ .../Controller/ConsoleControllerTest.php | 18 +-- tests/RbCommentTest/Form/CommentFormTest.php | 4 +- .../RbCommentTest/Model/CommentTableTest.php | 66 +++++----- tests/RbCommentTest/Model/CommentTest.php | 22 ++-- .../Mvc/Controller/Plugin/MailerTest.php | 4 +- .../RbCommentTest/View/Helper/CommentTest.php | 4 +- tests/TestConfig.php.dist | 2 +- 19 files changed, 280 insertions(+), 256 deletions(-) create mode 100644 phpcs.xml diff --git a/.travis.yml b/.travis.yml index eccc2ed..f8b6089 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ before_script: - composer update script: + - php ./vendor/bin/phpcs - php ./vendor/bin/phpunit -c ./tests/ --coverage-clover=coverage.xml after_success: diff --git a/composer.json b/composer.json index 0d7c5c5..7c02a66 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,8 @@ }, "require-dev": { "robertboloc/zf2-components-list-generator": "dev-master", - "phpunit/phpunit": "3.7.*" + "phpunit/phpunit": "3.7.*", + "squizlabs/php_codesniffer": "^2.7" }, "autoload": { "psr-0": { diff --git a/config/module.config.php b/config/module.config.php index ae9e733..74a24d3 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,66 +1,66 @@ array( - 'routes' => array( - 'rbcomment' => array( +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( + ], + ], + ], + ], + ], + ], + 'controllers' => [ + 'invokables' => [ 'RbComment\Controller\Comment' => 'RbComment\Controller\CommentController', 'RbComment\Controller\Console' => 'RbComment\Controller\ConsoleController', - ), - ), - 'controller_plugins' => array( - 'invokables' => array( + ], + ], + 'controller_plugins' => [ + 'invokables' => [ 'rbMailer' => 'RbComment\Mvc\Controller\Plugin\Mailer', - ) - ), - 'view_helpers' => array( - 'invokables' => array( + ] + ], + 'view_helpers' => [ + 'invokables' => [ 'rbComment' => 'RbComment\View\Helper\Comment', - ) - ), - 'view_manager' => array( - 'template_map' => array( + ] + ], + '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( + 'strings' => [ 'author' => 'Author', 'contact' => 'Email', 'content' => 'Comment', @@ -71,8 +71,8 @@ '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 +80,7 @@ /** * Email addresses where to send the notification. */ - 'to' => array(), + 'to' => [], /** * From header. Usually something like noreply@myserver.com */ @@ -93,8 +93,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 +108,7 @@ * be configured to avoid false positives. * Uses the class \Zend\Http\PhpEnvironment\RemoteAddress */ - 'proxy' => array( + 'proxy' => [ /** * Use proxy addresses or not. */ @@ -116,25 +116,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..db84103 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,22 @@ + + + RbComment Coding Standard + + + + + + + + + + + + + + + + config + src + tests + diff --git a/src/RbComment/Controller/CommentController.php b/src/RbComment/Controller/CommentController.php index 6a81d0c..35a6f36 100644 --- a/src/RbComment/Controller/CommentController.php +++ b/src/RbComment/Controller/CommentController.php @@ -78,14 +78,14 @@ protected function isSpam($comment, $rbCommentConfig) $remote->setTrustedProxies($rbCommentConfig->akismet['proxy']['trusted']); $remote->setProxyHeader($rbCommentConfig->akismet['proxy']['header']); - return $this->getAkismetService()->isSpam(array( + return $this->getAkismetService()->isSpam([ '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, - )); + ]); } /** diff --git a/src/RbComment/Controller/ConsoleController.php b/src/RbComment/Controller/ConsoleController.php index 9da1cb7..d9ab40e 100644 --- a/src/RbComment/Controller/ConsoleController.php +++ b/src/RbComment/Controller/ConsoleController.php @@ -32,4 +32,4 @@ public function getCommentTable() return $this->commentTable; } -} \ No newline at end of file +} diff --git a/src/RbComment/Form/CommentForm.php b/src/RbComment/Form/CommentForm.php index a5bccec..0901db9 100644 --- a/src/RbComment/Form/CommentForm.php +++ b/src/RbComment/Form/CommentForm.php @@ -10,68 +10,68 @@ public function __construct(array $strings) { parent::__construct('rbcomment'); - $this->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( + 'attributes' => [ 'type' => 'text', 'placeholder' => $strings['author'], - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'contact', - 'attributes' => array( + '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', - ), - )); + ], + ]); } } diff --git a/src/RbComment/Model/Comment.php b/src/RbComment/Model/Comment.php index 17d9871..81f4a58 100644 --- a/src/RbComment/Model/Comment.php +++ b/src/RbComment/Model/Comment.php @@ -91,71 +91,71 @@ public function getInputFilter() $inputFilter = new InputFilter(); $factory = new InputFactory(); - $inputFilter->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..0c987a5 100644 --- a/src/RbComment/Model/CommentTable.php +++ b/src/RbComment/Model/CommentTable.php @@ -39,10 +39,10 @@ public function fetchAll() public function fetchAllForThread($thread) { $select = new Select($this->tableGateway->getTable()); - $select->columns(array('id', 'author', 'content', 'contact', + $select->columns(['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)) + 'published_on' => new Expression("DATE_FORMAT(published_on, '%M %d, %Y %H:%i')")]) + ->where(['thread' => $thread, 'visible' => 1]) ->order('published_on_raw ASC'); $resultSet = $this->tableGateway->selectWith($select); @@ -59,10 +59,10 @@ public function fetchAllForThread($thread) public function fetchAllPendingForThread($thread) { $select = new Select($this->tableGateway->getTable()); - $select->columns(array('id', 'author', 'content', 'contact', + $select->columns(['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)) + 'published_on' => new Expression("DATE_FORMAT(published_on, '%M %d, %Y %H:%i')")]) + ->where(['thread' => $thread, 'visible' => 0]) ->order('published_on_raw ASC'); $resultSet = $this->tableGateway->selectWith($select); @@ -90,7 +90,7 @@ public function fetchAllUsingSelect(\Zend\Db\Sql\Select $select) public function getComment($id) { $id = (int) $id; - $rowset = $this->tableGateway->select(array('id' => $id)); + $rowset = $this->tableGateway->select(['id' => $id]); $row = $rowset->current(); return $row; @@ -104,7 +104,7 @@ public function getComment($id) */ public function saveComment(Comment $comment) { - $data = array( + $data = [ 'thread' => $comment->thread, 'uri' => $comment->uri, 'author' => $comment->author, @@ -112,7 +112,7 @@ public function saveComment(Comment $comment) 'content' => $comment->content, 'visible' => $comment->visible, 'spam' => $comment->spam, - ); + ]; $id = (int) $comment->id; if ($id === 0) { @@ -120,7 +120,7 @@ public function saveComment(Comment $comment) $id = $this->tableGateway->lastInsertValue; } else { if ($this->getComment($id)) { - $this->tableGateway->update($data, array('id' => $id)); + $this->tableGateway->update($data, ['id' => $id]); } } @@ -134,7 +134,7 @@ public function saveComment(Comment $comment) */ public function deleteComment($id) { - $this->tableGateway->delete(array('id' => $id)); + $this->tableGateway->delete(['id' => $id]); } @@ -145,6 +145,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..8cd9151 100755 --- a/src/RbComment/Mvc/Controller/Plugin/Mailer.php +++ b/src/RbComment/Mvc/Controller/Plugin/Mailer.php @@ -46,7 +46,7 @@ public function __invoke($comment) $html->type = "text/html"; $body = new MimeMessage(); - $body->setParts(array($html)); + $body->setParts([$html]); $message = new Message(); $message->addFrom($mailerConfig['from']) diff --git a/src/RbComment/View/Helper/Comment.php b/src/RbComment/View/Helper/Comment.php index 5cf1b61..ed3af7d 100644 --- a/src/RbComment/View/Helper/Comment.php +++ b/src/RbComment/View/Helper/Comment.php @@ -10,11 +10,11 @@ class Comment extends AbstractHelper implements ServiceLocatorAwareInterface { private $serviceLocator; - protected $themes = array( + protected $themes = [ 'default' => true, 'uikit' => true, 'bootstrap3' => true, - ); + ]; public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { @@ -46,18 +46,18 @@ public function __invoke($theme = 'default') $config = $serviceManager->get('Config'); $strings = $config['rb_comment']['strings']; - echo $viewHelperManager->get('partial')->__invoke($invokablePartial, array( + echo $viewHelperManager->get('partial')->__invoke($invokablePartial, [ 'comments' => $serviceManager->get('RbComment\Model\CommentTable') ->fetchAllForThread($thread), 'form' => new \RbComment\Form\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'], - )); + ]); } } diff --git a/tests/RbCommentTest/Controller/CommentControllerTest.php b/tests/RbCommentTest/Controller/CommentControllerTest.php index 67de5c7..a7dfffc 100644 --- a/tests/RbCommentTest/Controller/CommentControllerTest.php +++ b/tests/RbCommentTest/Controller/CommentControllerTest.php @@ -10,16 +10,16 @@ class CommentControllerTest extends PHPUnit_Framework_TestCase { - protected $configMock = array( - 'rb_comment' => array( - 'strings' => array( + protected $configMock = [ + 'rb_comment' => [ + 'strings' => [ 'author' => 'author', 'contact' => 'contact', 'content' => 'content', 'submit' => 'submit', - ), - ), - ); + ], + ], + ]; protected $requestMock; @@ -29,15 +29,15 @@ public function setUp() { $this->serviceLocatorMock = $this->getMock( 'Zend\ServiceManager\ServiceLocatorInterface', - array('get', 'has'), - array(), + ['get', 'has'], + [], 'ServiceLocatorInterface' ); $this->requestMock = $this->getMock( 'Zend\Http\Request', - array('isPost', 'getPost'), - array(), + ['isPost', 'getPost'], + [], '', false ); @@ -62,8 +62,8 @@ public function testAddActionOnlyWorksWithPostMethod() // CommentController Mock $commentControllerMock = $this->getMock( 'RbComment\Controller\CommentController', - array('getRequest', 'getServiceLocator'), - array(), + ['getRequest', 'getServiceLocator'], + [], '', false ); @@ -82,11 +82,11 @@ public function testAddActionOnlyWorksWithPostMethod() public function testAddActionLogsFormErrorsIntoTheRbCommentNamespace() { //'contact' key is missing on purpose - $postMock = array( + $postMock = [ 'author' => 'Tester', 'content' => 'test', 'uri' => '/test', - ); + ]; // ServiceLocator Mock Setup $this->serviceLocatorMock->expects($this->once()) @@ -106,8 +106,8 @@ public function testAddActionLogsFormErrorsIntoTheRbCommentNamespace() // FlashMessenger Mock $flashMessengerMock = $this->getMock( 'Zend\Mvc\Controller\Plugin\FlashMessenger', - array('setNamespace', 'addMessage'), - array(), + ['setNamespace', 'addMessage'], + [], '', false ); @@ -119,8 +119,8 @@ public function testAddActionLogsFormErrorsIntoTheRbCommentNamespace() // Redirect Mock $redirectMock = $this->getMock( 'Zend\Mvc\Controller\Plugin\Redirect', - array('toUrl'), - array(), + ['toUrl'], + [], '', false ); @@ -132,8 +132,8 @@ public function testAddActionLogsFormErrorsIntoTheRbCommentNamespace() // CommentController Mock $commentControllerMock = $this->getMock( 'RbComment\Controller\CommentController', - array('getRequest', 'getServiceLocator', 'flashMessenger', 'redirect'), - array(), + ['getRequest', 'getServiceLocator', 'flashMessenger', 'redirect'], + [], '', false ); @@ -162,20 +162,20 @@ public function testAddActionLogsFormErrorsIntoTheRbCommentNamespace() */ public function testIsSpam($comment, $isSpam) { - $rbCommentConfig = (object) array( - 'akismet' => array( - 'proxy' => array( + $rbCommentConfig = (object) [ + 'akismet' => [ + 'proxy' => [ 'use' => false, - 'trusted' => array(), + 'trusted' => [], 'header' => '', - ), - ), - ); + ], + ], + ]; $akismetServiceMock = $this->getMock( 'ZendService\Akismet\Akismet', - array('isSpam'), - array(), + ['isSpam'], + [], '', false ); @@ -202,36 +202,36 @@ public function testIsSpam($comment, $isSpam) public static function isSpamDataProvider() { - return array( - array( + return [ + [ // comment - (object) array( + (object) [ 'author' => 'not a spammer', 'contact' => 'me@me.com', 'content' => 'test', - ), + ], // isSpam false, - ), - array( + ], + [ // comment - (object) array( + (object) [ 'author' => 'spammer', 'contact' => 'spam@spamfactory.com', 'content' => 'spam', - ), + ], // isSpam true, - ), - ); + ], + ]; } public function testGetCommentTableReturnsAnInstanceOfCommentTable() { $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array(), - array(), + [], + [], '', false ); diff --git a/tests/RbCommentTest/Controller/ConsoleControllerTest.php b/tests/RbCommentTest/Controller/ConsoleControllerTest.php index 97523f9..bf9968b 100644 --- a/tests/RbCommentTest/Controller/ConsoleControllerTest.php +++ b/tests/RbCommentTest/Controller/ConsoleControllerTest.php @@ -14,8 +14,8 @@ public function setUp() { $this->serviceLocatorMock = $this->getMock( 'Zend\ServiceManager\ServiceLocatorInterface', - array('get', 'has'), - array(), + ['get', 'has'], + [], 'ServiceLocatorInterface' ); } @@ -24,9 +24,9 @@ public function testDeleteSpamAction() { $deletedCount = rand(1, 100); - $commentTableMock = $this->getMock('RbComment\Model\CommentTable', array( + $commentTableMock = $this->getMock('RbComment\Model\CommentTable', [ 'deleteSpam' - ), array(), '', false); + ], [], '', false); // Expect this to be called once $commentTableMock->expects($this->once()) @@ -42,8 +42,8 @@ public function testDeleteSpamAction() // CommentController Mock $consoleControllerMock = $this->getMock( 'RbComment\Controller\ConsoleController', - array('getServiceLocator'), - array(), + ['getServiceLocator'], + [], '', false ); @@ -64,8 +64,8 @@ public function testGetCommentTableReturnsAnInstanceOfCommentTable() { $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array(), - array(), + [], + [], '', false ); @@ -83,4 +83,4 @@ public function testGetCommentTableReturnsAnInstanceOfCommentTable() $this->assertEquals($commentTable, $commentController->getCommentTable()); $this->assertInstanceOf('RbComment\Model\CommentTable', $commentController->getCommentTable()); } -} \ No newline at end of file +} diff --git a/tests/RbCommentTest/Form/CommentFormTest.php b/tests/RbCommentTest/Form/CommentFormTest.php index 5505636..d109a2f 100644 --- a/tests/RbCommentTest/Form/CommentFormTest.php +++ b/tests/RbCommentTest/Form/CommentFormTest.php @@ -10,12 +10,12 @@ class CommentFormTest extends PHPUnit_Framework_TestCase /** * @var array */ - protected $formStrings = array( + protected $formStrings = [ 'author' => '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..34ba0dc 100644 --- a/tests/RbCommentTest/Model/CommentTableTest.php +++ b/tests/RbCommentTest/Model/CommentTableTest.php @@ -15,8 +15,8 @@ public function testConstructorSetsDependencies() { $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array(), - array(), + [], + [], 'TableGateway', false ); @@ -38,8 +38,8 @@ public function testFetchAllReturnsAllComments() $resultSet = new ResultSet(); $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array('select'), - array(), + ['select'], + [], '', false ); @@ -57,8 +57,8 @@ public function testFetchAllForThreadReturnsAllTheCommentsInThread() $resultSet = new ResultSet(); $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array('selectWith'), - array(), + ['selectWith'], + [], '', false ); @@ -77,8 +77,8 @@ public function testFetchAllPendingForThreadReturnsAllThePendingCommentsInAThrea $resultSet = new ResultSet(); $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array('selectWith'), - array(), + ['selectWith'], + [], '', false ); @@ -96,7 +96,7 @@ public function testFetchAllUsingSelectUsesTheCustomSelectAndReturnsTheResult() { $tableGatewayMock = $this->getMockBuilder('Zend\Db\TableGateway\TableGateway') - ->setMethods(array('selectWith')) + ->setMethods(['selectWith']) ->disableOriginalConstructor() ->getMock(); @@ -119,7 +119,7 @@ public function testFetchAllUsingSelectUsesTheCustomSelectAndReturnsTheResult() public function testCanRetrieveACommentByItsId() { $comment = new Comment(); - $comment->exchangeArray(array( + $comment->exchangeArray([ 'id' => 12345, 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', 'uri' => '/test', @@ -128,23 +128,23 @@ public function testCanRetrieveACommentByItsId() 'content' => 'Bla bla bla', 'visible' => 1, 'spam' => 0, - )); + ]); $resultSet = new ResultSet(); $resultSet->setArrayObjectPrototype(new Comment()); - $resultSet->initialize(array($comment)); + $resultSet->initialize([$comment]); $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array('select'), - array(), + ['select'], + [], '', false ); $tableGatewayMock->expects($this->once()) ->method('select') - ->with(array('id' => 12345)) + ->with(['id' => 12345]) ->will($this->returnValue($resultSet)); $commentTable = new CommentTable($tableGatewayMock); @@ -155,7 +155,7 @@ public function testCanRetrieveACommentByItsId() public function testSaveCommentWillInsertNewCommentIfDoesNotAlreadyHaveAnId() { $comment = new Comment(); - $commentData = array( + $commentData = [ 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', 'uri' => '/test', 'author' => 'Robert Boloc', @@ -163,14 +163,14 @@ public function testSaveCommentWillInsertNewCommentIfDoesNotAlreadyHaveAnId() 'content' => 'Bla bla bla', 'visible' => 1, 'spam' => 0, - ); + ]; $comment->exchangeArray($commentData); $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array('insert'), - array(), + ['insert'], + [], '', false ); @@ -186,7 +186,7 @@ public function testSaveCommentWillInsertNewCommentIfDoesNotAlreadyHaveAnId() public function testSaveCommentWillUpdateExistingCommentIfItAlreadyHasAnId() { $comment = new Comment(); - $commentData = array( + $commentData = [ 'id' => 12345, 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', 'uri' => '/test', @@ -195,29 +195,29 @@ public function testSaveCommentWillUpdateExistingCommentIfItAlreadyHasAnId() 'content' => 'Bla bla bla', 'visible' => 1, 'spam' => 0, - ); + ]; $comment->exchangeArray($commentData); $resultSet = new ResultSet(); $resultSet->setArrayObjectPrototype(new Comment()); - $resultSet->initialize(array($comment)); + $resultSet->initialize([$comment]); $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array('select', 'update'), - array(), + ['select', 'update'], + [], '', false ); $tableGatewayMock->expects($this->once()) ->method('select') - ->with(array('id' => 12345)) + ->with(['id' => 12345]) ->will($this->returnValue($resultSet)); $tableGatewayMock->expects($this->once()) ->method('update') - ->with(array( + ->with([ 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', 'uri' => '/test', 'author' => 'Robert Boloc', @@ -225,7 +225,7 @@ public function testSaveCommentWillUpdateExistingCommentIfItAlreadyHasAnId() 'content' => 'Bla bla bla', 'visible' => 1, 'spam' => 0, - ), array('id' => 12345)); + ], ['id' => 12345]); $commentTable = new CommentTable($tableGatewayMock); $commentTable->saveComment($comment); @@ -235,15 +235,15 @@ public function testCanDeleteACommentByItsId() { $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array('delete'), - array(), + ['delete'], + [], '', false ); $tableGatewayMock->expects($this->once()) ->method('delete') - ->with(array('id' => 12345)); + ->with(['id' => 12345]); $commentTable = new CommentTable($tableGatewayMock); $commentTable->deleteComment(12345); @@ -253,15 +253,15 @@ public function testCanDeleteSpam() { $tableGatewayMock = $this->getMock( 'Zend\Db\TableGateway\TableGateway', - array('delete'), - array(), + ['delete'], + [], '', false ); $tableGatewayMock->expects($this->once()) ->method('delete') - ->with(array('spam' => 1)); + ->with(['spam' => 1]); $commentTable = new CommentTable($tableGatewayMock); $commentTable->deleteSpam(); diff --git a/tests/RbCommentTest/Model/CommentTest.php b/tests/RbCommentTest/Model/CommentTest.php index ae6f52e..514b132 100644 --- a/tests/RbCommentTest/Model/CommentTest.php +++ b/tests/RbCommentTest/Model/CommentTest.php @@ -15,7 +15,7 @@ public function setUp() { $this->testComment = new Comment(); - $this->testArray = array( + $this->testArray = [ 'id' => 1, 'thread' => 12345, 'uri' => 'test-uri', @@ -25,7 +25,7 @@ public function setUp() 'visible' => 1, 'spam' => 0, 'published_on' => 12345678, - ); + ]; } public function testCommentInitialState() @@ -95,23 +95,23 @@ public function testGetInputFilterValidation($data, $validity) 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', '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..c459c32 100644 --- a/tests/RbCommentTest/Mvc/Controller/Plugin/MailerTest.php +++ b/tests/RbCommentTest/Mvc/Controller/Plugin/MailerTest.php @@ -11,8 +11,8 @@ public function testSetAndGetServiceLocator() { $serviceLocatorMock = $this->getMock( 'Zend\ServiceManager\ServiceLocatorInterface', - array(), - array(), + [], + [], 'ServiceLocatorInterface' ); diff --git a/tests/RbCommentTest/View/Helper/CommentTest.php b/tests/RbCommentTest/View/Helper/CommentTest.php index f693f7d..e75d083 100644 --- a/tests/RbCommentTest/View/Helper/CommentTest.php +++ b/tests/RbCommentTest/View/Helper/CommentTest.php @@ -11,8 +11,8 @@ public function testSetAndGetServiceLocator() { $serviceLocatorMock = $this->getMock( 'Zend\ServiceManager\ServiceLocatorInterface', - array(), - array(), + [], + [], 'ServiceLocatorInterface' ); 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 @@ Date: Wed, 11 Jan 2017 21:21:17 +0000 Subject: [PATCH 03/17] Update dependencies to Zend Framework 3 --- composer.json | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 7c02a66..965e3d7 100644 --- a/composer.json +++ b/composer.json @@ -19,26 +19,26 @@ ], "require": { "php": "^5.6 || ^7.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/zend-mvc": "^3.0.1", + "zendframework/zend-form": "^2.9", + "zendframework/zend-db": "^2.8.1", + "zendframework/zend-inputfilter": "^2.7.2", + "zendframework/zend-view": "^2.8", + "zendframework/zend-servicemanager": "^3.1", + "zendframework/zend-loader": "^2.5.1", + "zendframework/zend-stdlib": "^3.0.1", + "zendframework/zend-modulemanager": "^2.7.2", + "zendframework/zend-serializer": "^2.8", + "zendframework/zend-mail": "^2.7.1", + "zendframework/zend-mime": "^2.6", + "zendframework/zend-i18n": "^2.7.3", "zendframework/zendservice-akismet": "2.0.2", - "zendframework/zend-http": "2.*", - "zendframework/zend-console": "2.*" + "zendframework/zend-http": "^2.5.4", + "zendframework/zend-console": "^2.6" }, "require-dev": { "robertboloc/zf2-components-list-generator": "dev-master", - "phpunit/phpunit": "3.7.*", + "phpunit/phpunit": "^5.7", "squizlabs/php_codesniffer": "^2.7" }, "autoload": { From 8e64d433af42ff422a8e1f554bc0408186088ec2 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Thu, 12 Jan 2017 22:31:44 +0000 Subject: [PATCH 04/17] Refactor the usage of the ServiceAwareInterface in the Comment view helper --- Module.php | 15 ----- composer.json | 24 +++---- config/module.config.php | 7 ++- .../Factory/View/Helper/CommentFactory.php | 27 ++++++++ src/RbComment/View/Helper/Comment.php | 62 +++++++++---------- .../Controller/CommentControllerTest.php | 8 +-- .../Controller/ConsoleControllerTest.php | 7 +-- 7 files changed, 78 insertions(+), 72 deletions(-) create mode 100644 src/RbComment/Factory/View/Helper/CommentFactory.php diff --git a/Module.php b/Module.php index 3b4f60f..7850f8d 100644 --- a/Module.php +++ b/Module.php @@ -1,5 +1,4 @@ array( - __DIR__ . '/autoload_classmap.php', - ), - 'Zend\Loader\StandardAutoloader' => array( - 'namespaces' => array( - __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, - ), - ), - ); - } - public function getServiceConfig() { return array( diff --git a/composer.json b/composer.json index 965e3d7..a4176ff 100644 --- a/composer.json +++ b/composer.json @@ -19,22 +19,24 @@ ], "require": { "php": "^5.6 || ^7.0", - "zendframework/zend-mvc": "^3.0.1", - "zendframework/zend-form": "^2.9", + "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-view": "^2.8", - "zendframework/zend-servicemanager": "^3.1", "zendframework/zend-loader": "^2.5.1", - "zendframework/zend-stdlib": "^3.0.1", - "zendframework/zend-modulemanager": "^2.7.2", - "zendframework/zend-serializer": "^2.8", "zendframework/zend-mail": "^2.7.1", + "zendframework/zend-math": "^3.0", "zendframework/zend-mime": "^2.6", - "zendframework/zend-i18n": "^2.7.3", - "zendframework/zendservice-akismet": "2.0.2", - "zendframework/zend-http": "^2.5.4", - "zendframework/zend-console": "^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", diff --git a/config/module.config.php b/config/module.config.php index 74a24d3..9b39241 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,4 +1,7 @@ [ @@ -44,8 +47,8 @@ ] ], 'view_helpers' => [ - 'invokables' => [ - 'rbComment' => 'RbComment\View\Helper\Comment', + 'factories' => [ + 'rbComment' => Factory\View\Helper\CommentFactory::class, ] ], 'view_manager' => [ 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/View/Helper/Comment.php b/src/RbComment/View/Helper/Comment.php index ed3af7d..851cda3 100644 --- a/src/RbComment/View/Helper/Comment.php +++ b/src/RbComment/View/Helper/Comment.php @@ -1,31 +1,34 @@ true, 'uikit' => true, 'bootstrap3' => true, ]; - public function setServiceLocator(ServiceLocatorInterface $serviceLocator) + private $viewHelperManager; + private $routerService; + private $configService; + private $commentTable; + + public function __construct( + $viewHelperManager, + $routerService, + array $configService, + CommentTable $commentTable + ) { - $this->serviceLocator = $serviceLocator; - - return $this; - } - - public function getServiceLocator() - { - return $this->serviceLocator; + $this->viewHelperManager = $viewHelperManager; + $this->routerService = $routerService; + $this->configService = $configService; + $this->commentTable = $commentTable; } public function __invoke($theme = 'default') @@ -35,29 +38,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, [ - '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)) : [], - '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/RbCommentTest/Controller/CommentControllerTest.php b/tests/RbCommentTest/Controller/CommentControllerTest.php index a7dfffc..1c892a0 100644 --- a/tests/RbCommentTest/Controller/CommentControllerTest.php +++ b/tests/RbCommentTest/Controller/CommentControllerTest.php @@ -4,6 +4,7 @@ use RbComment\Model\CommentTable; use RbComment\Controller\CommentController; +use Zend\ServiceManager\ServiceLocatorInterface; use ZendService\Akismet\Akismet; use PHPUnit_Framework_TestCase; use ReflectionClass; @@ -27,12 +28,7 @@ class CommentControllerTest extends PHPUnit_Framework_TestCase public function setUp() { - $this->serviceLocatorMock = $this->getMock( - 'Zend\ServiceManager\ServiceLocatorInterface', - ['get', 'has'], - [], - 'ServiceLocatorInterface' - ); + $this->serviceLocatorMock = $this->createMock(ServiceLocatorInterface::class); $this->requestMock = $this->getMock( 'Zend\Http\Request', diff --git a/tests/RbCommentTest/Controller/ConsoleControllerTest.php b/tests/RbCommentTest/Controller/ConsoleControllerTest.php index bf9968b..e11c323 100644 --- a/tests/RbCommentTest/Controller/ConsoleControllerTest.php +++ b/tests/RbCommentTest/Controller/ConsoleControllerTest.php @@ -12,12 +12,7 @@ class ConsoleControllerTest extends PHPUnit_Framework_TestCase public function setUp() { - $this->serviceLocatorMock = $this->getMock( - 'Zend\ServiceManager\ServiceLocatorInterface', - ['get', 'has'], - [], - 'ServiceLocatorInterface' - ); + $this->serviceLocatorMock = $this->createMock(ServiceLocatorInterface::class); } public function testDeleteSpamAction() From f0a20958c2482cbf8a2c222a317e98487482d949 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Sat, 14 Jan 2017 20:22:08 +0000 Subject: [PATCH 05/17] Refactor the usage of the service locator aware interface in the comment controller --- Module.php | 2 +- config/module.config.php | 4 +- .../Controller/CommentController.php | 59 +++--- .../Controller/CommentControllerFactory.php | 28 +++ .../Controller/CommentControllerTest.php | 173 +++++------------- 5 files changed, 105 insertions(+), 161 deletions(-) create mode 100644 src/RbComment/Factory/Controller/CommentControllerFactory.php diff --git a/Module.php b/Module.php index 7850f8d..5d81473 100644 --- a/Module.php +++ b/Module.php @@ -45,7 +45,7 @@ public function getServiceConfig() 'RbComment\Akismet' => function ($serviceManager) { $config = $serviceManager->get('Config'); - $viewHelperManager = $serviceManager->get('viewhelpermanager'); + $viewHelperManager = $serviceManager->get('ViewHelperManager'); $akismetConfig = $config['rb_comment']['akismet']; diff --git a/config/module.config.php b/config/module.config.php index 9b39241..4929a91 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -36,8 +36,10 @@ ], ], 'controllers' => [ + 'factories' => [ + 'RbComment\Controller\Comment' => Factory\Controller\CommentControllerFactory::class, + ], 'invokables' => [ - 'RbComment\Controller\Comment' => 'RbComment\Controller\CommentController', 'RbComment\Controller\Console' => 'RbComment\Controller\ConsoleController', ], ], diff --git a/src/RbComment/Controller/CommentController.php b/src/RbComment/Controller/CommentController.php index 35a6f36..06e228d 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) && @@ -78,7 +93,7 @@ protected function isSpam($comment, $rbCommentConfig) $remote->setTrustedProxies($rbCommentConfig->akismet['proxy']['trusted']); $remote->setProxyHeader($rbCommentConfig->akismet['proxy']['header']); - return $this->getAkismetService()->isSpam([ + return $this->akismetService->isSpam([ 'user_ip' => $remote->getIpAddress(), 'user_agent' => filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), 'comment_type' => 'comment', @@ -87,30 +102,4 @@ protected function isSpam($comment, $rbCommentConfig) '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'); - } - - return $this->akismetService; - } } 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/tests/RbCommentTest/Controller/CommentControllerTest.php b/tests/RbCommentTest/Controller/CommentControllerTest.php index 1c892a0..f6902aa 100644 --- a/tests/RbCommentTest/Controller/CommentControllerTest.php +++ b/tests/RbCommentTest/Controller/CommentControllerTest.php @@ -1,13 +1,15 @@ serviceLocatorMock = $this->createMock(ServiceLocatorInterface::class); - - $this->requestMock = $this->getMock( - 'Zend\Http\Request', - ['isPost', 'getPost'], - [], - '', - false - ); + $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'; @@ -44,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', - ['getRequest', 'getServiceLocator'], - [], - '', - 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(); } @@ -84,59 +73,39 @@ public function testAddActionLogsFormErrorsIntoTheRbCommentNamespace() 'uri' => '/test', ]; - // ServiceLocator Mock Setup - $this->serviceLocatorMock->expects($this->once()) - ->method('get') - ->with('Config') - ->will($this->returnValue($this->configMock)); - // 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', - ['setNamespace', 'addMessage'], - [], - '', - false - ); + $flashMessengerMock = $this->createMock(FlashMessenger::class); $flashMessengerMock->expects($this->once()) ->method('setNamespace') ->with('RbComment'); // Redirect Mock - $redirectMock = $this->getMock( - 'Zend\Mvc\Controller\Plugin\Redirect', - ['toUrl'], - [], - '', - false - ); + $redirectMock = $this->createMock(Redirect::class); $redirectMock->expects($this->once()) ->method('toUrl') ->with($postMock['uri'] . '#rbcomment'); // CommentController Mock - $commentControllerMock = $this->getMock( - 'RbComment\Controller\CommentController', - ['getRequest', 'getServiceLocator', 'flashMessenger', 'redirect'], - [], - '', - 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') @@ -168,30 +137,26 @@ public function testIsSpam($comment, $isSpam) ], ]; - $akismetServiceMock = $this->getMock( - 'ZendService\Akismet\Akismet', - ['isSpam'], - [], - '', - false - ); + $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)); } @@ -221,44 +186,4 @@ public static function isSpamDataProvider() ], ]; } - - public function testGetCommentTableReturnsAnInstanceOfCommentTable() - { - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - [], - [], - '', - 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()); - } } From 49fea1dd6233a76a6517cb6191c2fbf112bb7b1b Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Sun, 15 Jan 2017 18:36:46 +0000 Subject: [PATCH 06/17] Move services factories from closures into own files --- Module.php | 59 ++++--------------- config/module.config.php | 16 +++++ .../Factory/Model/CommentTableFactory.php | 23 ++++++++ .../Model/RbCommentTableGatewayFactory.php | 29 +++++++++ .../Factory/Service/AkismetServiceFactory.php | 29 +++++++++ src/RbComment/View/Helper/Comment.php | 3 +- 6 files changed, 108 insertions(+), 51 deletions(-) create mode 100644 src/RbComment/Factory/Model/CommentTableFactory.php create mode 100644 src/RbComment/Factory/Model/RbCommentTableGatewayFactory.php create mode 100644 src/RbComment/Factory/Service/AkismetServiceFactory.php diff --git a/Module.php b/Module.php index 5d81473..a9ee1e0 100644 --- a/Module.php +++ b/Module.php @@ -1,67 +1,28 @@ 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/config/module.config.php b/config/module.config.php index 4929a91..0d20fb9 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -2,6 +2,8 @@ namespace RbComment; use RbComment\Factory; +use RbComment\Model\CommentTable; +use Zend\Mail\Transport\Sendmail; return [ 'router' => [ @@ -48,6 +50,20 @@ 'rbMailer' => 'RbComment\Mvc\Controller\Plugin\Mailer', ] ], + '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, 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..06bbf10 --- /dev/null +++ b/src/RbComment/Factory/Model/RbCommentTableGatewayFactory.php @@ -0,0 +1,29 @@ +get(Adapter::class); + $resultSetPrototype = new ResultSet(); + $resultSetPrototype->setArrayObjectPrototype(new Comment()); + + return new TableGateway('rb_comments', $databaseAdapter, null, $resultSetPrototype); + } +} 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/View/Helper/Comment.php b/src/RbComment/View/Helper/Comment.php index 851cda3..311ebfa 100644 --- a/src/RbComment/View/Helper/Comment.php +++ b/src/RbComment/View/Helper/Comment.php @@ -23,8 +23,7 @@ public function __construct( $routerService, array $configService, CommentTable $commentTable - ) - { + ) { $this->viewHelperManager = $viewHelperManager; $this->routerService = $routerService; $this->configService = $configService; From ca1e57a545ea4c83b284636c735e21cd1b175a5c Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Sun, 22 Jan 2017 14:14:17 +0000 Subject: [PATCH 07/17] Refactored the usage of SM in the Console controller --- config/module.config.php | 4 +- .../Controller/ConsoleController.php | 27 +++----- .../Controller/ConsoleControllerFactory.php | 26 ++++++++ .../Controller/ConsoleControllerTest.php | 62 +++---------------- 4 files changed, 45 insertions(+), 74 deletions(-) create mode 100644 src/RbComment/Factory/Controller/ConsoleControllerFactory.php diff --git a/config/module.config.php b/config/module.config.php index 0d20fb9..3629638 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -40,9 +40,7 @@ 'controllers' => [ 'factories' => [ 'RbComment\Controller\Comment' => Factory\Controller\CommentControllerFactory::class, - ], - 'invokables' => [ - 'RbComment\Controller\Console' => 'RbComment\Controller\ConsoleController', + 'RbComment\Controller\Console' => Factory\Controller\ConsoleControllerFactory::class, ], ], 'controller_plugins' => [ diff --git a/src/RbComment/Controller/ConsoleController.php b/src/RbComment/Controller/ConsoleController.php index d9ab40e..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'); } } 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/tests/RbCommentTest/Controller/ConsoleControllerTest.php b/tests/RbCommentTest/Controller/ConsoleControllerTest.php index e11c323..312609f 100644 --- a/tests/RbCommentTest/Controller/ConsoleControllerTest.php +++ b/tests/RbCommentTest/Controller/ConsoleControllerTest.php @@ -1,51 +1,33 @@ serviceLocatorMock = $this->createMock(ServiceLocatorInterface::class); + $this->commentTableMock = $this->createMock(CommentTable::class); } + /** + * @group controller + */ public function testDeleteSpamAction() { $deletedCount = rand(1, 100); - $commentTableMock = $this->getMock('RbComment\Model\CommentTable', [ - 'deleteSpam' - ], [], '', 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', - ['getServiceLocator'], - [], - '', - false - ); - - $consoleControllerMock->expects($this->once()) - ->method('getServiceLocator') - ->will($this->returnValue($this->serviceLocatorMock)); + $consoleControllerMock = new ConsoleController($this->commentTableMock); // Capture output ob_start(); @@ -54,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', - [], - [], - '', - 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()); - } } From 31ed4ea160f6dc25c38dd7193d3d188da42e0950 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Tue, 24 Jan 2017 22:33:18 +0000 Subject: [PATCH 08/17] Refactor the usage of the SM in the mailer mvc plugin --- config/module.config.php | 4 +- .../Mvc/Controller/Plugin/MailerFactory.php | 25 +++++++++++ .../Mvc/Controller/Plugin/Mailer.php | 41 ++++++++----------- .../Mvc/Controller/Plugin/MailerTest.php | 29 ++++++++----- 4 files changed, 61 insertions(+), 38 deletions(-) create mode 100644 src/RbComment/Factory/Mvc/Controller/Plugin/MailerFactory.php diff --git a/config/module.config.php b/config/module.config.php index 3629638..132c54f 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -44,8 +44,8 @@ ], ], 'controller_plugins' => [ - 'invokables' => [ - 'rbMailer' => 'RbComment\Mvc\Controller\Plugin\Mailer', + 'factories' => [ + 'rbMailer' => Factory\Mvc\Controller\Plugin\MailerFactory::class, ] ], 'service_manager' => [ 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..6896609 --- /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/Mvc/Controller/Plugin/Mailer.php b/src/RbComment/Mvc/Controller/Plugin/Mailer.php index 8cd9151..12b4a38 100755 --- a/src/RbComment/Mvc/Controller/Plugin/Mailer.php +++ b/src/RbComment/Mvc/Controller/Plugin/Mailer.php @@ -1,44 +1,35 @@ serviceLocator = $serviceLocator; - - return $this; - } - - public function getServiceLocator() + private $serverUrlHelper; + private $mailerService; + private $configService; + + public function __construct( + $serverUrlHelper, + $mailerService, + array $configService + ) { - return $this->serviceLocator; + $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 .= '' . + $htmlContent .= '' . $mailerConfig['context_link_text'] . ''; @@ -57,6 +48,6 @@ public function __invoke($comment) $message->addTo($mConfig); } - $mailerService->send($message); + $this->mailerService->send($message); } } diff --git a/tests/RbCommentTest/Mvc/Controller/Plugin/MailerTest.php b/tests/RbCommentTest/Mvc/Controller/Plugin/MailerTest.php index c459c32..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', - [], - [], - '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); } } From 0ae04033b9d0a2e7e06f3387b06853e3549ad7a8 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Wed, 25 Jan 2017 19:42:49 +0000 Subject: [PATCH 09/17] Fix Comment Table tests and formatting --- src/RbComment/Model/CommentTable.php | 66 ++-- .../RbCommentTest/Model/CommentTableTest.php | 291 ++++++++---------- 2 files changed, 167 insertions(+), 190 deletions(-) diff --git a/src/RbComment/Model/CommentTable.php b/src/RbComment/Model/CommentTable.php index 0c987a5..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(['id', 'author', 'content', 'contact', - 'published_on_raw' => 'published_on', - 'published_on' => new Expression("DATE_FORMAT(published_on, '%M %d, %Y %H:%i')")]) + $select->columns($columns) ->where(['thread' => $thread, 'visible' => 1]) ->order('published_on_raw ASC'); @@ -54,14 +59,22 @@ 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(['id', 'author', 'content', 'contact', - 'published_on_raw' => 'published_on', - 'published_on' => new Expression("DATE_FORMAT(published_on, '%M %d, %Y %H:%i')")]) + $select->columns($columns) ->where(['thread' => $thread, 'visible' => 0]) ->order('published_on_raw ASC'); @@ -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; + $id = (int)$id; $rowset = $this->tableGateway->select(['id' => $id]); - $row = $rowset->current(); + $row = $rowset->current(); return $row; } @@ -99,22 +114,23 @@ 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 = [ - 'thread' => $comment->thread, - 'uri' => $comment->uri, - 'author' => $comment->author, + '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; diff --git a/tests/RbCommentTest/Model/CommentTableTest.php b/tests/RbCommentTest/Model/CommentTableTest.php index 34ba0dc..7ba2308 100644 --- a/tests/RbCommentTest/Model/CommentTableTest.php +++ b/tests/RbCommentTest/Model/CommentTableTest.php @@ -1,5 +1,4 @@ getMock( - 'Zend\Db\TableGateway\TableGateway', - [], - [], - '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', - ['select'], - [], - '', - 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', - ['selectWith'], - [], - '', - 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', - ['selectWith'], - [], - '', - 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(['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([ - '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([$comment]); - - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - ['select'], - [], - '', - false - ); + $resultSet->initialize([$commentData]); - $tableGatewayMock->expects($this->once()) - ->method('select') - ->with(['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 = [ - 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', - 'uri' => '/test', + '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', - ['insert'], - [], - '', - 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 = [ - 'id' => 12345, - 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', - 'uri' => '/test', + '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([$comment]); - - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - ['select', 'update'], - [], - '', - 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(['id' => 12345]) - ->will($this->returnValue($resultSet)); - $tableGatewayMock->expects($this->once()) - ->method('update') - ->with([ - 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', - 'uri' => '/test', + $updateWith = [ + 'thread' => 'f133a4599372cf531bcdbfeb1116b9afe8d09b4f', + 'uri' => '/test', 'author' => 'Robert Boloc', - 'contact' => 'robertboloc@gmail.com', - 'content' => 'Bla bla bla', - 'visible' => 1, - 'spam' => 0, - ], ['id' => 12345]); + 'contact' => 'robertboloc@gmail.com', + 'content' => 'Bla bla bla', + 'visible' => 1, + 'spam' => 0, + ]; + + $this->tableGatewayMock->expects($this->once()) + ->method('update') + ->with($updateWith, ['id' => 12345]); - $commentTable = new CommentTable($tableGatewayMock); + $commentTable = new CommentTable($this->tableGatewayMock); $commentTable->saveComment($comment); } + /** + * @group table + */ public function testCanDeleteACommentByItsId() { - $tableGatewayMock = $this->getMock( - 'Zend\Db\TableGateway\TableGateway', - ['delete'], - [], - '', - false - ); - - $tableGatewayMock->expects($this->once()) - ->method('delete') - ->with(['id' => 12345]); + $this->tableGatewayMock->expects($this->once()) + ->method('delete') + ->with(['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', - ['delete'], - [], - '', - false - ); - - $tableGatewayMock->expects($this->once()) - ->method('delete') - ->with(['spam' => 1]); + $this->tableGatewayMock->expects($this->once()) + ->method('delete') + ->with(['spam' => 1]); - $commentTable = new CommentTable($tableGatewayMock); + $commentTable = new CommentTable($this->tableGatewayMock); $commentTable->deleteSpam(); } } From f8bb3a038334cc708a197979a977739e56f09c37 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Thu, 26 Jan 2017 21:51:53 +0000 Subject: [PATCH 10/17] Fix comment view helper unit tests --- tests/RbCommentTest/Model/CommentTest.php | 21 ++++++++++--- .../RbCommentTest/View/Helper/CommentTest.php | 30 ++++++++++++------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/tests/RbCommentTest/Model/CommentTest.php b/tests/RbCommentTest/Model/CommentTest.php index 514b132..3b41ea7 100644 --- a/tests/RbCommentTest/Model/CommentTest.php +++ b/tests/RbCommentTest/Model/CommentTest.php @@ -1,5 +1,4 @@ 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,6 +103,9 @@ public function testGetInputFilterValidation($data, $validity) $this->assertEquals($inputFilter->isValid(), $validity); } + /** + * @return array + */ public static function inputFilterDataProvider() { return [ @@ -104,9 +117,9 @@ public static function inputFilterDataProvider() // True because everything is ok [ [ - 'id' => 1, - 'thread' => md5('test'), - 'author' => 'Robert Boloc', + 'id' => 1, + 'thread' => md5('test'), + 'author' => 'Robert Boloc', 'contact' => 'robert@test.com', 'content' => 'bla bla bla', ], diff --git a/tests/RbCommentTest/View/Helper/CommentTest.php b/tests/RbCommentTest/View/Helper/CommentTest.php index e75d083..0533f5c 100644 --- a/tests/RbCommentTest/View/Helper/CommentTest.php +++ b/tests/RbCommentTest/View/Helper/CommentTest.php @@ -1,24 +1,32 @@ getMock( - 'Zend\ServiceManager\ServiceLocatorInterface', - [], - [], - '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); } } From 727f6b55f7601342ebc264756187c82dfbe40bf6 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Fri, 27 Jan 2017 22:39:18 +0000 Subject: [PATCH 11/17] Code style fixes --- config/module.config.php | 20 +++++++++---------- phpcs.xml | 1 + .../Controller/CommentController.php | 20 ++++++++++--------- src/RbComment/Form/CommentForm.php | 7 +++---- src/RbComment/Model/Comment.php | 3 +-- .../Mvc/Controller/Plugin/Mailer.php | 3 +-- tests/Bootstrap.php | 1 - .../Controller/CommentControllerTest.php | 15 ++++++++------ tests/RbCommentTest/Form/CommentFormTest.php | 1 - tests/RbCommentTest/Model/CommentTest.php | 16 +++++++-------- 10 files changed, 44 insertions(+), 43 deletions(-) diff --git a/config/module.config.php b/config/module.config.php index 132c54f..1c86149 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -30,7 +30,7 @@ 'route' => 'delete spam', 'defaults' => [ 'controller' => 'RbComment\Controller\Console', - 'action' => 'delete-spam', + 'action' => 'delete-spam', ], ], ], @@ -80,15 +80,15 @@ */ 'default_visibility' => 1, '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', + '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' => [ diff --git a/phpcs.xml b/phpcs.xml index db84103..255744c 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -19,4 +19,5 @@ config src tests + Module.php diff --git a/src/RbComment/Controller/CommentController.php b/src/RbComment/Controller/CommentController.php index 06e228d..e686cd8 100644 --- a/src/RbComment/Controller/CommentController.php +++ b/src/RbComment/Controller/CommentController.php @@ -82,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) @@ -93,13 +93,15 @@ protected function isSpam($comment, $rbCommentConfig) $remote->setTrustedProxies($rbCommentConfig->akismet['proxy']['trusted']); $remote->setProxyHeader($rbCommentConfig->akismet['proxy']['header']); - return $this->akismetService->isSpam([ - '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, - ]); + 'comment_content' => $comment->content, + ]; + + return $this->akismetService->isSpam($content); } } diff --git a/src/RbComment/Form/CommentForm.php b/src/RbComment/Form/CommentForm.php index 0901db9..5abe7df 100644 --- a/src/RbComment/Form/CommentForm.php +++ b/src/RbComment/Form/CommentForm.php @@ -1,5 +1,4 @@ add([ 'name' => 'author', 'attributes' => [ - 'type' => 'text', + 'type' => 'text', 'placeholder' => $strings['author'], ], ]); @@ -52,7 +51,7 @@ public function __construct(array $strings) $this->add([ 'name' => 'contact', 'attributes' => [ - 'type' => 'text', + 'type' => 'text', 'placeholder' => $strings['contact'], ], ]); @@ -70,7 +69,7 @@ public function __construct(array $strings) '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 81f4a58..a48f779 100644 --- a/src/RbComment/Model/Comment.php +++ b/src/RbComment/Model/Comment.php @@ -1,5 +1,4 @@ serverUrlHelper = $serverUrlHelper; $this->mailerService = $mailerService; $this->configService = $configService; 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 @@ 'Tester', + 'author' => 'Tester', 'content' => 'test', - 'uri' => '/test', + 'uri' => '/test', ]; // Request Mock Setup @@ -130,9 +130,9 @@ public function testIsSpam($comment, $isSpam) $rbCommentConfig = (object) [ 'akismet' => [ 'proxy' => [ - 'use' => false, + 'use' => false, 'trusted' => [], - 'header' => '', + 'header' => '', ], ], ]; @@ -161,13 +161,16 @@ public function testIsSpam($comment, $isSpam) $this->assertEquals($isSpam, $isSpamReflection->invoke($commentController, $comment, $rbCommentConfig)); } + /** + * @return array + */ public static function isSpamDataProvider() { return [ [ // comment (object) [ - 'author' => 'not a spammer', + 'author' => 'not a spammer', 'contact' => 'me@me.com', 'content' => 'test', ], @@ -177,7 +180,7 @@ public static function isSpamDataProvider() [ // comment (object) [ - 'author' => 'spammer', + 'author' => 'spammer', 'contact' => 'spam@spamfactory.com', 'content' => 'spam', ], diff --git a/tests/RbCommentTest/Form/CommentFormTest.php b/tests/RbCommentTest/Form/CommentFormTest.php index d109a2f..2e7ecf7 100644 --- a/tests/RbCommentTest/Form/CommentFormTest.php +++ b/tests/RbCommentTest/Form/CommentFormTest.php @@ -1,5 +1,4 @@ testComment = new Comment(); $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, + '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, ]; } From e8b13cc5f40e5824c787a7529919b5237546b969 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Sat, 28 Jan 2017 23:56:29 +0000 Subject: [PATCH 12/17] Add tests for the comment controller factory --- .../CommentControllerFactoryTest.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/RbCommentTest/Factory/Controller/CommentControllerFactoryTest.php 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); + } +} From d88f000ae7ca31fa0f0970cda4994decf82a2934 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Sun, 29 Jan 2017 00:03:49 +0000 Subject: [PATCH 13/17] Update factories coverage --- .../ConsoleControllerFactoryTest.php | 33 +++++++++++++++++++ .../Factory/Model/CommentTableFactoryTest.php | 33 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/RbCommentTest/Factory/Controller/ConsoleControllerFactoryTest.php create mode 100644 tests/RbCommentTest/Factory/Model/CommentTableFactoryTest.php 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); + } +} From 59a0790d632946b21c6b193fe990f2056f6e7490 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Sun, 29 Jan 2017 21:41:08 +0000 Subject: [PATCH 14/17] Add more tests for the factories --- .../Model/RbCommentTableGatewayFactory.php | 1 + .../Mvc/Controller/Plugin/MailerFactory.php | 2 +- .../RbCommentTableGatewayFactoryTest.php | 33 ++++++++++ .../Controller/Plugin/MailerFactoryTest.php | 54 +++++++++++++++ .../Service/AkismetServiceFactoryTest.php | 65 +++++++++++++++++++ .../View/Helper/CommentFactoryTest.php | 53 +++++++++++++++ 6 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 tests/RbCommentTest/Factory/Model/RbCommentTableGatewayFactoryTest.php create mode 100644 tests/RbCommentTest/Factory/Mvc/Controller/Plugin/MailerFactoryTest.php create mode 100644 tests/RbCommentTest/Factory/Service/AkismetServiceFactoryTest.php create mode 100644 tests/RbCommentTest/Factory/View/Helper/CommentFactoryTest.php diff --git a/src/RbComment/Factory/Model/RbCommentTableGatewayFactory.php b/src/RbComment/Factory/Model/RbCommentTableGatewayFactory.php index 06bbf10..a431167 100644 --- a/src/RbComment/Factory/Model/RbCommentTableGatewayFactory.php +++ b/src/RbComment/Factory/Model/RbCommentTableGatewayFactory.php @@ -21,6 +21,7 @@ final class RbCommentTableGatewayFactory implements FactoryInterface public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $databaseAdapter = $container->get(Adapter::class); + $resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new Comment()); diff --git a/src/RbComment/Factory/Mvc/Controller/Plugin/MailerFactory.php b/src/RbComment/Factory/Mvc/Controller/Plugin/MailerFactory.php index 6896609..bfaf531 100644 --- a/src/RbComment/Factory/Mvc/Controller/Plugin/MailerFactory.php +++ b/src/RbComment/Factory/Mvc/Controller/Plugin/MailerFactory.php @@ -16,7 +16,7 @@ final class MailerFactory implements FactoryInterface */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - return new Comment( + return new Mailer( $container->get('ViewHelperManager')->get('serverUrl'), $container->get('RbComment\Mailer'), $container->get('Config') 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); + } +} From 6db971e3713966e518ac0a2ed24a9f19762aae69 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Fri, 3 Feb 2017 21:48:01 +0000 Subject: [PATCH 15/17] Fix serverUrl invokable call in the mailer plugin --- src/RbComment/Mvc/Controller/Plugin/Mailer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RbComment/Mvc/Controller/Plugin/Mailer.php b/src/RbComment/Mvc/Controller/Plugin/Mailer.php index 87816f6..3d80b88 100755 --- a/src/RbComment/Mvc/Controller/Plugin/Mailer.php +++ b/src/RbComment/Mvc/Controller/Plugin/Mailer.php @@ -28,7 +28,7 @@ public function __invoke($comment) $htmlContent = $comment->content; $htmlContent .= '

'; - $htmlContent .= '' . + $htmlContent .= '' . $mailerConfig['context_link_text'] . ''; From 5ae51420a856c73bab4720ad2857afe86e251771 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Fri, 3 Feb 2017 21:56:06 +0000 Subject: [PATCH 16/17] Fix CS issues --- src/RbComment/Mvc/Controller/Plugin/Mailer.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/RbComment/Mvc/Controller/Plugin/Mailer.php b/src/RbComment/Mvc/Controller/Plugin/Mailer.php index 3d80b88..092d97e 100755 --- a/src/RbComment/Mvc/Controller/Plugin/Mailer.php +++ b/src/RbComment/Mvc/Controller/Plugin/Mailer.php @@ -28,9 +28,10 @@ public function __invoke($comment) $htmlContent = $comment->content; $htmlContent .= '

'; - $htmlContent .= '' . - $mailerConfig['context_link_text'] . - ''; + $htmlContent .= + '' . + $mailerConfig['context_link_text'] . + ''; $html = new MimePart($htmlContent); $html->type = "text/html"; From 0015a59cb778aafcf5e46d4888b5787d3e1a01ce Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Sat, 4 Feb 2017 23:09:39 +0000 Subject: [PATCH 17/17] Prepare branch for release --- .gitattributes | 7 +++++++ LICENSE.md | 2 +- README.md | 4 ++-- composer.json | 6 +++--- 4 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 .gitattributes 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/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/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 a4176ff..a25530d 100644 --- a/composer.json +++ b/composer.json @@ -7,13 +7,13 @@ "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" } ], @@ -53,7 +53,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.5.x-dev" + "dev-master": "2.0.x-dev" } } }