/** * @param Table $scheme * @return TaskResult */ public function getContent(Table $scheme) { ob_start(); $targetName = StringUtil::camelize($scheme->getName()); $className = $targetName . 'Test'; $selectOne = $scheme->createSelectOne(); $primaryKeys = $scheme->getPrimaryKeys(); print "<?php\n"; ?> namespace <?php echo $this->appName; ?> \actions; use \Hoimi\Response\Json; use \Mahotora\DatabaseSessionImpl; class <?php echo $className; ?> extends \PHPUnit_Framework_TestCase { private $target = null; private $dao = null; public function setUp () { $this->target = new <?php echo $targetName; ?> (); $this->dao = \Phake::mock('\<?php echo $this->appName; ?> \classes\dao\<?php echo $targetName; ?> '); $this->target->setDao($this->dao); } <?php if (count($primaryKeys) === 1) { ?> public function testGetWithId () { $entity = (object)<?php echo $this->dumpHash($selectOne['dummyHash'], 8); ?> ; $this->target->setRequest(new \Hoimi\Request( array(), array('id' => '<?php echo $primaryKeys[0]->dummyValue; ?> '), array() )); \Phake::when($this->dao)->find('<?php echo $primaryKeys[0]->dummyValue; ?> ')->thenReturn($entity); $result = $this->target->get(); $this->assertSame('<?php echo json_encode((object) $selectOne['dummyHash']); ?> ', $result->getContent()); } /** * @expectedException \Hoimi\Exception\ForbiddenException */ public function testGetNoId () { $result = $this->target->get(); } /** * @expectedException \Hoimi\Exception\NotFoundException */ public function testGetNotFound() { $this->target->setRequest(new \Hoimi\Request( array(), array('id' => '<?php echo $primaryKeys[0]->dummyValue; ?> '), array() )); $result = $this->target->get(); } public function testPostWithId () { $entity = (object)<?php echo $this->dumpHash($selectOne['dummyHash'], 8); ?> ; $this->target->setRequest(new \Hoimi\Request( array(), <?php echo $this->dumpHash($selectOne['dummyHash'], 8); ?> , array() )); \Phake::when($this->dao)->find(1)->thenReturn($entity); $result = $this->post(); $this->assertInstanceOf('\Hoimi\Response\Json', $result); \Phake::verify($this->dao)->save((object)<?php echo $this->dumpHash($selectOne['dummyHash'], 8); ?> ); } <?php foreach ($scheme->getColumns() as $column) { if ($column->getNotNull()) { ?> public function testPostNoId () { $entity = (object)<?php echo $this->dumpHash($selectOne['dummyHash'], 8); ?> ; $this->target->setRequest(new \Hoimi\Request( array(), <?php echo $this->dumpHash($selectOne['dummyHash'], 8); ?> , array() )); \Phake::when($this->dao)->find(1)->thenReturn($entity); $result = $this->post(); $this->assertInstanceOf('\Hoimi\Response\Json', $result); \Phake::verify($this->dao)->save((object)<?php echo $this->dumpHash($selectOne['dummyHash'], 8); ?> ); } <?php } } } else { ?> // TODO: too many primary keys. Generator can't generate this action from Table. <?php } ?> } <?php return new TaskResult('tests/actions/' . $className . '.php', ob_get_clean()); }
/** * @param Table $scheme * @return TaskResult */ public function getContent(Table $scheme) { $className = StringUtil::camelize($scheme->getName()); ob_start(); echo "<?php\n"; ?> namespace <?php echo $this->appName; ?> \actions; use \Hoimi\Response\Json; use \Mahotora\DatabaseSessionImpl; use \Mahotora\DatabaseSessionFactory; class <?php echo $className; ?> extends \Hoimi\BaseAction { <?php if (count($scheme->getPrimaryKeys()) === 1) { ?> private $dao = null; public function get() { $id = $this->getRequest()->get('id'); $response = null; if ($id) { $data = $this->getDao()->find($id); if ($data) { $response = new \Hoimi\Response\Json($data); } else { throw new \Hoimi\Exception\NotFoundException(); } } else { throw new \Hoimi\Exception\ForbiddenException(); } return $response; } public function post() { $request = $this->getRequest(); $validationResult = \Hoimi\Validator::validate($request, array( <?php foreach ($scheme->getColumns() as $column) { ?> '<?php echo $column->getName(); ?> ' => <?php echo preg_replace('#\\s*,\\s*\\)#', ')', preg_replace('#\\(\\s*#', '(', str_replace("\n", "", var_export($column->validatorDefinition(), true)))); ?> , <?php } ?> )); if ($validationResult) { throw new \Hoimi\Exception\ValidationException($validationResult); } $id = $request->get('id'); $response = null; if ($id) { $data = $this->getDao()->find($id); } if (!$data) { $data = new \stdClass(); } <?php foreach ($scheme->getColumns() as $column) { ?> $data-><?php echo $column->getName(); ?> = $request->get('<?php echo $column->getName(); ?> '); <?php } ?> $this->getDao()->save($data); $response = new \Hoimi\Response\Json($data); return $response; } <?php } else { ?> // too many primary keys. we can't generate action. <?php } ?> public function setDao($dao) { $this->dao = $dao; } public function getDao() { if ($this->dao === null) { $this->dao = new \<?php echo $this->appName; ?> \classes\dao\<?php echo StringUtil::camelize($scheme->getName()); ?> Dao( DatabaseSessionFactory::build($this->getConfig()->get('database')) ); } return $this->dao; } } <?php return new TaskResult('app/actions/' . $className . '.php', ob_get_clean()); }