Skip to content

Commit

Permalink
Merge pull request FriendsOfSymfony#174 from merk/more_testing
Browse files Browse the repository at this point in the history
More testing
  • Loading branch information
merk committed Apr 9, 2012
2 parents ba7fedd + 8c76198 commit fd49915
Show file tree
Hide file tree
Showing 28 changed files with 410 additions and 93 deletions.
6 changes: 4 additions & 2 deletions .gitignore
@@ -1,4 +1,6 @@
composer.phar
phpunit.xml
Tests/autoload.php

vendor/*
Tests/Functional/app/Behat/nginx.conf
Tests/Functional/web/bundles
vendor
15 changes: 12 additions & 3 deletions .travis.yml
Expand Up @@ -5,10 +5,19 @@ php:
- 5.4

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --install-suggests --quiet
- export DISPLAY=:99.0 && sh -e /etc/init.d/xvfb start
- wget --quiet -O sahi.zip http://sourceforge.net/projects/sahi/files/sahi-v35/20110719/sahi_20110719.zip/download && unzip -qq sahi.zip
- export SAHI_HOME=sahi && sh sahi/bin/sahi.sh > /dev/null &
- curl --silent http://getcomposer.org/installer | php > /dev/null && php composer.phar install --install-suggests --quiet
- sudo apt-get -qq install nginx > /dev/null 2>&1
- nginx -v && sudo nginx -c app/Behat/nginx.conf.dist -p ./Tests/Functional
- for file in `find /home/vagrant/.phpenv -name 'xdebug.ini'`; do echo "xdebug.max_nesting_level=200" >> $file; done;
- php-cgi -b 127.0.0.1:9000 &
- ./Tests/Functional/app/console assets:install --symlink Tests/Functional/web > /dev/null && ./Tests/Functional/app/console doctrine:database:drop --force > /dev/null && ./Tests/Functional/app/console doctrine:database:create > /dev/null && ./Tests/Functional/app/console doctrine:schema:create

script: ./run_tests.sh

notifications:
email:
- friendsofsymfony-dev@googlegroups.com
- tim+build@nagel.com.au
- tim+build@nagel.com.au
18 changes: 11 additions & 7 deletions EventListener/ThreadPermalinkListener.php
Expand Up @@ -15,8 +15,8 @@
use FOS\CommentBundle\Event\ThreadEvent;
use FOS\CommentBundle\Model\ThreadInterface;
use FOS\CommentBundle\Model\ThreadManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* Responsible for setting a permalink for each new Thread object.
Expand All @@ -26,18 +26,18 @@
class ThreadPermalinkListener implements EventSubscriberInterface
{
/**
* @var Request
* @var ContainerInterface
*/
protected $request;
protected $container;

/**
* Constructor.
*
* @param Request $request
* @param ContainerInterface $container
*/
public function __construct(Request $request)
public function __construct(ContainerInterface $container)
{
$this->request = $request;
$this->container = $container;
}

/**
Expand All @@ -47,8 +47,12 @@ public function __construct(Request $request)
*/
public function onThreadCreate(ThreadEvent $event)
{
if (!$this->container->isScopeActive('request')) {
return;
}

$thread = $event->getThread();
$thread->setPermalink($this->request->getUri());
$thread->setPermalink($this->container->get('request')->getUri());
}

static public function getSubscribedEvents()
Expand Down
36 changes: 36 additions & 0 deletions Features/Context/FeatureContext.php
@@ -0,0 +1,36 @@
<?php

namespace FOS\CommentBundle\Features\Context;

use Behat\BehatBundle\Context\MinkContext;
use Behat\Behat\Exception\PendingException;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* Feature context.
*/
class FeatureContext extends MinkContext
{
/**
* @Given /^I have a thread identified by "([^"]*)" with a link of "([^"]*)"$/
*/
public function iHaveAThreadIdentifiedBy($id, $link)
{
$thread = $this->getThreadManager()->findThreadById($id);
if (!$thread) {
$thread = $this->getThreadManager()->createThread($id);
}

$thread->setPermalink($link);

$this->getThreadManager()->saveThread($thread);
}

/**
* @return \FOS\CommentBundle\Model\ThreadManagerInterface
*/
private function getThreadManager()
{
return $this->getContainer()->get('fos_comment.manager.thread');
}
}
20 changes: 20 additions & 0 deletions Features/EmbedThread.feature
@@ -0,0 +1,20 @@
Feature: Embed a thread on a page

Background:
Given I have a thread identified by "test" with a link of "test"

Scenario: Embed a thread inline
When I go to "inline/test"
Then I should see a "#fos_comment_thread[data-thread=test]" element

@javascript
Scenario: Embed a thread async
When I go to "async/test"
Then I should see a "#fos_comment_thread[data-thread=test]" element

@javascript
Scenario: Reply to a thread
When I go to "async/test"
And I fill in "fos_comment_comment_body" with "I am replying to a comment"
And I press "fos_comment_comment_new_submit"
Then I should see "I am replying to a comment" in the ".fos_comment_comment_body" element
1 change: 1 addition & 0 deletions Resources/assets/js/comments.js
Expand Up @@ -96,6 +96,7 @@
{permalink: encodeURIComponent(permalink)},
function(data) {
FOS_COMMENT.thread_container.html(data);
FOS_COMMENT.thread_container.attr('data-thread', identifier);
}
);
},
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/events.xml
Expand Up @@ -24,8 +24,8 @@
<tag name="kernel.event_listener" event="fos_comment.comment.pre_persist" method="onCommentPersist" />
</service>

<service id="fos_comment.listener.thread_permalink" class="FOS\CommentBundle\EventListener\ThreadPermalinkListener" scope="request">
<argument type="service" id="request" />
<service id="fos_comment.listener.thread_permalink" class="FOS\CommentBundle\EventListener\ThreadPermalinkListener">
<argument type="service" id="service_container" />
<!-- TODO: 2.1 <tag name="kernel.event_subscriber" /> -->
<tag name="kernel.event_listener" event="fos_comment.thread.create" method="onThreadCreate" />
</service>
Expand Down
4 changes: 2 additions & 2 deletions Resources/doc/11-running_the_test_suite.md
Expand Up @@ -25,8 +25,8 @@ Functional tests have more dependencies.

* vendors set up by [Composer](http://getcomposer.org)
* `php composer.phar install --install-suggests`
* [Node.js](http://nodejs.org/)
* [Zombie.js](https://github.com/assaf/zombie)
* Sahi
* A browser supported by Sahi (headless or otherwise)
* A webserver with ability to parse PHP
* Once vendors are installed, a few commands must be run to set up the environment
* ./Tests/Functional/app/console assets:install --symlink Tests/Functional/web
Expand Down
4 changes: 4 additions & 0 deletions Tests/Entity/CommentManagerTest.php
Expand Up @@ -29,6 +29,10 @@ class CommentManagerTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
if (!class_exists('Doctrine\\ORM\\EntityManager')) {
$this->markTestSkipped('Doctrine ORM not installed');
}

$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
Expand Down
4 changes: 4 additions & 0 deletions Tests/Entity/ThreadManagerTest.php
Expand Up @@ -28,6 +28,10 @@ class ThreadManagerTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
if (!class_exists('Doctrine\\ORM\\EntityManager')) {
$this->markTestSkipped('Doctrine ORM not installed');
}

$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
Expand Down
4 changes: 4 additions & 0 deletions Tests/Entity/VoteManagerTest.php
Expand Up @@ -28,6 +28,10 @@ class VoteManagerTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
if (!class_exists('Doctrine\\ORM\\EntityManager')) {
$this->markTestSkipped('Doctrine ORM not installed');
}

$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
Expand Down
Expand Up @@ -44,10 +44,7 @@ public function inlineAction(Request $request, $id)

return $this->render('CommentBundle:Comment:inline.html.twig', array(
'comments' => $comments,
'displayDepth' => null,
'sorter' => null,
'thread' => $thread,
'view' => 'tree',
));
}
}
2 changes: 1 addition & 1 deletion Tests/Functional/Bundle/CommentBundle/Entity/Comment.php
Expand Up @@ -42,7 +42,7 @@ class Comment extends BaseComment implements SignedCommentInterface, VotableComm
protected $thread;

/**
* @ORM\Column(type="string")
* @ORM\Column(type="string", nullable=true)
* @var string
*/
protected $author;
Expand Down
@@ -1,5 +1,6 @@
{% extends "::base.html.twig"%}

{% block body %}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
{% include 'FOSCommentBundle:Thread:async.html.twig' with { 'id': id } %}
{% endblock body %}
@@ -1,19 +1,17 @@
{% extends "::base.html.twig" %}

{% block body %}
<div id="fos_comment_thread"></div>
<div id="fos_comment_thread" data-thread="{{ thread.id }}"></div>

{% include 'FOSCommentBundle:Thread:comments.html.twig' with {
'comments': comments,
'displayDepth': displayDepth,
'sorter': sorter,
'thread': thread,
'view': view
'thread': thread
} %}
{% endblock body %}

{% block javascript %}
{% javascripts '@FOSCommentBundle/Resources/assets/js/comments.js' %}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="{{ asset_url }}">
// URI identifier for the thread comments
var fos_comment_thread_id = '{{ path('fos_comment_get_thread_comments', {'id': thread.id}) }}';
Expand Down
131 changes: 131 additions & 0 deletions Tests/Functional/SymfonyDriver.php
@@ -0,0 +1,131 @@
<?php

namespace FOS\CommentBundle\Tests\Functional;

use Behat\Mink\Driver\GoutteDriver;
use Symfony\Component\BrowserKit\Client;

/*
* This file has been duplicated from Behat, a private $client
* does not allow us to overwrite the "new $class()" call because
* our kernel has different parameters to the normal Kernel.
*/

/**
* Symfony2 Mink driver.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class SymfonyDriver extends GoutteDriver
{
/**
* Initializes Goutte driver.
*
* @param Symfony\Component\BrowserKit\Client $client BrowserKit client instance
*/
public function __construct(Client $client = null)
{
// create new kernel, that could be easily rebooted
$class = get_class($client->getKernel());
$kernel = new $class('Behat', 'config.yml', $client->getKernel()->getEnvironment(), $client->getKernel()->isDebug());
$kernel->boot();

parent::__construct($kernel->getContainer()->get('test.client'));
}

/**
* {@inheritdoc}
*
* removes "*.php/" from urls and then passes it to GoutteDriver::visit().
*/
public function visit($url)
{
$url = preg_replace('/^(https?\:\/\/[^\/]+)(\/[^\/]+\.php)?/', '$1', $url);
parent::visit($url);
}

/**
* {@inheritdoc}
*/
public function reset()
{
parent::reset();

$this->getClient()->getKernel()->shutdown();
$this->getClient()->getKernel()->boot();
}

/**
* {@inheritdoc}
*/
public function setBasicAuth($user, $password)
{
$this->getClient()->setServerParameter('PHP_AUTH_USER', $user);
$this->getClient()->setServerParameter('PHP_AUTH_PW', $password);
}

/**
* {@inheritdoc}
*/
public function setRequestHeader($name, $value)
{
switch (strtolower($name)) {
case 'accept':
$name = 'HTTP_ACCEPT';
break;
case 'accept-charset':
$name = 'HTTP_ACCEPT_CHARSET';
break;
case 'accept-encoding':
$name = 'HTTP_ACCEPT_ENCODING';
break;
case 'accept-language':
$name = 'HTTP_ACCEPT_LANGUAGE';
break;
case 'connection':
$name = 'HTTP_CONNECTION';
break;
case 'host':
$name = 'HTTP_HOST';
break;
case 'user-agent':
$name = 'HTTP_USER_AGENT';
break;
case 'authorization':
$name = 'PHP_AUTH_DIGEST';
break;
}

$this->getClient()->setServerParameter($name, $value);
}

/**
* {@inheritDoc}
*/
public function getResponseHeaders()
{
$headers = array();
$responseHeaders = trim($this->getClient()->getResponse()->headers->__toString());

foreach (explode("\r\n", $responseHeaders) as $header) {
list($name, $value) = array_map('trim', explode(':', $header, 2));

if (isset($headers[$name])) {
$headers[$name] = array($headers[$name]);
$headers[$name][] = $value;
} else {
$headers[$name] = $value;
}
}

return $headers;
}

/**
* {@inheritdoc}
*/
public function getStatusCode()
{
return $this->getClient()->getResponse()->getStatusCode();
}
}

0 comments on commit fd49915

Please sign in to comment.