Permalink
Browse files

Updated testing - run phpunit from root of project.

  • Loading branch information...
ngnpope committed Jul 16, 2010
1 parent 5bd8891 commit 8e9708354ff5f107f200d77a6703adfb5df0319e
Showing with 294 additions and 21 deletions.
  1. +6 −1 phpunit.xml
  2. +145 −0 tests/Twitter/AutolinkTest.php
  3. +106 −0 tests/Twitter/ExtractorTest.php
  4. +18 −3 tests/bootstrap.php
  5. +19 −17 tests/runtests.php
View
@@ -1,4 +1,5 @@
-<phpunit bootstrap="tests/bootstrap.php"
+<phpunit backupGlobals="true"
+ bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
@@ -24,4 +25,8 @@
<log type="plain" target="build/logs/plain.txt" />
</logging>
+<php>
+ <const name="DATA" value="tests/data/twitter-text-conformance" />
+</php>
+
</phpunit>
@@ -0,0 +1,145 @@
+<?php
+/**
+ * @author Nick Pope <nick@nickpope.me.uk>
+ * @copyright Copyright © 2010, Mike Cochrane, Nick Pope
+ * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
+ * @package Twitter
+ */
+
+/**
+ * Twitter Autolink Class Unit Tests
+ *
+ * @author Nick Pope <nick@nickpope.me.uk>
+ * @copyright Copyright © 2010, Mike Cochrane, Nick Pope
+ * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
+ * @package Twitter
+ */
+class Twitter_AutolinkTest extends PHPUnit_Framework_TestCase {
+
+ /**
+ * A helper function for providers.
+ *
+ * @param string $test The test to fetch data for.
+ *
+ * @return array The test data to provide.
+ */
+ protected function providerHelper($test) {
+ $data = Spyc::YAMLLoad(DATA.'/autolink.yml');
+ return isset($data['tests'][$test]) ? $data['tests'][$test] : array();
+ }
+
+ /**
+ * @dataProvider addLinksToUsernamesProvider
+ */
+ public function testAddLinksToUsernames($description, $text, $expected) {
+ $linked = Twitter_Autolink::create($text, false)
+ ->setNoFollow(false)->setExternal(false)->setTarget('')
+ ->setUsernameClass('tweet-url username')
+ ->setListClass('tweet-url list-slug')
+ ->setHashtagClass('tweet-url hashtag')
+ ->setURLClass('')
+ ->addLinksToUsernamesAndLists();
+ $this->assertSame($expected, $linked, $description);
+ }
+
+ /**
+ *
+ */
+ public function addLinksToUsernamesProvider() {
+ return $this->providerHelper('usernames');
+ }
+
+ /**
+ * @dataProvider addLinksToListsProvider
+ */
+ public function testAddLinksToLists($description, $text, $expected) {
+ $linked = Twitter_Autolink::create($text, false)
+ ->setNoFollow(false)->setExternal(false)->setTarget('')
+ ->setUsernameClass('tweet-url username')
+ ->setListClass('tweet-url list-slug')
+ ->setHashtagClass('tweet-url hashtag')
+ ->setURLClass('')
+ ->addLinksToUsernamesAndLists();
+ $this->assertSame($expected, $linked, $description);
+ }
+
+ /**
+ *
+ */
+ public function addLinksToListsProvider() {
+ return $this->providerHelper('lists');
+ }
+
+ /**
+ * @dataProvider addLinksToHashtagsProvider
+ */
+ public function testAddLinksToHashtags($description, $text, $expected) {
+ $linked = Twitter_Autolink::create($text, false)
+ ->setNoFollow(false)->setExternal(false)->setTarget('')
+ ->setUsernameClass('tweet-url username')
+ ->setListClass('tweet-url list-slug')
+ ->setHashtagClass('tweet-url hashtag')
+ ->setURLClass('')
+ ->addLinksToHashtags();
+ # XXX: Need to re-order for hashtag as it is written out differently...
+ # We use the same wrapping function for adding links for all methods.
+ $linked = preg_replace(array(
+ '!<a class="([^"]*)" href="([^"]*)">([^<]*)</a>!',
+ '!title="#([^"]+)"!'
+ ), array(
+ '<a href="$2" title="$3" class="$1">$3</a>',
+ 'title="#$1"'
+ ), $linked);
+ $this->assertSame($expected, $linked, $description);
+ }
+
+ /**
+ *
+ */
+ public function addLinksToHashtagsProvider() {
+ return $this->providerHelper('hashtags');
+ }
+
+ /**
+ * @dataProvider addLinksToURLsProvider
+ */
+ public function testAddLinksToURLs($description, $text, $expected) {
+ $linked = Twitter_Autolink::create($text, false)
+ ->setNoFollow(false)->setExternal(false)->setTarget('')
+ ->setUsernameClass('tweet-url username')
+ ->setListClass('tweet-url list-slug')
+ ->setHashtagClass('tweet-url hashtag')
+ ->setURLClass('')
+ ->addLinksToURLs();
+ $this->assertSame($expected, $linked, $description);
+ }
+
+ /**
+ *
+ */
+ public function addLinksToURLsProvider() {
+ return $this->providerHelper('urls');
+ }
+
+ /**
+ * @dataProvider addLinksProvider
+ */
+ public function testAddLinks($description, $text, $expected) {
+ $linked = Twitter_Autolink::create($text, false)
+ ->setNoFollow(false)->setExternal(false)->setTarget('')
+ ->setUsernameClass('tweet-url username')
+ ->setListClass('tweet-url list-slug')
+ ->setHashtagClass('tweet-url hashtag')
+ ->setURLClass('')
+ ->addLinks();
+ $this->assertSame($expected, $linked, $description);
+ }
+
+ /**
+ *
+ */
+ public function addLinksProvider() {
+ return $this->providerHelper('all');
+ }
+
+}
@@ -0,0 +1,106 @@
+<?php
+/**
+ * @author Nick Pope <nick@nickpope.me.uk>
+ * @copyright Copyright © 2010, Mike Cochrane, Nick Pope
+ * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
+ * @package Twitter
+ */
+
+/**
+ * Twitter Extractor Class Unit Tests
+ *
+ * @author Nick Pope <nick@nickpope.me.uk>
+ * @copyright Copyright © 2010, Mike Cochrane, Nick Pope
+ * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
+ * @package Twitter
+ */
+class Twitter_ExtractorTest extends PHPUnit_Framework_TestCase {
+
+ /**
+ * A helper function for providers.
+ *
+ * @param string $test The test to fetch data for.
+ *
+ * @return array The test data to provide.
+ */
+ protected function providerHelper($test) {
+ $data = Spyc::YAMLLoad(DATA.'/extract.yml');
+ return isset($data['tests'][$test]) ? $data['tests'][$test] : array();
+ }
+
+ /**
+ * @dataProvider extractMentionedUsernamesProvider
+ */
+ public function testExtractMentionedUsernames($description, $text, $expected) {
+ $extracted = Twitter_Extractor::create($text)->extractMentionedUsernames();
+ $this->assertSame($expected, $extracted, $description);
+ }
+
+ /**
+ *
+ */
+ public function extractMentionedUsernamesProvider() {
+ return $this->providerHelper('mentions');
+ }
+
+ /**
+ * @dataProvider extractRepliedUsernamesProvider
+ */
+ public function testExtractRepliedUsernames($description, $text, $expected) {
+ $extracted = Twitter_Extractor::create($text)->extractRepliedUsernames();
+ $this->assertSame($expected, $extracted, $description);
+ }
+
+ /**
+ *
+ */
+ public function extractRepliedUsernamesProvider() {
+ return $this->providerHelper('replies');
+ }
+
+ /**
+ * @dataProvider extractURLsProvider
+ */
+ public function testExtractURLs($description, $text, $expected) {
+ $extracted = Twitter_Extractor::create($text)->extractURLs();
+ $this->assertSame($expected, $extracted, $description);
+ }
+
+ /**
+ *
+ */
+ public function extractURLsProvider() {
+ return $this->providerHelper('urls');
+ }
+
+ /**
+ * @dataProvider extractHashtagsProvider
+ */
+ public function testExtractHashtags($description, $text, $expected) {
+ $extracted = Twitter_Extractor::create($text)->extractHashtags();
+ $this->assertSame($expected, $extracted, $description);
+ }
+
+ /**
+ *
+ */
+ public function extractHashtagsProvider() {
+ return $this->providerHelper('hashtags');
+ }
+
+ /**
+ * @dataProvider extractHashtagsWithIndicesProvider
+ */
+ public function testExtractHashtagsWithIndices($description, $text, $expected) {
+ $extracted = Twitter_Extractor::create($text)->extractHashtagsWithIndices();
+ $this->assertSame($expected, $extracted, $description);
+ }
+
+ /**
+ *
+ */
+ public function extractHashtagsWithIndicesProvider() {
+ return $this->providerHelper('hashtags_with_indices');
+ }
+
+}
View
@@ -1,5 +1,20 @@
<?php
-require_once 'spyc/spyc.php';
-require_once '../src/Twitter/Autolink.php';
-require_once '../src/Twitter/Extractor.php';
+# Set up error reporting:
+if (!defined('E_DEPRECATED')) define('E_DEPRECATED', 8192);
+error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
+
+# Set default timezone to hide warnings:
+date_default_timezone_set('Europe/London');
+
+# Set encoding for manipulation of multi byte strings:
+mb_internal_encoding('UTF-8');
+
+# Set up path variables.
+$ROOT = dirname(dirname(__FILE__));
+$DATA = $ROOT.'/tests/data/twitter-text-conformance';
+
+# Include required classes.
+require_once $ROOT.'/lib/Twitter/Autolink.php';
+require_once $ROOT.'/lib/Twitter/Extractor.php';
+require_once $ROOT.'/tests/spyc/spyc.php';
View
@@ -10,18 +10,19 @@
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
*/
-if (!defined('E_DEPRECATED')) define('E_DEPRECATED', 8192);
-error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
-
-$ROOT = dirname(dirname(__FILE__));
-$DATA = $ROOT.'/tests/data/twitter-text-conformance';
-
-require_once $ROOT.'/lib/Twitter/Autolink.php';
-require_once $ROOT.'/lib/Twitter/Extractor.php';
-require_once $ROOT.'/tests/spyc/spyc.php';
+require_once dirname(__FILE__).'/bootstrap.php';
$browser = (PHP_SAPI != 'cli');
+function pretty_format($a) {
+ return preg_replace(array(
+ "/\n/", '/ +\[/', '/ +\)/', '/Array +\(/', '/(?<!\() \[/', '/\[([^]]+)\]/',
+ '/"(\d+)"/', '/(?<=^| )\((?= )/', '/(?<= )\)(?=$| )/',
+ ), array(
+ ' ', ' [', ' )', '(', ', [', '"$1"', '$1', '[', ']',
+ ), print_r($a, true));
+}
+
$pass_total = 0;
$fail_total = 0;
$pass_group = 0;
@@ -66,6 +67,7 @@
'urls' => 'extractURLs',
'mentions' => 'extractMentionedUsernames',
'replies' => 'extractRepliedUsernames',
+ 'hashtags_with_indices' => 'extractHashtagsWithIndices',
);
# Perform testing.
@@ -100,14 +102,14 @@
if ($browser) {
echo '<pre>';
echo 'Original: '.htmlspecialchars($test['text'], ENT_QUOTES, 'UTF-8', false), PHP_EOL;
- echo 'Expected: '.str_replace("\n", ' ', print_r($test['expected'], true)), PHP_EOL;
- echo 'Actual: '.str_replace("\n", ' ', print_r($extracted, true));
+ echo 'Expected: '.pretty_format($test['expected']), PHP_EOL;
+ echo 'Actual: '.pretty_format($extracted);
echo '</pre>';
} else {
echo PHP_EOL, PHP_EOL;
echo ' Original: '.$test['text'], PHP_EOL;
- echo ' Expected: '.str_replace("\n", ' ', print_r($test['expected'], true)), PHP_EOL;
- echo ' Actual: '.str_replace("\n", ' ', print_r($extracted, true)), PHP_EOL;
+ echo ' Expected: '.pretty_format($test['expected']), PHP_EOL;
+ echo ' Actual: '.pretty_format($extracted), PHP_EOL;
}
}
if ($browser) echo '</li>';
@@ -189,14 +191,14 @@
if ($browser) {
echo '<pre>';
echo 'Original: '.htmlspecialchars($test['text'], ENT_QUOTES, 'UTF-8', false), PHP_EOL;
- echo 'Expected: '.str_replace("\n", ' ', print_r($test['expected'], true)), PHP_EOL;
- echo 'Actual: '.str_replace("\n", ' ', print_r($linked, true));
+ echo 'Expected: '.pretty_format($test['expected']), PHP_EOL;
+ echo 'Actual: '.pretty_format($linked);
echo '</pre>';
} else {
echo PHP_EOL, PHP_EOL;
echo ' Original: '.$test['text'], PHP_EOL;
- echo ' Expected: '.str_replace("\n", ' ', print_r($test['expected'], true)), PHP_EOL;
- echo ' Actual: '.str_replace("\n", ' ', print_r($linked, true)), PHP_EOL;
+ echo ' Expected: '.pretty_format($test['expected']), PHP_EOL;
+ echo ' Actual: '.pretty_format($linked), PHP_EOL;
}
}
if ($browser) echo '</li>';

0 comments on commit 8e97083

Please sign in to comment.