Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 37 additions & 35 deletions tests/ConstraintsTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
use Gregwar\Formidable\Form;
use Gregwar\Formidable\Factory;
use Gregwar\Formidable\PostIndicator;
use Gregwar\Formidable\Fields\FileField;
use PHPUnit\Framework\TestCase;

/**
* Special type "file" returning hash of the file instead of actually saving it
* Special type "file" returning hash of the file instead of actually saving it
*/
class FileField_NoSave extends \Gregwar\Formidable\Fields\FileField
class FileField_NoSave extends FileField
{
public function save($filename)
{
Expand All @@ -20,7 +22,7 @@ public function save($filename)
*
* @author Grégoire Passault <g.passault@gmail.com>
*/
class ConstraintsTests extends \PHPUnit\Framework\TestCase
class ConstraintsTests extends TestCase
{
/**
* Testing rendering a required field
Expand Down Expand Up @@ -68,7 +70,7 @@ public function testOptional()
$this->assertAccept($form, array(
'name' => ''
));

$this->assertEquals('', $form->name);

$this->assertAccept($form, array(
Expand All @@ -84,7 +86,7 @@ public function testOptional()
public function testMaxLength()
{
$form = $this->getForm('maxlength.html');

$this->assertContains('maxlength', "$form");

$this->assertAccept($form, array(
Expand All @@ -102,7 +104,7 @@ public function testMaxLength()
public function testMinLength()
{
$form = $this->getForm('minlength.html');

$this->assertContains('minlength', "$form");

$this->assertAccept($form, array(
Expand Down Expand Up @@ -291,7 +293,7 @@ public function testSelectOut()
$this->assertRefuse($form, array(
'city' => 'xy'
));

$this->assertAccept($form, array(
'city' => ''
));
Expand All @@ -307,7 +309,7 @@ public function testSelectOut()
$this->assertRefuse($form, array(
'city' => 'xy'
));

$this->assertRefuse($form, array(
'city' => ''
));
Expand All @@ -319,19 +321,19 @@ public function testSelectOut()
public function testSelectMultiple()
{
$form = $this->getForm('select-multiple.html');

$this->assertAccept($form, array(
'city' => array('pa')
));

$this->assertAccept($form, array(
'city' => array('pa', 'la')
));

$this->assertRefuse($form, array(
'city' => 'pa'
));

$this->assertRefuse($form, array(
'city' => array('xy')
));
Expand Down Expand Up @@ -391,7 +393,7 @@ public function testOptions()

$form->source('animals', array(
'1' => 'Cat',
'2' => 'Dog',
'2' => 'Dog',
'3' => 'Zebra'
));

Expand Down Expand Up @@ -538,8 +540,8 @@ public function testMultiCheckBox()
'animals' => array('1' => '1', '3' => '1')
));

$this->assertTrue(in_array('1', $form->animals));
$this->assertTrue(in_array('3', $form->animals));
$this->assertContains('1', $form->animals);
$this->assertContains('3', $form->animals);

$this->assertContains('checked', "$form");

Expand All @@ -556,7 +558,7 @@ public function testMultiCheckBox()
public function testMultiCheckBoxSetValue()
{
$form = $this->getForm('multicheckbox.html');

$form->source('animals', array(
'1' => 'Cat',
'2' => 'Dog',
Expand All @@ -565,7 +567,7 @@ public function testMultiCheckBoxSetValue()

$form->setValue('animals', array('2' => '1', '3' => '1'));
$this->assertEquals(array('2', '3'), $form->getValue('animals'));

$form->setValue('animals', array('2', '3'));
$this->assertEquals(array('2', '3'), $form->getValue('animals'));
}
Expand All @@ -576,7 +578,7 @@ public function testMultiCheckBoxSetValue()
public function testFile()
{
$factory = new Factory;
$factory->registerType('file', '\FileField_NoSave');
$factory->registerType('file', \FileField_NoSave::class);
$form = $factory->getForm(__DIR__.'/files/form/upload.html');
$file = __DIR__.'/files/upload/test.txt';
$hash = sha1(file_get_contents($file));
Expand Down Expand Up @@ -611,7 +613,7 @@ public function testFile()
public function testMultipleFiles()
{
$factory = new Factory;
$factory->registerType('file', '\FileField_NoSave');
$factory->registerType('file', \FileField_NoSave::class);
$form = $factory->getForm(__DIR__.'/files/form/multiple_file.html');
$file = __DIR__.'/files/upload/test.txt';

Expand All @@ -633,7 +635,7 @@ public function testMultipleFiles()
)
)
));

$this->assertRefuse($form, array(), array(
'files' => array(
0 => array(
Expand Down Expand Up @@ -748,17 +750,17 @@ public function testMultiple()
array('first_name' => 'Bob', 'age' => '8'),
array('first_name' => 'Jack', 'age' => '18'),
)));

$this->assertAccept($form, array('book_name' => 'Test', 'authors' => array(
array('first_name' => 'Bob', 'age' => '8'),
array('first_name' => 'Jack', 'age' => '18'),
)));

// The min-entries constraint
// The min-entries constraint
$this->assertRefuse($form, array('book_name' => 'Test', 'authors' => array(
array('first_name' => 'Bob', 'age' => '8'),
)));

// The min
$this->assertRefuse($form, array('book_name' => 'Test', 'authors' => array(
array('first_name' => 'Bob', 'age' => '3'),
Expand All @@ -781,7 +783,7 @@ public function testFixedMultiple()
array('first_name' => 'Bob', 'age' => '8'),
array('first_name' => 'Jack', 'age' => '18'),
)));

$this->assertRefuse($form, array('book_name' => 'Test', 'authors' => array(
array('first_name' => 'Bob', 'age' => '8'),
array('first_name' => 'Jack', 'age' => '18'),
Expand All @@ -793,16 +795,16 @@ public function testFixedMultiple()
array('first_name' => 'Jack', 'age' => '18'),
)));
}

/**
* Testing bad type

* @expectedException Gregwar\Formidable\ParserException
* @expectedExceptionMessage Unknown field type: name
*/
public function testCheckBadType()
{
$form = $this->getForm('bad_type.html');
$this->getForm('bad_type.html');
}

/**
Expand All @@ -812,18 +814,18 @@ public function testArrayNames()
{
$form = $this->getForm('array.html');

$this->assertTrue(is_array($form->getValue('t')));
$this->assertEquals(2, count($form->getValue('t')));
$this->assertInternalType('array', $form->getValue('t'));
$this->assertCount(2, $form->getValue('t'));

$values = $form->getValues();
$this->assertTrue(is_array($values['a']));
$this->assertTrue(is_array($values['a']['b']));
$this->assertInternalType('array', $values['a']);
$this->assertInternalType('array', $values['a']['b']);
$this->assertEquals('testing', $values['a']['b']['c']);
$this->assertEquals('testing2', $values['a']['b']['d']);

$this->assertTrue(is_array($values['x']));
$this->assertTrue(is_array($values['x']['y']));
$this->assertTrue(is_array($values['x']['y']['z']));
$this->assertInternalType('array', $values['x']);
$this->assertInternalType('array', $values['x']['y']);
$this->assertInternalType('array', $values['x']['y']['z']);
$this->assertEquals('2', $values['x']['y']['z']['k']);

$this->assertAccept($form, array(
Expand Down Expand Up @@ -955,7 +957,7 @@ private function getForm($file)
return new Form(__DIR__.'/files/form/'.$file);
}

public function setup()
protected function setup()
{
$_SESSION = array();
}
Expand Down
6 changes: 4 additions & 2 deletions tests/FactoryTests.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

use Gregwar\Formidable\Factory;
use Gregwar\Formidable\Fields\TextField;
use PHPUnit\Framework\TestCase;

/**
* Testing Formidable factory
*
* @author Grégoire Passault <g.passault@gmail.com>
*/
class FactoryTests extends \PHPUnit\Framework\TestCase
class FactoryTests extends TestCase
{
/**
* Testing creating forms using the factory
Expand All @@ -28,7 +30,7 @@ public function testFactoryCreation()
public function testFactoryCustomType()
{
$factory = new Factory;
$factory->registerType('testing', '\Gregwar\Formidable\Fields\TextField');
$factory->registerType('testing', TextField::class);

$form = $factory->getForm(__DIR__.'/files/factory/testing.html');
$html = "$form";
Expand Down
18 changes: 9 additions & 9 deletions tests/FormTests.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

use Gregwar\Cache\Cache;
use Gregwar\Formidable\Form;
use Gregwar\Formidable\PostIndicator;
use PHPUnit\Framework\TestCase;

/**
* Testing Formidable forms
*
* @author Grégoire Passault <g.passault@gmail.com>
*/
class FormTests extends \PHPUnit\Framework\TestCase
class FormTests extends TestCase
{
/**
* Testing that toString() give the same thing as getHtml()
Expand Down Expand Up @@ -38,7 +40,7 @@ public function testGuessPathOrContent()
public function testEnctype()
{
$form = $this->getForm('enctype_normal.html');
$this->assertFalse(strpos("$form", 'enctype='));
$this->assertNotContains('enctype=', "$form");

$form = $this->getForm('enctype_file.html');
$this->assertContains('enctype=', "$form");
Expand Down Expand Up @@ -176,7 +178,6 @@ public function testCsrfSecretGeneration()
*/
public function testPostIndicator()
{

unset($_SESSION);
$form1 = $this->getForm('post-indicator.html');
$token1 = $form1->getToken();
Expand Down Expand Up @@ -275,7 +276,7 @@ public function testQuotesAttributes()
{
$form = $this->getForm('quotes.html');
$field = $form->getField('xxx');

$this->assertTrue($field->hasAttribute('foo'));
$this->assertEquals($field->getAttribute('foo'), 'bar baz "bax"');

Expand All @@ -286,7 +287,6 @@ public function testQuotesAttributes()
$this->assertFalse($element == null);
$this->assertTrue($element->hasAttribute('foo'));
$this->assertEquals($element->getAttribute('foo'), 'bar baz "bax"');

}

public function testPlaceholder()
Expand Down Expand Up @@ -314,7 +314,7 @@ public function testPlaceholder()
*/
public function testCache()
{
$cache = new Gregwar\Cache\Cache;
$cache = new Cache;
$cache->setCacheDirectory($this->getCacheDirectory());

$form = $this->getForm('basic.html', null, $cache);
Expand Down Expand Up @@ -343,7 +343,7 @@ public function testSourceOrder()

preg_match_all('#option value="(.+)"#mUsi', $html, $matches);

$this->assertEquals(3, count($matches[1]));
$this->assertCount(3, $matches[1]);
$this->assertEquals('X', $matches[1][0]);
$this->assertEquals('A', $matches[1][1]);
$this->assertEquals('B', $matches[1][2]);
Expand All @@ -354,7 +354,7 @@ private function getForm($file, $vars = array(), $cache = false)
return new Form(__DIR__.'/files/form/'.$file, $vars, $cache);
}

public function setup()
protected function setup()
{
$_SESSION = array();
}
Expand All @@ -364,7 +364,7 @@ public function getCacheDirectory()
return __DIR__ . '/cache';
}

public function teardown()
protected function teardown()
{
$cacheDir = $this->getCacheDirectory();
`rm -rf $cacheDir`;
Expand Down
Loading