Skip to content

Instantly share code, notes, and snippets.

@andrewrcollins
Last active August 29, 2015 14:24
Show Gist options
  • Save andrewrcollins/0dc35ff12e90b316b1b1 to your computer and use it in GitHub Desktop.
Save andrewrcollins/0dc35ff12e90b316b1b1 to your computer and use it in GitHub Desktop.
<?php
/**
* PHPUnit Tests.
**/
/**
* @todo implement true autoloader
**/
include 'PersonName.php';
<?php
/**
* First Code Sample.
*
* Make a class which you would pass the components of someone's name
* (first name, last name) to the constructor, and have two functions
* callable off of the object:
*
* 1. Simply print out the whole name, i.e. "John Smith"
* 2. Print out the name in phone book fashion, i.e. "Smith, John"
*
* (Don't worry about validating empty strings.)
**/
/**
* PersonName represents a person's name.
*
* @author Andrew Collins <andrew@dripsandcastle.com>
**/
class PersonName
{
/**
* @var string Person's first name.
**/
private $firstName;
/**
* @var string Person's last name.
**/
private $lastName;
/**
* Create a new PersonName.
*
* @param string $firstName Person's first name.
* @param string $lastName Person's last name.
**/
public function __construct($firstName, $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
/**
* Print out person's full name.
*
* Print out person's full name, i.e. "John Smith".
**/
public function printFullName()
{
$fullName = $this->firstName.' '.$this->lastName;
echo $fullName;
}
/**
* Print out person's name in phone book fashion.
*
* Print out person's name in phone book fashion, i.e. "Smith, John".
**/
public function printPhoneBook()
{
$phoneBook = $this->lastName.', '.$this->firstName;
echo $phoneBook;
}
}
<?php
/**
* PHPUnit Tests.
**/
/**
* PersonNameTest.
**/
class PersonNameTest extends PHPUnit_Framework_TestCase
{
/**
* Test printFullName().
**/
public function testFullName()
{
$personName = new PersonName('Andrew', 'Collins');
$fullName = $personName->printFullName();
$this->assertEquals('Andrew Collins', $fullName);
}
/**
* Test printPhoneBook().
**/
public function testPhoneBook()
{
$personName = new PersonName('Andrew', 'Collins');
$phoneBook = $personName->printPhoneBook();
$this->assertEquals('Collins, Andrew', $phoneBook);
}
}
<phpunit bootstrap="bootstrap.php"></phpunit>
@andrewrcollins
Copy link
Author

Initial commit with class specification.

@andrewrcollins
Copy link
Author

Flesh out PersonName class.

@andrewrcollins
Copy link
Author

Add PHPUnit tests. Though, current PersonName class methods print strings rather than return strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment