Skip to content

Instantly share code, notes, and snippets.

@pyrou
Last active April 20, 2018 07:55
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 pyrou/5e440aa6c95669fcfa265ac14b7c2625 to your computer and use it in GitHub Desktop.
Save pyrou/5e440aa6c95669fcfa265ac14b7c2625 to your computer and use it in GitHub Desktop.
PHP Candidate Test

Test-driven development exercise

Instructions

Complete implementation of the file Context.php class to ensure ContextTest.php test case will pass.

<?php
/**
* Context objects aims to be used as a small bucket of mixed data.
* This class provide methods to define and retrieve these data
* identified by a Key.
*/
class Context {
/* @var array internal storage of Context variables */
protected $_data = array();
/**
* Get a value from internal storage _data.
* If the stored value is a Callable, this method should
* execute the Callable before returning the computed value.
*
* @param string $name Name of the data to retrieve
* @return mixed Value retrieved, NULL if the key doesn't exists
* @see http://php.net/callable
*/
function get($name)
{
// [code here]
}
/**
* Set a value to the internal storage _data.
*
* @param string $name Name of the data we want to set
* @param mixed Variable we want to set
*/
function set($name, $value = null)
{
// [code here]
}
/**
* Test if a key is defined
* @discussion NULL values are considered has non-existing
* @return bool
*/
function has($name)
{
return isset($this->_data[$name]);
}
}
<?php
// include_once __DIR__."/Context.php";
class ContextTest extends PHPUnit_Framework_TestCase
{
/**
* @var Context
*/
var $obj;
public function setUp()
{
parent::setUp();
$this->obj = new Context();
}
public function tearDown()
{
parent::tearDown();
$this->obj = null;
}
public function test_basic() {
$this->assertFalse($this->obj->has("key"));
$this->obj->set('key', "value");
$this->assertTrue($this->obj->has("key"));
}
public function test_unset() {
$this->obj->set('key', "value");
$this->assertTrue($this->obj->has("key"));
$this->obj->set('key', null);
$this->assertFalse($this->obj->has("key"));
}
public function test_set() {
$this->obj->set('key1', "value");
$this->obj->set(array('key2' => "value"));
$this->assertTrue($this->obj->has("key1"));
$this->assertTrue($this->obj->has("key2"));
}
public function test_get() {
$this->obj->set('key', "value");
$this->assertNull($this->obj->get("key2"));
$this->assertNotNull($this->obj->get("key"));
$this->assertEquals("value", $this->obj->get("key"));
}
public function test_callback() {
$this->obj->set('key', function() { return "value"; });
$this->assertTrue($this->obj->has("key"));
$this->assertEquals("value", $this->obj->get("key"));
}
public function test_get_function_name() {
$this->obj->set('username', 'max');
$this->assertTrue($this->obj->has("username"));
$this->assertEquals("max", $this->obj->get("username"));
}
public function test_callback_called_once() {
$Mock = $this->getMockBuilder('StdClass')
->setMethods(array('__invoke'))
->getMock();
// ensure __invoke() is called exactly 1 time
$Mock->expects($this->once())
->method('__invoke')
->will($this->returnValue(1));
$this->obj->set('key', $Mock);
$this->assertEquals(1, $this->obj->get("key"));
$this->assertEquals(1, $this->obj->get("key"));
$this->obj->set('key', null);
$this->assertFalse($this->obj->has("key"));
$this->assertNull($this->obj->get("key"));
}
public function test_context() {
$this->obj->set('key1', function() { return "hello"; });
$this->obj->set('key4', function($ctx) { return strtoupper($ctx->get('key3')); });
$this->obj->set('key3', function($ctx) { return $ctx->get('key1').$ctx->get('key2'); });
$this->obj->set('key2', function() { return "world"; });
$this->assertEquals("HELLOWORLD", $this->obj->get('key4'));
}
public function test_bulk_context() {
$this->obj->set(array(
'key1' => function() { return "hello"; },
'key4' => function($ctx) { return strtoupper($ctx->get('key3')); },
'key3' => function($ctx) { return $ctx->get('key1') . $ctx->get('key2'); },
'key2' => function() { return "world"; }
));
$this->assertEquals("HELLOWORLD", $this->obj->get('key4'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment