Skip to content

Commit

Permalink
clean some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-23 committed Jul 16, 2015
1 parent 0443771 commit a01feb9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 46 deletions.
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
21 changes: 10 additions & 11 deletions Services/Impl/AkismetServiceImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,37 @@ class AkismetServiceImpl implements AkismetService
*/
public function commentCheck(Comment $comment)
{
$this->overLoadComment($comment);

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

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

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

return $comment;
}

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

return $this->akismet->submitSpam($comment);
return $this->akismet->submitSpam($this->completeComment($comment));
}

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

return $this->akismet->submitHam($comment);
return $this->akismet->submitHam($this->completeComment($comment));
}

public function setAkismet(AkismetService $akismet)
Expand Down
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 @@ -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.

30 changes: 25 additions & 5 deletions Tests/Services/Impl/AkismetServiceImplTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
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 @@ -19,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 Down Expand Up @@ -106,18 +112,32 @@ private function assertCommentCheckParams()
protected function setUp()
{
$this->akismetService = new AkismetServiceImpl();
$this->akismetService->setAkismet($this->getAkismet());
$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;
}
}

0 comments on commit a01feb9

Please sign in to comment.