Skip to content

Instantly share code, notes, and snippets.

@pyrou
Last active Apr 20, 2018
Embed
What would you like to do?
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