Skip to content

Instantly share code, notes, and snippets.

@thepsion5
Created September 28, 2018 21:20
Show Gist options
  • Save thepsion5/c3f90d9a898f811b85a715eb819dd869 to your computer and use it in GitHub Desktop.
Save thepsion5/c3f90d9a898f811b85a715eb819dd869 to your computer and use it in GitHub Desktop.
Example of a unit test for an implementation of a Twitter Profile PHP class taken from a project at work, using PHPUnit
<?php
namespace App\Tests\Domain\Twitter;
use App\Domain\Twitter\TwitterProfile;
class TwitterProfileTest extends \App\Tests\BaseUnitTest
{
private $accessToken = 'oauth-token';
private $accessSecret = 'oauth-secret';
/** @test */
public function is_constructed_with_a_username_and_description()
{
$expectedUsername = 'foo';
$expectedDesc = 'bar';
$account = new TwitterProfile($expectedUsername, $expectedDesc);
$this->assertEquals($expectedUsername, $account->getUsername());
$this->assertEquals($expectedDesc, $account->getDescription());
}
/** @test */
public function sets_and_retrieves_its_description()
{
$expectedDesc = 'new Description';
$account = new TwitterProfile('foo', 'desc');
$account->setDescription($expectedDesc);
$this->assertEquals($expectedDesc, $account->getDescription());
}
/** @test */
public function sets_and_retrieves_its_oauth_credentials()
{
$account = (new TwitterProfile('foo', 'bar'))
->setAccessCredentials($this->accessToken, $this->accessSecret);
$this->assertEquals($this->accessToken, $account->getAccessToken());
$this->assertEquals($this->accessSecret, $account->getAccessSecret());
}
/** @test */
public function determines_if_it_has_all_required_credentials()
{
$full = (new TwitterProfile('foo', 'bar'))
->setAccessCredentials($this->accessToken, $this->accessSecret);
$none = new TwitterProfile('foo', 'bar');
$this->assertTrue($full->hasCredentials());
$this->assertFalse($none->hasCredentials());
}
/** @test */
public function can_be_created_with_and_retrieve_its_allowed_categories()
{
$expectedCategories = ['Foo', 'Bar'];
$profile = new TwitterProfile('username', 'desc', '', '', $expectedCategories);
$actualCategories = $profile->getAllowedCategories();
$this->assertEquals($expectedCategories, $actualCategories);
}
/** @test */
public function checks_whether_it_has_an_allowed_category()
{
$profile = new TwitterProfile('username', 'desc', '', '', ['Foo', 'Bar']);
$this->assertTrue( $profile->hasAllowedCategory('Foo') );
$this->assertFalse( $profile->hasAllowedCategory('Baz') );
}
/** @test */
public function sets_its_allowed_categories()
{
$expectedCategories = ['Foo', 'Bar'];
$profile = new TwitterProfile('username', 'desc');
$profile->setAllowedCategories($expectedCategories);
$actualCategories = $profile->getAllowedCategories();
$this->assertEquals($expectedCategories, $actualCategories);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment