Skip to content

Instantly share code, notes, and snippets.

@sousk
Created November 2, 2009 06:14
Show Gist options
  • Save sousk/223993 to your computer and use it in GitHub Desktop.
Save sousk/223993 to your computer and use it in GitHub Desktop.
a mock object lib for symfony/doctest
<?php
/**
* #test prepare for the test
* <code>
* class User {};
* function setup() {
* MockObject::setFixturePath(sfConfig::get('sf_test_dir') .'/fixtures/test_mock_object.yml', true);
* }
*
* </code>
*/
/**
* convenience method
*
* function fixture($name)
* {
* static $loaded;
* if (! $loaded) {
* MockObject::setFixturePath(sfConfig::get('sf_test_dir') .'/fixtures/mock_object.yml', true);
* $loaded = true;
* }
*
* return MockObject::fixture($name);
* }
*
* function propel_fixture($name)
* {
* return to_propel_object(fixture($name));
* }
*
* function to_propel_object($fixture)
* {
* $i = $fixture->toInstance();
* $i->fromArray($fixture->properties, BasePeer::TYPE_FIELDNAME);
* return $i;
* }
*
*/
/**
* #test basic functions
* <code>
* $o = new MockObject();
* #is($o->getHoge(), null, 'got null');
* #is($o->hoge(), null, 'got null');
* </code>
*
* #test mock accessors
* <code>
* $id = 20042; $name = 'john';
* $o = new MockObject(array('id'=> $id, 'name' => $name));
* #is($o->getId(), $id, "getter works");
* </code>
*
* #test mock methods
* <code>
* $o = new MockObject(array(), array('save' => true));
* #is($o->save(), true, "method works");
* </code>
*/
class MockObject {
public static $fixture_path = null;
private static $fixture_data = array();
private static $fixture_objects = array();
public $properties = array();
public $methods = array();
public function __construct($properties=array(), $methods=array()) {
$this->properties = $properties;
$this->methods = $methods;
}
public function __call($name, $params=array())
{
if (preg_match("/^get([A-Z].*)/", $name, $m)) {
$propname = self::underscorize($m[1]);
if (isset($this->properties[$propname])) {
return $this->properties[$propname];
}
}
if (array_key_exists($name, $this->methods)) {
return $this->methods[$name];
}
}
/**
* #test toInstance
* <code>
* setup();
* #ok($fx = MockObject::fixture('john'));
* #ok($obj = $fx->toInstance());
* #isa_ok($obj, 'User', "got instance");
* </code>
*/
public function toInstance()
{
if ($klass = $this->properties['_class']) {
return new $klass;
}
}
/**
* #test set/get fixture dir
* <code>
* #ok(MockObject::setFixturePath('hoge'), "set path");
* #is(MockObject::getFixturePath(), 'hoge', "get path");
* $msg = 'got exception correctly';
* try {
* MockObject::setFixturePath('hoge', true);
* #fail($msg);
* }
* catch (Exception $e) {
* #diag($msg .$e->getMessage());
* }
* </code>
*/
public static function setFixturePath($path, $strict=false)
{
if ($strict && !file_exists($path)) {
throw new Exception("file does not exists: ". $path);
}
self::$fixture_path = $path;
return $path;
}
public static function getFixturePath()
{
return self::$fixture_path;
}
/**
* #test loading yaml (has dependent on data & structure)
* <code>
* #ok($o = MockObject::fixture('john'), "got object");
* #ok($o = MockObject::fixture('john'), "got object");
* #is($o->getId(), 10, "got prop");
* #is($o->save(), true, "methods works");
* </code>
*/
public static function fixture($name, $reload=false)
{
if (! self::$fixture_data) {
self::$fixture_data = sfYaml::load(self::getFixturePath());
}
if ($reload || ! array_key_exists($name, self::$fixture_objects)) {
$params = self::dataToParams(self::$fixture_data[$name]);
self::$fixture_objects[$name] = new self($params[0], $params[1]);
}
return self::$fixture_objects[$name];
}
/**
* #test dataToParams
* <code>
* #ok($p = MockObject::dataToParams(array('id'=>1,'_method'=>array('save'=>true))));
* #is($p[0]['id'], 1);
* #is($p[1]['save'], true);
* </code>
*/
public static function dataToParams($data)
{
$key = '_method';
$methods = array();
if (array_key_exists($key, $data)) {
$methods = $data[$key];
unset($data[$key]);
}
$params = array(
$data,
$methods
);
return $params;
}
public static function underscorize($word)
{
static $_cached;
if(!isset($_cached[$word])){
$_cached[$word] = strtolower(preg_replace(
array('/[^A-Z^a-z^0-9^\/]+/','/([a-z\d])([A-Z])/','/([A-Z]+)([A-Z][a-z])/'),
array('_','\1_\2','\1_\2'), $word));
}
return $_cached[$word];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment