Skip to content

Instantly share code, notes, and snippets.

@nonlocalize
Created September 28, 2015 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nonlocalize/3a572f7a6acc998cbebf to your computer and use it in GitHub Desktop.
Save nonlocalize/3a572f7a6acc998cbebf to your computer and use it in GitHub Desktop.
First foray into TDD with some very basic tests for a custom CMS/website I'm making for a client.
<?php
use App\guyj\Discogs\Discogs;
class DiscogsTest extends \TestCase
{
public function setUp()
{
$this->discogs = new Discogs('guy-j-dizzy-moments-diaspora');
}
public function testArtworkLinkChangeForLargerImage()
{
$originalUrl = 'https://i1.sndcdn.com/artworks-000075254465-cxcdfz-large.jpg';
$expectedUrl = 'https://i1.sndcdn.com/artworks-000075254465-cxcdfz-t500x500.jpg';
$newUrl = str_replace('large', 't500x500', $originalUrl);
$this->assertEquals($expectedUrl, $newUrl);
}
/**
* @expectedException App\Exceptions\SoundcloudPlaylistException
*/
public function testIfSoundcloudPlaylistExceptionThrown()
{
$nonExistingPlaylist = new Discogs('BlueJays2015');
$nonExistingPlaylist->connected();
}
}
<?php
use App\guyj\Gigs\Gigatools;
class GigatoolsTest extends \TestCase
{
public function setUp()
{
$this->gigatools = new Gigatools('LukeFair');
}
/**
* @expectedException App\Exceptions\GigatoolsException
*/
public function testThrowExceptionIfNoUserFound()
{
$gigatools = new Gigatools('LukeFairy');
$gigatools->connected();
}
public function testReturnsEmptyIfNoUpcomingGigsExist()
{
$results = $this->gigatools->listings();
$this->assertTrue(empty($results));
}
public function testGigatoolsDataConvertedFromJsonToArray()
{
$data = json_decode(file_get_contents($this->gigatools->url));
$this->assertTrue(is_array($data));
}
public function testIfValidJson()
{
$gigatools = new Gigatools('LukeFair');
json_decode(file_get_contents($gigatools->url));
$this->assertTrue(json_last_error() == JSON_ERROR_NONE);
}
}
<?php
use App\guyj\Twitter\Twitter;
class TwitterTest extends \TestCase
{
public function setUp()
{
$this->keys = [
'consumer_key' => 'xxxxxxxx',
'consumer_secret' => 'xxxxxxxx',
'access_token' => 'xxxxxxxx',
'access_secret' => 'xxxxxxxx'
];
$this->twitter = new Twitter($this->keys);
}
public function testIfTwitterConnectionWorks()
{
$this->assertTrue($this->twitter->connected());
}
/**
* @expectedException App\Exceptions\TwitterConnectionException
*/
public function testIfTwitterConnectionExceptionThrownWhenNotConnected()
{
$this->keys['consumer_key'] = 'v311O7vuG14ksvhdM0NoFKyxxs';
$twitter = new Twitter($this->keys);
$twitter->connected();
}
public function testGetUrlFromTweet()
{
$tweets = $this->twitter->grab();
$firstTweet = $tweets[0];
if (isset($firstTweet->entities->url[0]))
{
$url = $firstTweet->entities->urls[0]->url;
$this->assertTrue(strpos($url, 't.co') !== false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment