Skip to content

Commit

Permalink
Merge pull request #2 from OpenClassrooms/OC-3931_add_spam_button
Browse files Browse the repository at this point in the history
Oc 3931 add spam button
  • Loading branch information
arnaud-23 committed Jul 29, 2015
2 parents 1edcb00 + a01feb9 commit 7e83896
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 44 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
language: php

php:
- "5.4"
- "5.5"
- "5.6"

before_script:
- composer self-update
- composer install --no-scripts --no-interaction --dev
- composer install --no-scripts --no-interaction
- composer dump-autoload -o
- phpenv config-add ./Tests/travis.ini

Expand Down
1 change: 0 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>

<service id="openclassrooms.akismet.client" class="OpenClassrooms\Akismet\Client\Impl\ClientImpl">
<argument>%openclassrooms.akismet.key%</argument>
<argument>%openclassrooms.akismet.blog%</argument>
Expand Down
31 changes: 28 additions & 3 deletions Services/Impl/AkismetServiceImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,41 @@ class AkismetServiceImpl implements AkismetService
private $requestStack;

/**
* @return bool
* {@inheritdoc}
*/
public function commentCheck(Comment $comment)
{
$request = $this->requestStack->getCurrentRequest();
return $this->akismet->commentCheck($this->completeComment($comment));
}

/**
* @return Comment
*/
private function completeComment(Comment $comment)
{
$request = $this->requestStack->getMasterRequest();

$comment->setUserIp($request->getClientIp());
$comment->setUserAgent($request->headers->get('User-Agent'));
$comment->setReferrer($request->headers->get('referrer'));

return $this->akismet->commentCheck($comment);
return $comment;
}

/**
* {@inheritdoc}
*/
public function submitSpam(Comment $comment)
{
return $this->akismet->submitSpam($this->completeComment($comment));
}

/**
* {@inheritdoc}
*/
public function submitHam(Comment $comment)
{
return $this->akismet->submitHam($this->completeComment($comment));
}

public function setAkismet(AkismetService $akismet)
Expand All @@ -45,4 +69,5 @@ public function setRequestStack(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use OpenClassrooms\Bundle\AkismetBundle\DependencyInjection\OpenClassroomsAkismetExtension;
use OpenClassrooms\Bundle\AkismetBundle\OpenClassroomsAkismetBundle;
use OpenClassrooms\Bundle\AkismetBundle\Tests\Doubles\HttpFoundation\RequestStackMock;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* @author Arnaud Lefèvre <arnaud.lefevre@openclassrooms.com>
Expand Down Expand Up @@ -76,7 +76,7 @@ public function Configuration()
/** @var \GuzzleHttp\Client $guzzle */
$guzzle = $rp->getValue($client);

$this->assertEquals($expectedBaseUrl, $guzzle->getBaseUrl());
$this->assertEquals($expectedBaseUrl, $guzzle->getConfig('base_uri'));
}

/**
Expand All @@ -94,7 +94,7 @@ protected function setUp()
{
$this->container = new ContainerBuilder();
$this->extension = new OpenClassroomsAkismetExtension();
$this->container->set('request_stack', new RequestStackMock());
$this->container->set('request_stack', new RequestStack());
$this->container->registerExtension($this->extension);
$this->container->loadFromExtension('open_classrooms_akismet');
$this->configLoader = new YamlFileLoader(
Expand Down
27 changes: 0 additions & 27 deletions Tests/Doubles/HttpFoundation/RequestStackMock.php

This file was deleted.

78 changes: 71 additions & 7 deletions Tests/Services/Impl/AkismetServiceImplTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace OpenClassrooms\Bundle\AkismetBundle\Tests\Services\Impl;

use OpenClassrooms\Akismet\Models\Impl\CommentBuilderImpl;
use OpenClassrooms\Akismet\Models\Resource;
use OpenClassrooms\Akismet\Services\AkismetService;
use OpenClassrooms\Akismet\Tests\Models\CommentStub;
use OpenClassrooms\Akismet\Services\Impl\AkismetServiceImpl as Akismet;
use OpenClassrooms\Akismet\Tests\Models\CommentStub;
use OpenClassrooms\Bundle\AkismetBundle\Services\Impl\AkismetServiceImpl;
use OpenClassrooms\Bundle\AkismetBundle\Tests\Doubles\Client\ClientMock;
use OpenClassrooms\Bundle\AkismetBundle\Tests\Doubles\HttpFoundation\RequestStackMock;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Class AkismetServiceImplTest
Expand All @@ -18,8 +20,13 @@
class AkismetServiceImplTest extends \PHPUnit_Framework_TestCase
{
const KEY = '123APIKey';

const BLOG_URL = 'http://www.blogdomainname.com/';

const USER_AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6';

const REFERRER = 'http://www.google.com';

/**
* @var AkismetService
*/
Expand All @@ -30,6 +37,8 @@ class AkismetServiceImplTest extends \PHPUnit_Framework_TestCase
*/
public function commentCheck()
{
ClientMock::$postReturn = 'true';

$commentBuilder = new CommentBuilderImpl();

$response = $this->akismetService->commentCheck(
Expand All @@ -43,7 +52,49 @@ public function commentCheck()
);

$this->assertTrue($response);
$this->assertEquals(AkismetService::RESOURCE, ClientMock::$resource);
$this->assertEquals(Resource::COMMENT_CHECK, ClientMock::$resource);
$this->assertCommentCheckParams();
}

/**
* @test
*/
public function submitSpam()
{
$commentBuilder = new CommentBuilderImpl();

$this->akismetService->submitSpam(
$commentBuilder
->create()
->withPermalink(CommentStub::PERMALINK)
->withAuthorName(CommentStub::AUTHOR_NAME)
->withAuthorEmail(CommentStub::AUTHOR_EMAIL)
->withContent(CommentStub::CONTENT)
->build()
);

$this->assertEquals(Resource::SUBMIT_SPAM, ClientMock::$resource);
$this->assertCommentCheckParams();
}

/**
* @test
*/
public function submitHam()
{
$commentBuilder = new CommentBuilderImpl();

$this->akismetService->submitHam(
$commentBuilder
->create()
->withPermalink(CommentStub::PERMALINK)
->withAuthorName(CommentStub::AUTHOR_NAME)
->withAuthorEmail(CommentStub::AUTHOR_EMAIL)
->withContent(CommentStub::CONTENT)
->build()
);

$this->assertEquals(Resource::SUBMIT_HAM, ClientMock::$resource);
$this->assertCommentCheckParams();
}

Expand All @@ -61,19 +112,32 @@ private function assertCommentCheckParams()
protected function setUp()
{
$this->akismetService = new AkismetServiceImpl();
$this->akismetService->setAkismet($this->getAkismet());
ClientMock::$postReturn = true;
$this->akismetService->setRequestStack(new RequestStackMock());
$this->akismetService->setAkismet($this->buildAkismet());
$this->akismetService->setRequestStack($this->buildRequestStack());
}

/**
* @return Akismet
*/
private function getAkismet()
private function buildAkismet()
{
$akismet = new Akismet();
$akismet->setClient(new ClientMock());

return $akismet;
}

/**
* @return RequestStack
*/
protected function buildRequestStack()
{
$request = Request::create('http://localhost');
$request->headers->set('User-Agent', self::USER_AGENT);
$request->headers->set('referrer', self::REFERRER);
$requestStack = new RequestStack();
$requestStack->push($request);

return $requestStack;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"symfony/dependency-injection": "~2.6",
"symfony/config": "~2.6",

"openclassrooms/akismet": "1.0.x-dev"
"openclassrooms/akismet": "dev-OC-3931_add_spam_button@dev"
},
"require-dev": {
"phpunit/phpunit": "~4.7",
Expand Down

0 comments on commit 7e83896

Please sign in to comment.