Skip to content
Permalink
Browse files

Final tests around twitter cron

  • Loading branch information...
jacobemerick committed Sep 8, 2017
1 parent 654bb82 commit 117246697f38ce99e7bfb5ea718d40b4194fd18b
Showing with 233 additions and 5 deletions.
  1. +233 −5 tests/unit/Cron/TwitterTest.php
@@ -527,27 +527,255 @@ public function testFetchTweetsReturnsTweets()
public function testProcessTweetChecksTweetExists()
{
$this->markTestIncomplete();
$tweet = (object) [
'id_str' => '123',
];
$mockTwitterModel = $this->createMock(TwitterModel::class);
$mockLogger = $this->createMock(Logger::class);
$mockLogger->expects($this->never())
->method('debug');
$twitter = $this->getMockBuilder(Twitter::class)
->disableOriginalConstructor()
->setMethods([
'checkTweetExists',
'checkTweetUpdated',
'insertTweet',
'updateTweet',
])
->getMock();
$twitter->expects($this->once())
->method('checkTweetExists')
->with(
$this->equalTo($mockTwitterModel),
$this->equalTo($tweet->id_str)
)
->willReturn(true);
$twitter->expects($this->never())
->method('insertTweet');
$reflectedTwitter = new ReflectionClass(Twitter::class);
$reflectedLoggerProperty = $reflectedTwitter->getProperty('logger');
$reflectedLoggerProperty->setAccessible(true);
$reflectedLoggerProperty->setValue($twitter, $mockLogger);
$reflectedProcessTweetMethod = $reflectedTwitter->getMethod('processTweet');
$reflectedProcessTweetMethod->setAccessible(true);
$reflectedProcessTweetMethod->invokeArgs($twitter, [
$mockTwitterModel,
$tweet,
]);
}
public function testProcessTweetInsertsIfTweetDoesNotExist()
{
$this->markTestIncomplete();
$tweet = (object) [
'id_str' => '123',
];
$mockTwitterModel = $this->createMock(TwitterModel::class);
$mockTimezone = $this->createMock(DateTimeZone::class);
$mockContainer = $this->createMock(Container::class);
$mockContainer->method('get')
->will($this->returnValueMap([
[ 'timezone', $mockTimezone ],
]));
$mockLogger = $this->createMock(Logger::class);
$mockLogger->expects($this->once())
->method('debug')
->with(
$this->equalTo('Inserted new tweet: 123')
);
$twitter = $this->getMockBuilder(Twitter::class)
->disableOriginalConstructor()
->setMethods([
'checkTweetExists',
'checkTweetUpdated',
'insertTweet',
'updateTweet',
])
->getMock();
$twitter->method('checkTweetExists')
->willReturn(false);
$twitter->expects($this->never())
->method('checkTweetUpdated');
$twitter->expects($this->once())
->method('insertTweet')
->with(
$this->equalTo($mockTwitterModel),
$this->equalTo($tweet),
$this->equalTo($mockTimezone)
);
$reflectedTwitter = new ReflectionClass(Twitter::class);
$reflectedContainerProperty = $reflectedTwitter->getProperty('container');
$reflectedContainerProperty->setAccessible(true);
$reflectedContainerProperty->setValue($twitter, $mockContainer);
$reflectedLoggerProperty = $reflectedTwitter->getProperty('logger');
$reflectedLoggerProperty->setAccessible(true);
$reflectedLoggerProperty->setValue($twitter, $mockLogger);
$reflectedProcessTweetMethod = $reflectedTwitter->getMethod('processTweet');
$reflectedProcessTweetMethod->setAccessible(true);
$result = $reflectedProcessTweetMethod->invokeArgs($twitter, [
$mockTwitterModel,
$tweet,
]);
$this->assertTrue($result);
}
public function testProcessTweetChecksTweetUpdated()
{
$this->markTestIncomplete();
$tweet = (object) [
'id_str' => '123',
];
$mockTwitterModel = $this->createMock(TwitterModel::class);
$mockLogger = $this->createMock(Logger::class);
$mockLogger->expects($this->never())
->method('debug');
$twitter = $this->getMockBuilder(Twitter::class)
->disableOriginalConstructor()
->setMethods([
'checkTweetExists',
'checkTweetUpdated',
'insertTweet',
'updateTweet',
])
->getMock();
$twitter->method('checkTweetExists')
->willReturn(true);
$twitter->expects($this->once())
->method('checkTweetUpdated')
->with(
$this->equalTo($mockTwitterModel),
$this->equalTo($tweet->id_str),
$this->equalTo($tweet)
)
->willReturn(false);
$twitter->expects($this->never())
->method('updateTweet');
$reflectedTwitter = new ReflectionClass(Twitter::class);
$reflectedLoggerProperty = $reflectedTwitter->getProperty('logger');
$reflectedLoggerProperty->setAccessible(true);
$reflectedLoggerProperty->setValue($twitter, $mockLogger);
$reflectedProcessTweetMethod = $reflectedTwitter->getMethod('processTweet');
$reflectedProcessTweetMethod->setAccessible(true);
$reflectedProcessTweetMethod->invokeArgs($twitter, [
$mockTwitterModel,
$tweet,
]);
}
public function testProcessTweetUpdatesIfTweetHasBeenUpdated()
{
$this->markTestIncomplete();
$tweet = (object) [
'id_str' => '123',
];
$mockTwitterModel = $this->createMock(TwitterModel::class);
$mockLogger = $this->createMock(Logger::class);
$mockLogger->expects($this->once())
->method('debug')
->with(
$this->equalTo('Updated tweet: 123')
);
$twitter = $this->getMockBuilder(Twitter::class)
->disableOriginalConstructor()
->setMethods([
'checkTweetExists',
'checkTweetUpdated',
'insertTweet',
'updateTweet',
])
->getMock();
$twitter->method('checkTweetExists')
->willReturn(true);
$twitter->method('checkTweetUpdated')
->willReturn(true);
$twitter->expects($this->once())
->method('updateTweet')
->with(
$this->equalTo($mockTwitterModel),
$this->equalTo($tweet->id_str),
$this->equalTo($tweet)
);
$reflectedTwitter = new ReflectionClass(Twitter::class);
$reflectedLoggerProperty = $reflectedTwitter->getProperty('logger');
$reflectedLoggerProperty->setAccessible(true);
$reflectedLoggerProperty->setValue($twitter, $mockLogger);
$reflectedProcessTweetMethod = $reflectedTwitter->getMethod('processTweet');
$reflectedProcessTweetMethod->setAccessible(true);
$result = $reflectedProcessTweetMethod->invokeArgs($twitter, [
$mockTwitterModel,
$tweet,
]);
$this->assertTrue($result);
}
public function testProcessTweetReturnsFalseIfNoChange()
{
$this->markTestIncomplete();
$tweet = (object) [
'id_str' => '123',
];
$mockTwitterModel = $this->createMock(TwitterModel::class);
$mockLogger = $this->createMock(Logger::class);
$twitter = $this->getMockBuilder(Twitter::class)
->disableOriginalConstructor()
->setMethods([
'checkTweetExists',
'checkTweetUpdated',
'insertTweet',
'updateTweet',
])
->getMock();
$twitter->method('checkTweetExists')
->willReturn(true);
$twitter->method('checkTweetUpdated')
->willReturn(false);
$reflectedTwitter = new ReflectionClass(Twitter::class);
$reflectedLoggerProperty = $reflectedTwitter->getProperty('logger');
$reflectedLoggerProperty->setAccessible(true);
$reflectedLoggerProperty->setValue($twitter, $mockLogger);
$reflectedProcessTweetMethod = $reflectedTwitter->getMethod('processTweet');
$reflectedProcessTweetMethod->setAccessible(true);
$result = $reflectedProcessTweetMethod->invokeArgs($twitter, [
$mockTwitterModel,
$tweet,
]);
$this->assertFalse($result);
}
public function testCheckTweetExistsPullsFromTwitterModel()

0 comments on commit 1172466

Please sign in to comment.
You can’t perform that action at this time.