Author: Andres Gutierrez (andres@phalconphp.com)
Author: Serghei Iakovlev (serghei@phalconphp.com)
Inheritance: extends Phalcon\Mvc\Model
Exemplo n.º 1
0
 public function testModelBinding(IntegrationTester $I)
 {
     $dispatcher = new Dispatcher();
     $dispatcher->setModelBinding(true);
     $dispatcher->setDI(Di::getDefault());
     $I->assertInstanceOf('Phalcon\\Di', $dispatcher->getDI());
     $I->haveServiceInDi('dispatcher', $dispatcher);
     // Model to test with
     $model = People::findFirst();
     $dispatcher->setControllerName('test10');
     $dispatcher->setActionName('view');
     $dispatcher->setParams([$model->cedula]);
     $dispatcher->dispatch();
     $I->assertInstanceOf('Phalcon\\Test\\Models\\People', $dispatcher->getReturnedValue());
     $I->assertEquals($model->cedula, $dispatcher->getReturnedValue()->cedula);
     // Reset dispatcher
     $dispatcher = new Dispatcher();
     $dispatcher->setDI(Di::getDefault());
     $I->assertInstanceOf('Phalcon\\Di', $dispatcher->getDI());
     $I->haveServiceInDi('dispatcher', $dispatcher);
     $dispatcher->setControllerName('test9');
     $dispatcher->setActionName('view');
     $dispatcher->setParams([$model->cedula]);
     try {
         $dispatcher->dispatch();
         $I->assertTrue(false, 'Here must be the exception about passing non model to the controller action');
     } catch (\Exception $e) {
         // PHP 5.x
         $I->assertEquals('Argument 1 passed to Test9Controller::viewAction() must be an instance of Phalcon\\Mvc\\Model, string given', $e->getMessage());
     } catch (\TypeError $e) {
         // PHP 7.x
         $I->assertEquals('Argument 1 passed to Test9Controller::viewAction() must be an instance of Phalcon\\Mvc\\Model, string given', $e->getMessage());
     }
     $dispatcher->setModelBinding(true);
     $dispatcher->dispatch();
     $I->assertInstanceOf('Phalcon\\Test\\Models\\People', $dispatcher->getReturnedValue());
     $I->assertEquals($dispatcher->getReturnedValue()->cedula, $model->cedula);
 }