Permalink
Browse files

initial tests, test for Event

  • Loading branch information...
1 parent 070b860 commit 6e58b041e8e696c5e6cd0ef4ba08815e875ac7b6 @igorw committed Dec 31, 2011
Showing with 158 additions and 4 deletions.
  1. +2 −0 .gitignore
  2. +12 −0 README.md
  3. +19 −0 phpunit.xml.dist
  4. +4 −4 src/Igorw/EventSource/Event.php
  5. +116 −0 tests/Igorw/Tests/EventSource/EventTest.php
  6. +5 −0 tests/bootstrap.php
View
2 .gitignore
@@ -0,0 +1,2 @@
+composer.lock
+vendor
View
12 README.md
@@ -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.
View
19 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>
View
8 src/Igorw/EventSource/Event.php
@@ -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;
}
@@ -65,7 +65,7 @@ public function dump()
$this->getFormattedId().
$this->getFormattedEvent().
$this->getFormattedData();
-
+
return '' !== $response ? $response."\n" : '';
}
View
116 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());
+ }
+}
View
5 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.