This repository has been archived by the owner on Apr 13, 2021. It is now read-only.
forked from mzsanford/twitter-text-php
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated testing - run phpunit from root of project.
- Loading branch information
Showing
5 changed files
with
294 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters