Skip to content

Commit

Permalink
initial tests, test for Event
Browse files Browse the repository at this point in the history
  • Loading branch information
igorw committed Dec 31, 2011
1 parent 070b860 commit 6e58b04
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
composer.lock
vendor
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -125,6 +125,18 @@ set_time_limit(0);
ini_set('memory_limit', '512M');
```

Tests
-----

Before running the tests you need to have composer set up an autoloader:

$ wget http://getcomposer.org/composer.phar
$ php composer.phar install

Now you can run the unit tests.

$ phpunit

License
-------
MIT, see LICENSE.
19 changes: 19 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="EventSource Test Suite">
<directory>./tests/Igorw/</directory>
</testsuite>
</testsuites>
</phpunit>
8 changes: 4 additions & 4 deletions src/Igorw/EventSource/Event.php
Expand Up @@ -24,21 +24,21 @@ public function addComment($comment)
$this->comments,
$this->extractNewlines($comment)
);

return $this;
}

public function setId($id = null)
{
$this->id = $id;

return $this;
}

public function setEvent($event = null)
{
$this->event = $event;

return $this;
}

Expand All @@ -65,7 +65,7 @@ public function dump()
$this->getFormattedId().
$this->getFormattedEvent().
$this->getFormattedData();

return '' !== $response ? $response."\n" : '';
}

Expand Down
116 changes: 116 additions & 0 deletions tests/Igorw/Tests/EventSource/EventTest.php
@@ -0,0 +1,116 @@
<?php

/*
* This file is part of EventSource.
*
* (c) Igor Wiedler <igor@wiedler.ch>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Igorw\Tests\EventSource;

use Igorw\EventSource\Event;

class EventTest extends \PHPUnit_Framework_TestCase
{
public function testInitialFormattedValuesShouldBeEmpty()
{
$event = new Event();
$this->assertSame('', $event->getFormattedComments());
$this->assertSame('', $event->getFormattedId());
$this->assertSame('', $event->getFormattedEvent());
$this->assertSame('', $event->getFormattedData());
}

public function testCommentFormatting()
{
$event = new Event();
$event->addComment('a comment');
$this->assertSame(": a comment\n", $event->getFormattedComments());
}

public function testCommentFormattingWithManyComments()
{
$event = new Event();
$event->addComment('a comment');
$event->addComment('a second comment');
$event->addComment('another comment');
$this->assertSame(": a comment\n: a second comment\n: another comment\n", $event->getFormattedComments());
}

public function testIdFormatting()
{
$event = new Event();
$event->setId('1');
$this->assertSame("id: 1\n", $event->getFormattedId());
}

public function testIdOverride()
{
$event = new Event();
$event->setId('1');
$this->assertSame("id: 1\n", $event->getFormattedId());
$event->setId('2');
$this->assertSame("id: 2\n", $event->getFormattedId());
}

public function testEventFormatting()
{
$event = new Event();
$event->setEvent('foo');
$this->assertSame("event: foo\n", $event->getFormattedEvent());
}

public function testEventOverride()
{
$event = new Event();
$event->setEvent('foo');
$this->assertSame("event: foo\n", $event->getFormattedEvent());
$event->setEvent('bar');
$this->assertSame("event: bar\n", $event->getFormattedEvent());
}

public function testDataFormatting()
{
$event = new Event();
$event->setData('happy new year');
$this->assertSame("data: happy new year\n", $event->getFormattedData());
}

public function testDataFormattingWithManyLines()
{
$event = new Event();
$event->setData("we wish you a merry christmas\nand a happy new year");
$this->assertSame("data: we wish you a merry christmas\ndata: and a happy new year\n", $event->getFormattedData());
}

public function testDataFormattingWithAppend()
{
$event = new Event();
$event->appendData('we wish you a merry christmas');
$event->appendData('and a happy new year');
$this->assertSame("data: we wish you a merry christmas\ndata: and a happy new year\n", $event->getFormattedData());
}

public function testDumpIncludesEverything()
{
$event = new Event();
$event->addComment('a juicy comment');
$event->setId('11');
$event->setEvent('foo');
$event->setData("we wish you a merry christmas\nand a happy new year");

$expected = <<<EOT
: a juicy comment
id: 11
event: foo
data: we wish you a merry christmas
data: and a happy new year
EOT;
$this->assertSame($expected, $event->dump());
}
}
5 changes: 5 additions & 0 deletions tests/bootstrap.php
@@ -0,0 +1,5 @@
<?php

$loader = require __DIR__.'/../vendor/.composer/autoload.php';
$loader->add('Igorw\Tests', __DIR__);
$loader->register();

0 comments on commit 6e58b04

Please sign in to comment.