Skip to content

Instantly share code, notes, and snippets.

@fprochazka
Created December 17, 2011 19:33
Show Gist options
  • Save fprochazka/1491152 to your computer and use it in GitHub Desktop.
Save fprochazka/1491152 to your computer and use it in GitHub Desktop.
Návrh na testování UI\Form komponent
<?php
/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008, 2011 Filip Procházka (filip.prochazka@kdyby.org)
*
* @license http://www.kdyby.org/license
*/
namespace Kdyby\Tests;
use Kdyby;
use Nette;
/**
* @author Filip Procházka <filip.prochazka@kdyby.org>
*/
class UIFormTest extends Kdyby\Tests\TestCase
{
public function testRun()
{
$model = $this->getMock('stdClass', array('save'));
$model->expects($this->once())
->method('save')
->with($this->equalTo(1));
$form = new TestingForm($model);
$presenter = new UIFormTestingPresenter($this->getContext(), $form);
$presenter->runPost(array('id' => 1));
}
}
class TestingForm extends Kdyby\Application\UI\Form
{
/** @var object */
private $model;
/**
* @param object $model
*/
public function __construct($model)
{
parent::__construct();
$this->model = $model;
}
/**
* Method gets called on construction
*/
protected function configure()
{
$this->addText('id', 'Id');
$this->addSubmit('save', 'Uložit');
}
/**
*/
public function handleSuccess()
{
$this->model->save($this['id']->value);
}
}
class UIFormTestingPresenter extends Nette\Application\UI\Presenter
{
/** @var \Nette\Application\UI\Form */
private $form;
/**
* @param \Kdyby\DI\Container $context
* @param \Nette\Application\UI\Form $form
*/
public function __construct($context, Nette\Application\UI\Form $form)
{
parent::__construct($context);
$this->form = $form;
}
public function runPost($post, $files = array())
{
// vytvořím "falešný" request
return $this->run(new Nette\Application\Request(
'presenter',
'POST',
array('do' => 'form-submit', 'action' => 'default'),
$post,
$files
));
}
// public function runGET()...
public function renderDefault()
{
// nechceme nic renderovat
// chceme jenom aby formulář přijal signál
$this->terminate();
}
protected function createComponentForm()
{
return $this->form;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment