PHP
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
data
lib
LICENSE
README

README

## Form tests

sfPHPUnitFormTestCase

    // AutoTests fixtures
    abstract protected function makeForm();
    abstract protected function getFields();
    abstract protected function getValidationTestingPlan();
    abstract protected function getValidData();

    // AutoTests
    public function testAutoFields()
    public function testAutoRequirements()
    public function testAutoValidation()
    public function testAutoFormIsValid()

    // Utils & Testers
    protected function getValidInput()
    protected function makeInput(array $input)
    protected function cleanInput(array $input)
    protected function assertFormFields($expected, sfForm $form, $message = null)
    protected function assertFormIsValid(sfForm $form, $message = null)
    protected function assertFormHasErrors(sfForm $form, $errorsCount, $message = null)
    protected function assertFormError(sfForm $form, $field, $expectedError, $message = null)
    protected function checkFormWithPlan(sfForm $form, array $plan, array $input, $errorCode, $message)
    protected function makeErrorMess(sfForm $form, $message)
    protected function isEmbeddedForm($fieldName)


Example:

    class ArticleFormTest extends sfPHPUnitFormTestCase
    {
        /**
         * Skip auto test which saves form
         * Default: true
         */
        protected $saveForm = false;


        /**
         * Make form
         */
        protected function makeForm()
        {
            return new ArticleForm();
        }


        /**
         * Return array with all expected form fields with valid valies
         */
        protected function getValidData()
        {
            return array(
                'title'   => 'Article title',
                'slug'    => 'slug-code',
                'text'    => 'Article text',
                'author' => array(
                    'name' => 'Author Name',
                ),
            );
        }


        /**
         * Describe all expected form fields and their requirements
         * PostValidator is disabled
         */
        protected function getFields()
        {
            return array(
                'title' => array(
                    'min_length'  => 5,
                    'max_length'  => 100,
                    'trim'        => true,
                    'required'    => true,
                ),
                'slug' => array(
                    'min_length'  => array('1234' => false, '12345' => true), // Custom testing plan
                    'max_length'  => 100,
                    'trim'        => true,
                    'required'    => true,
                    'invalid'     => array(
                        'invalid slug',
                    ),
                    'success'     => array(
                        'validslug',
                        'validslug1',
                        'Valid-Slug1',
                    ),
                    'transform' => array('SLUG' => 'slug'),
                ),
                'text' => array(
                    'trim'        => true,
                    'required'    => true,
                ),
                'author' => array(
                    'embed' => 'AuthorForm',
                ),
                '_csrf_token' => array(
                    'required'    => true,
                ),
            );
        }


        /**
         * Custom testing scenarios
         */
        protected function getValidationTestingPlan()
        {
            $validInput = $this->getValidInput();
            $someArticle = $this->makeArticle();

            return array(
                // Уникальность email
                'Slug should be unique' => new sfPHPUnitFormValidationItem(
                    array_merge($validInput, array('slug' => $someArticle->getSlug())),
                    array(
                        'email_address' => 'invalid',
                    )),
            );
        }


        // Do own tests here

        /**
         * Example
         */
        public function testSaveForm()
        {
            $form = $this->makeForm();

            $input = $this->getValidInput();
            $input['title'] = 'Some title'

            $form->bind($input);
            $this->assertFormIsValid($form);

            $ob = $form->save();
            $this->assertEquals(1, $this->queryFind(get_class($ob), $this->cleanInput($data))->count());
        }
    }