Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added test suite for TwitterSyndicator, fixed some bugs

  • Loading branch information...
commit 521e93b15882984b6aede822a4d8610fbcce9cf1 1 parent 7ab61ac
@barnabywalters authored
View
31 BarnabyWalters/Posse/EventListener/TwitterSyndicator.php
@@ -146,33 +146,33 @@ public function setGuzzle(\Guzzle\Http\ClientInterface $client) {
* the duplicates, append the tweet URL and set them again.
*
* @param \ActivityStreams\Event\ActivityEvent $event
- * @return boolean
+ * @return string|null The strings are just for test debugging purposes
*/
public function syndicateToTwitter(Event $event) {
if (!$event instanceof ActivityEvent)
- return false;
+ return 'Not an ActivityEvent';
/* @var $object ObjectInterface */
$object = $event->getObject();
- if (method_exists($object, 'getTags'))
- return false;
+ if (!method_exists($object, 'getTags'))
+ return 'Object has no getTags() method';
$tags = $object->getTags();
if (!is_array($tags))
- return false;
+ return 'Tags are not an array';
$syndicating = false;
if (($this->strategy == self::STRATEGY_SYNDICATE_IF_TAGGED
- and in_array($this->tag, $tags))
+ and in_array($this->tag, $tags))
or ($this->strategy == self::STRATEGY_SYNDICATE_UNLESS_TAGGED)
- and !in_array($this->tag, $tags))
+ and !in_array($this->tag, $tags))
$syndicating = true;
if (!$syndicating)
- return false;
+ return 'Object is not a syndication candidate';
// We’re syndicating!
$content = $object->getContent() ?: $object->getSummary();
@@ -180,6 +180,8 @@ public function syndicateToTwitter(Event $event) {
if (method_exists($object, 'getInReplyTo'))
$inReplyTo = $object->getInReplyTo();
+ else
+ $inReplyTo = null;
$twitterApiQuery = Helpers::prepareForTwitter($content, $url, $inReplyTo);
@@ -212,18 +214,21 @@ public function syndicateToTwitter(Event $event) {
$tweetUrl = 'https://twitter.com/' . $tweetUserHandle . '/statuses/' . $tweetId;
if (method_exists($object, 'addDownstreamDuplicate')) {
- $object->addDownstreamCopy($tweetUrl);
- return;
+ $object->addDownstreamDuplicate($tweetUrl);
+ return true;
}
// Otherwise, manually add it if permitted
if (method_exists($object, 'getDownstreamDuplicates')
and method_exists($object, 'setDownstreamDuplicates')) {
- $copies = $object->getDownstreamCopies();
+ $copies = $object->getDownstreamDuplicates();
array_push($copies, $tweetUrl);
- $object->setDownstreamCopies($copies);
- return;
+ var_dump($copies);
+ $object->setDownstreamDuplicates($copies);
+ return true;
}
+
+ return true;
}
}
View
2  composer.json
@@ -12,7 +12,7 @@
},
"require-dev": {
"doctrine/orm": "*",
- "activitystreams/activitystreams": "0.1.*",
+ "activitystreams/activitystreams": "*",
"symfony/event-dispatcher": "2.1.*",
"guzzle/guzzle": "*"
},
View
432 composer.lock
@@ -25,437 +25,7 @@
}
}
],
- "packages-dev": [
- {
- "name": "activitystreams/activitystreams",
- "version": "dev-master",
- "source": {
- "type": "git",
- "url": "https://github.com/barnabywalters/php-activitystreams",
- "reference": "v0.1.5"
- },
- "dist": {
- "type": "zip",
- "url": "https://github.com/barnabywalters/php-activitystreams/archive/v0.1.5.zip",
- "reference": "v0.1.5",
- "shasum": ""
- },
- "time": "1352844989",
- "type": "library",
- "installation-source": "source",
- "autoload": {
- "psr-0": {
- "ActivityStreams\\ActivityStreams": "ActivityStreams\\ActivityStreams",
- "ActivityStreams\\Event": "ActivityStreams\\Event"
- }
- },
- "description": "A set of PSR-0 autoloadable classes for representing ActivityStreams activities and objects in PHP",
- "keywords": [
- "web",
- "social",
- "indieweb",
- "activitystreams",
- "activity",
- "federated"
- ]
- },
- {
- "name": "doctrine/common",
- "version": "dev-master",
- "source": {
- "type": "git",
- "url": "https://github.com/doctrine/common",
- "reference": "30b8cea3bfe22f5eb58b5b0165f21df750aeb2e2"
- },
- "dist": {
- "type": "zip",
- "url": "https://github.com/doctrine/common/archive/30b8cea3bfe22f5eb58b5b0165f21df750aeb2e2.zip",
- "reference": "30b8cea3bfe22f5eb58b5b0165f21df750aeb2e2",
- "shasum": ""
- },
- "require": {
- "php": ">=5.3.2"
- },
- "time": "1352815365",
- "type": "library",
- "extra": {
- "branch-alias": {
- "dev-master": "2.4.x-dev"
- }
- },
- "installation-source": "source",
- "autoload": {
- "psr-0": {
- "Doctrine\\Common\\": "lib/"
- }
- },
- "license": [
- "MIT"
- ],
- "authors": [
- {
- "name": "Jonathan Wage",
- "email": "jonwage@gmail.com",
- "homepage": "http://www.jwage.com/"
- },
- {
- "name": "Guilherme Blanco",
- "email": "guilhermeblanco@gmail.com",
- "homepage": "http://www.instaclick.com"
- },
- {
- "name": "Roman Borschel",
- "email": "roman@code-factory.org"
- },
- {
- "name": "Benjamin Eberlei",
- "email": "kontakt@beberlei.de"
- },
- {
- "name": "Johannes Schmitt",
- "email": "schmittjoh@gmail.com",
- "homepage": "http://jmsyst.com",
- "role": "Developer of wrapped JMSSerializerBundle"
- }
- ],
- "description": "Common Library for Doctrine projects",
- "homepage": "http://www.doctrine-project.org",
- "keywords": [
- "collections",
- "spl",
- "eventmanager",
- "annotations",
- "persistence"
- ]
- },
- {
- "name": "doctrine/dbal",
- "version": "dev-master",
- "source": {
- "type": "git",
- "url": "https://github.com/doctrine/dbal",
- "reference": "3af9edb2c603275dc3dbbb8a5a1797602f941ab6"
- },
- "dist": {
- "type": "zip",
- "url": "https://github.com/doctrine/dbal/archive/3af9edb2c603275dc3dbbb8a5a1797602f941ab6.zip",
- "reference": "3af9edb2c603275dc3dbbb8a5a1797602f941ab6",
- "shasum": ""
- },
- "require": {
- "php": ">=5.3.2",
- "doctrine/common": ">=2.3-dev,<2.5-dev"
- },
- "time": "1352833472",
- "type": "library",
- "extra": {
- "branch-alias": {
- "dev-master": "2.4.x-dev"
- }
- },
- "installation-source": "source",
- "autoload": {
- "psr-0": {
- "Doctrine\\DBAL\\": "lib/"
- }
- },
- "license": [
- "MIT"
- ],
- "authors": [
- {
- "name": "Jonathan Wage",
- "email": "jonwage@gmail.com",
- "homepage": "http://www.jwage.com/"
- },
- {
- "name": "Guilherme Blanco",
- "email": "guilhermeblanco@gmail.com",
- "homepage": "http://www.instaclick.com"
- },
- {
- "name": "Roman Borschel",
- "email": "roman@code-factory.org"
- },
- {
- "name": "Benjamin Eberlei",
- "email": "kontakt@beberlei.de"
- }
- ],
- "description": "Database Abstraction Layer",
- "homepage": "http://www.doctrine-project.org",
- "keywords": [
- "database",
- "persistence",
- "dbal",
- "queryobject"
- ]
- },
- {
- "name": "doctrine/orm",
- "version": "dev-master",
- "source": {
- "type": "git",
- "url": "git://github.com/doctrine/doctrine2.git",
- "reference": "0d58e6627af4e9864c5d50ef8019a2022a80ee01"
- },
- "dist": {
- "type": "zip",
- "url": "https://github.com/doctrine/doctrine2/archive/0d58e6627af4e9864c5d50ef8019a2022a80ee01.zip",
- "reference": "0d58e6627af4e9864c5d50ef8019a2022a80ee01",
- "shasum": ""
- },
- "require": {
- "php": ">=5.3.2",
- "ext-pdo": "*",
- "symfony/console": "2.*",
- "doctrine/dbal": ">=2.3-dev,<2.5-dev"
- },
- "suggest": {
- "symfony/yaml": "If you want to use YAML Metadata Mapping Driver"
- },
- "time": "1352731699",
- "bin": [
- "bin/doctrine",
- "bin/doctrine.php"
- ],
- "type": "library",
- "extra": {
- "branch-alias": {
- "dev-master": "2.4.x-dev"
- }
- },
- "installation-source": "source",
- "autoload": {
- "psr-0": {
- "Doctrine\\ORM\\": "lib/"
- }
- },
- "license": [
- "MIT"
- ],
- "authors": [
- {
- "name": "Jonathan Wage",
- "email": "jonwage@gmail.com",
- "homepage": "http://www.jwage.com/"
- },
- {
- "name": "Guilherme Blanco",
- "email": "guilhermeblanco@gmail.com",
- "homepage": "http://www.instaclick.com"
- },
- {
- "name": "Roman Borschel",
- "email": "roman@code-factory.org"
- },
- {
- "name": "Benjamin Eberlei",
- "email": "kontakt@beberlei.de"
- }
- ],
- "description": "Object-Relational-Mapper for PHP",
- "homepage": "http://www.doctrine-project.org",
- "keywords": [
- "database",
- "orm"
- ]
- },
- {
- "name": "guzzle/guzzle",
- "version": "dev-master",
- "source": {
- "type": "git",
- "url": "https://github.com/guzzle/guzzle",
- "reference": "6361c1db471b95dad97a3401560474880cc26ed0"
- },
- "dist": {
- "type": "zip",
- "url": "https://github.com/guzzle/guzzle/archive/6361c1db471b95dad97a3401560474880cc26ed0.zip",
- "reference": "6361c1db471b95dad97a3401560474880cc26ed0",
- "shasum": ""
- },
- "require": {
- "php": ">=5.3.2",
- "ext-curl": "*",
- "symfony/event-dispatcher": "2.1.*"
- },
- "replace": {
- "guzzle/common": "self.version",
- "guzzle/http": "self.version",
- "guzzle/parser": "self.version",
- "guzzle/batch": "self.version",
- "guzzle/cache": "self.version",
- "guzzle/inflection": "self.version",
- "guzzle/iterator": "self.version",
- "guzzle/log": "self.version",
- "guzzle/plugin": "self.version",
- "guzzle/plugin-async": "self.version",
- "guzzle/plugin-backoff": "self.version",
- "guzzle/plugin-cache": "self.version",
- "guzzle/plugin-cookie": "self.version",
- "guzzle/plugin-curlauth": "self.version",
- "guzzle/plugin-history": "self.version",
- "guzzle/plugin-log": "self.version",
- "guzzle/plugin-mock": "self.version",
- "guzzle/plugin-oauth": "self.version",
- "guzzle/service": "self.version",
- "guzzle/stream": "self.version",
- "guzzle/plugin-md5": "self.version"
- },
- "require-dev": {
- "doctrine/common": "*",
- "symfony/class-loader": "*",
- "monolog/monolog": "1.*",
- "zend/zend-log1": "1.12",
- "zend/zend-cache1": "1.12",
- "zendframework/zend-cache": "2.0.*",
- "zendframework/zend-log": "2.0.*",
- "phpunit/phpunit": "3.7.*"
- },
- "suggest": {
- "symfony/class-loader": "*",
- "monolog/monolog": "*",
- "zend/zend-cache": "2.0.0beta3",
- "zend/zend-log": "2.0.0beta3"
- },
- "time": "1352920225",
- "type": "library",
- "installation-source": "source",
- "autoload": {
- "psr-0": {
- "Guzzle\\Tests": "tests/",
- "Guzzle": "src/"
- }
- },
- "license": [
- "MIT"
- ],
- "authors": [
- {
- "name": "Michael Dowling",
- "email": "mtdowling@gmail.com",
- "homepage": "https://github.com/mtdowling"
- },
- {
- "name": "Guzzle Community",
- "homepage": "https://github.com/guzzle/guzzle/contributors"
- }
- ],
- "description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients",
- "homepage": "http://guzzlephp.org/",
- "keywords": [
- "framework",
- "curl",
- "http",
- "rest",
- "http client",
- "client",
- "web service"
- ]
- },
- {
- "name": "symfony/console",
- "version": "dev-master",
- "target-dir": "Symfony/Component/Console",
- "source": {
- "type": "git",
- "url": "https://github.com/symfony/Console",
- "reference": "6da537054e8f9f795e64a5b02eb7f9646c9b59e7"
- },
- "dist": {
- "type": "zip",
- "url": "https://github.com/symfony/Console/archive/6da537054e8f9f795e64a5b02eb7f9646c9b59e7.zip",
- "reference": "6da537054e8f9f795e64a5b02eb7f9646c9b59e7",
- "shasum": ""
- },
- "require": {
- "php": ">=5.3.3"
- },
- "time": "1352815684",
- "type": "library",
- "extra": {
- "branch-alias": {
- "dev-master": "2.2-dev"
- }
- },
- "installation-source": "source",
- "autoload": {
- "psr-0": {
- "Symfony\\Component\\Console\\": ""
- }
- },
- "license": [
- "MIT"
- ],
- "authors": [
- {
- "name": "Fabien Potencier",
- "email": "fabien@symfony.com"
- },
- {
- "name": "Symfony Community",
- "homepage": "http://symfony.com/contributors"
- }
- ],
- "description": "Symfony Console Component",
- "homepage": "http://symfony.com"
- },
- {
- "name": "symfony/event-dispatcher",
- "version": "2.1.x-dev",
- "target-dir": "Symfony/Component/EventDispatcher",
- "source": {
- "type": "git",
- "url": "https://github.com/symfony/EventDispatcher",
- "reference": "b5b6807bcc192f3a1c4fba7ef48466ebbd3c4485"
- },
- "dist": {
- "type": "zip",
- "url": "https://github.com/symfony/EventDispatcher/archive/b5b6807bcc192f3a1c4fba7ef48466ebbd3c4485.zip",
- "reference": "b5b6807bcc192f3a1c4fba7ef48466ebbd3c4485",
- "shasum": ""
- },
- "require": {
- "php": ">=5.3.3"
- },
- "require-dev": {
- "symfony/dependency-injection": "2.1.*"
- },
- "suggest": {
- "symfony/dependency-injection": "2.1.*",
- "symfony/http-kernel": "2.1.*"
- },
- "time": "1352368308",
- "type": "library",
- "extra": {
- "branch-alias": {
- "dev-master": "2.1-dev"
- }
- },
- "installation-source": "source",
- "autoload": {
- "psr-0": {
- "Symfony\\Component\\EventDispatcher": ""
- }
- },
- "license": [
- "MIT"
- ],
- "authors": [
- {
- "name": "Fabien Potencier",
- "email": "fabien@symfony.com"
- },
- {
- "name": "Symfony Community",
- "homepage": "http://symfony.com/contributors"
- }
- ],
- "description": "Symfony EventDispatcher Component",
- "homepage": "http://symfony.com"
- }
- ],
+ "packages-dev": null,
"aliases": [
],
View
95 tests/BarnabyWalters/Posse/EventListener/TwitterSyndicatorTest.php
@@ -18,16 +18,103 @@
class TwitterSyndicatorTest extends \PHPUnit_Framework_TestCase {
public function testReturnsFalseIfNotActivityEvent() {
$t = new TwitterSyndicator();
- $this->assertFalse($t->syndicateToTwitter(new Event()));
+ $this->assertEquals('Not an ActivityEvent', $t->syndicateToTwitter(new Event()));
}
- public function testsReturnsFalseIfObjectHasNoTags() {
+ public function testsReturnsErrorIfObjectHasNoTags() {
$t = new TwitterSyndicator();
$object = new Object();
- $object->setTags(array());
+ $object->tags = 'non-array';
$event = new ActivityEvent('post', $object);
- $this->assertFalse($t->syndicateToTwitter($event));
+ $this->assertEquals('Tags are not an array', $t->syndicateToTwitter($event));
+ }
+
+ public function testDoesntSyndicateForIfStrategyAndNotTagged() {
+ $t = new TwitterSyndicator();
+ $object = new Object();
+ $object->setTags(array('random tag', 'another tag'));
+ $event = new ActivityEvent('post', $object);
+
+ $this->assertEquals('Object is not a syndication candidate', $t->syndicateToTwitter($event));
+ }
+
+ public function testDoesntSyndicateForUnlessStrategyAndTagged() {
+ $t = new TwitterSyndicator(array('strategy' => 'syndicate unless tagged'));
+ $object = new Object();
+ $object->setTags(array('tweet'));
+ $event = new ActivityEvent('post', $object);
+
+ $this->assertEquals('Object is not a syndication candidate', $t->syndicateToTwitter($event));
+ }
+
+ /**
+ * @dataProvider configProvider
+ */
+ public function testAcceptsTagStrategyConfig($config, $tags, $expected) {
+ $t = new TwitterSyndicator($config);
+
+ $mockClient = new Client();
+ $mock = new MockPlugin();
+ $mock->addResponse(new Response(
+ '201',
+ array('Content-type: application/json'),
+ '{"id_str":"12345678","user":{"screen_name":"username"}}'
+ ));
+
+ $mockClient->addSubscriber($mock);
+ $t->setGuzzle($mockClient);
+
+ $object = new Object();
+ $object->setTags($tags);
+ $event = new ActivityEvent('post', $object);
+
+ $this->assertSame($expected, $t->syndicateToTwitter($event));
+ }
+
+ public function configProvider() {
+ return array(
+ array(
+ array('strategy' => 'syndicate if tagged', 'tag' => 'tweetme'),
+ 'tags' => array('tweetme'),
+ true
+ ),
+ array(
+ array('strategy' => 'syndicate unless tagged', 'tag' => 'notweet'),
+ 'tags' => array('blah'),
+ true
+ ),
+ array(
+ array('strategy' => 'syndicate unless tagged', 'tag' => 'notweet'),
+ 'tags' => array('notweet'),
+ 'Object is not a syndication candidate'
+ )
+ );
+ }
+
+ public function testDownstreamDuplicateAddedAfterSyndication() {
+ $t = new TwitterSyndicator();
+
+ $mockClient = new Client();
+ $mock = new MockPlugin();
+ $mock->addResponse(new Response(
+ '201',
+ array('Content-type: application/json'),
+ '{"id_str":"12345678","user":{"screen_name":"username"}}'
+ ));
+
+ $mockClient->addSubscriber($mock);
+ $t->setGuzzle($mockClient);
+
+ $object = new Object();
+ $object->setTags(array('tweet'));
+ $event = new ActivityEvent('post', $object);
+
+ $t->syndicateToTwitter($event);
+
+ $tweetUrl = 'https://twitter.com/username/statuses/12345678';
+
+ $this->assertContains($tweetUrl, $object->getDownstreamDuplicates());
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.