Пример #1
0
    /**
     * @param $scheme
     * @return TaskResult
     */
    public function getContent(Table $scheme)
    {
        $selectOne = $scheme->createSelectOne();
        $deleteOne = $scheme->createDeleteOne();
        $className = StringUtil::camelize($scheme->getName()) . 'Dao';
        ob_start();
        echo "<?php\n";
        ?>
namespace <?php 
        echo $this->appName;
        ?>
\classes\dao;

class <?php 
        echo $className;
        ?>
 extends \Mahotora\BaseDao
{
    public function getTableName()
    {
        return '<?php 
        echo $scheme->getName();
        ?>
';
    }
<?php 
        if ($scheme->isAutoIncrement()) {
            ?>
    public function save($entity)
    {
        parent::save($entity);
        if (<?php 
            echo implode(' && ', array_map(function ($column) {
                return '!isset($entity->' . $column->getName() . ')';
            }, $scheme->getPrimaryKeys()));
            ?>
) {
            $id = $this->getDatabaseSession()->lastInsertId();
<?php 
            foreach ($scheme->getPrimaryKeys() as $column) {
                ?>
            $entity-><?php 
                echo $column->getName();
                ?>
 = $id;
<?php 
            }
            ?>
        }
    }
<?php 
        }
        ?>

    public function delete($id)
    {
        $this->getDatabaseSession()->executeNoResult(
            "<?php 
        echo $deleteOne['SQL'];
        ?>
",
            "<?php 
        echo $deleteOne['marker'];
        ?>
",
            is_array($id) ? $id : array ($id)
        );
    }

    public function find($id)
    {
        $result = $this->getDatabaseSession()->find(
            "<?php 
        echo $selectOne['SQL'];
        ?>
",
            "<?php 
        echo $selectOne['marker'];
        ?>
",
            is_array($id) ? $id : array ($id)
        );
        return $result ? $result[0] : null;
    }
}
<?php 
        return new TaskResult('app/classes/dao/' . $className . '.php', ob_get_clean());
    }
Пример #2
0
    /**
     * @param $scheme
     * @return TaskResult
     */
    public function getContent(Table $scheme)
    {
        $selectOne = $scheme->createSelectOne();
        ob_start();
        $className = StringUtil::camelize($scheme->getName()) . 'DaoTest';
        echo "<?php\n";
        ?>
namespace <?php 
        echo $this->appName;
        ?>
\classes\dao;

class <?php 
        echo $className;
        ?>
 extends \PHPUnit_Framework_TestCase
{
    private $target = null;
    private $databaseSession = null;

    public function setUp()
    {
        $this->databaseSession = \Phake::mock('Mahotora\DatabaseSessionImpl');
        $this->target = new \<?php 
        echo $this->appName;
        ?>
\classes\dao\<?php 
        echo StringUtil::camelize($scheme->getName());
        ?>
Dao($this->databaseSession);
    }

    public function testFind()
    {
        $entity = (object)<?php 
        echo $this->dumpHash($selectOne['dummyHash'], 8);
        ?>
;
        \Phake::when($this->databaseSession)->find(
            "<?php 
        echo $selectOne['SQL'];
        ?>
",
            "<?php 
        echo $selectOne['marker'];
        ?>
",
            <?php 
        echo $this->dumpArray($selectOne['dummyKeys'], 12);
        ?>
        )->thenReturn(array($entity));
        $result = $this->target->find(<?php 
        echo $this->dumpArray($selectOne['dummyKeys'], 8);
        ?>
);
        $this->assertSame($entity, $result);
    }

    public function testFindNoResult()
    {
        $entity = (object)<?php 
        echo $this->dumpHash($selectOne['dummyHash'], 8);
        ?>
;
        \Phake::when($this->databaseSession)->find(
            "<?php 
        echo $selectOne['SQL'];
        ?>
",
            "<?php 
        echo $selectOne['marker'];
        ?>
",
            <?php 
        echo $this->dumpArray($selectOne['dummyKeys'], 12);
        ?>
        )->thenReturn(array());
        $result = $this->target->find(<?php 
        echo $this->dumpArray($selectOne['dummyKeys'], 8);
        ?>
);
        $this->assertSame(null, $result);
    }
}
<?php 
        return new TaskResult('tests/classes/dao/' . $className . '.php', ob_get_clean());
    }
Пример #3
0
    /**
     * @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());
    }
Пример #4
0
    /**
     * @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());
    }