Skip to content

Instantly share code, notes, and snippets.

@michaelesmith
Created December 11, 2010 04:51
Show Gist options
  • Save michaelesmith/737154 to your computer and use it in GitHub Desktop.
Save michaelesmith/737154 to your computer and use it in GitHub Desktop.
class Something {
public function doSomthing(Lead $lead){
//code here
}
}
class MockDoctrineRecord {
protected $data = array();
public $events = array();
public function __construct($data = array()) {
$this->data = $data;
}
public function __call($name, $arguments) {
if(substr($name, 0, 3) == 'get'){
return array_key_exists($key = substr($name, 3), $this->data) ? $this->data[$key] : null;
}elseif(substr($name, 0, 3) == 'set'){
$this->data[substr($name, 3)] = $arguments[0];
}else{
$this->events[] = $name;
}
}
}
class Lead extends MockDoctrineRecord {
}
$lead = new Lead(array('Date1'=>'2010-12-05', 'Date2'=>'2010-12-19');
$something->doSomething($lead);
//Assert date3 was set on the lead object
$this->assertTrue('2010-12-15', $lead->getDate3());
//Assert lead was saved
$this->assertContains('save', $lead->events);
class BaseLead extends MockDoctrineRecord {
}
/**
* Lead
*
* This class has been auto-generated by the Doctrine ORM Framework
*
* @package symfony
* @subpackage model
* @author msmith
* @version SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
*/
class Lead extends BaseLead {
public function __toString(){
return sprintf('%s %s', $this->getFirstName(), $this->getLastName());
}
/**
* Returns an array of all open action items
*
* @return array ActionItem
*/
public function getOpenActionItems(){
$ret_arr = array();
foreach($this->getActionItems() as $action_item){
if(! $action_item->getCloseDate()){
$ret_arr[] = $action_item;
}
}
return $ret_arr;
}
/**
* Returns an array of all closed action items
*
* @return array ActionItem
*/
public function getClosedActionItems(){
$ret_arr = array();
foreach($this->getActionItems() as $action_item){
if($action_item->getCloseDate()){
$ret_arr[] = $action_item;
}
}
return $ret_arr;
}
}
require_once dirname(__FILE__).'/../../bootstrap/unit.php';
class BaseLead extends MockDoctrineRecord {
}
class ActionItem extends MockDoctrineRecord {
}
class unit_LeadTest extends tmaPHPUnitBaseTestCase {
public function testDefault(){
$lead = new Lead(array('ActionItems' => array(
new ActionItem(array('CloseDate' => $date1 = date('Y-m-d', strtotime('-1 month')))),
new ActionItem(),
new ActionItem(array('CloseDate' => $date2 = date('Y-m-d', strtotime('+1 month')))),
new ActionItem(),
)));
$open = $lead->getOpenActionItems();
$this->assertEquals(2, count($open));
$this->assertNull($open[0]->getCloseDate());
$this->assertNull($open[1]->getCloseDate());
$closed = $lead->getClosedActionItems();
$this->assertEquals(2, count($closed));
$this->assertEquals($date1, $closed[0]->getCloseDate());
$this->assertEquals($date2, $closed[1]->getCloseDate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment