public function testInputIncorrectStringFromHttpPost() { global $_POST; $_POST['TestString'] = '@Test'; // Invalid format $_POST['TestNotAStr'] = array('Array here'); // Another invalid format $errors = array(); $exceptionCatched = false; $input = Input\Input::from(Input\Source\HttpPost::import())->fields(Input\Field\Strings::bind('TestString')->defaults('Default Test')->limits(Input\Limit\Validate::create()->format('alphanumber')->maxlen(64)->minlen(1)), Input\Field\Strings::bind('TestNotAStr')->defaults('Default Test')->limits(Input\Limit\Validate::create()->format('standard')->maxlen(64)->minlen(1)))->errors($errors)->prepare(); // prepare returns a new object: Input\Result $this->assertEquals(2, count($errors)); // Error type should be INVALID $this->assertEquals('INVALID', $errors[0]->type()); $this->assertEquals('INVALID', $errors[1]->type()); // Error code should be FORMAT $this->assertEquals('FORMAT', $errors[0]->code()); // And this should be DATA_TYPE $this->assertEquals('DATATYPE', $errors[1]->code()); // Error code should be TESTSTRING_FORMAT $this->assertEquals('TESTSTRING_FORMAT', $errors[0]->errorCode()); // And this should be TESTNOTASTR_DATA_TYPE $this->assertEquals('TESTNOTASTR_DATATYPE', $errors[1]->errorCode()); try { $input->get('TestString')->value(); } catch (Input\Base\Exception\Results\FieldNotFound $e) { $exceptionCatched = true; } // An exception should be throw out $this->assertTrue($exceptionCatched); }
public function testWithInput() { global $_POST; $_POST['testOneof'] = 'ValueA'; $_POST['testOneof2'] = 'ValueB'; $errors = array(); $input = Input\Input::from(Input\Source\HttpPost::import())->fields(Input\Field\Strings::bind('testOneof')->limits(Input\Limit\Oneof::create()->add('ValueA'))->limits(Input\Limit\Oneof::create()->add('ValueC')), Input\Field\Strings::bind('testOneof2')->limits(Input\Limit\Oneof::create()->add('ValueA'))->limits(Input\Limit\Oneof::create()->add('ValueC'))->defaults('ValueA'))->errors($errors)->prepare(); // There should be one error $this->assertEquals(1, count($errors)); // Error should be CASE_UNKNOWN $this->assertEquals('INVALID', $errors[0]->type()); $this->assertEquals('CASEUNKNOWN', $errors[0]->code()); }
public function testInputArrayedStringButInvalid2() { global $_POST; $_POST['TestStringArray3']['UserName'] = '******'; $_POST['TestStringArray3']['Password'] = '******'; $_POST['TestStringArray2'] = array(array('UserName' => '!Username1', 'Password' => '!1234567890'), array('UserName' => '!Username2', 'Password' => '!1234567891')); $errors = array(); $input = Input\Input::from(Input\Source\HttpPost::import())->fields(Input\Field\Subs::bind('TestStringArray3')->subs(Input\Field\Strings::bind('UserName')->required(true)->limits(Input\Limit\Validate::create()->format('username')), Input\Field\Strings::bind('Password')->required(true)->limits(Input\Limit\Validate::create()->format('username'))), Input\Field\Groups::bind('TestStringArray2')->adds(Input\Field\Strings::bind('UserName')->limits(Input\Limit\Validate::create()->format('username')), Input\Field\Strings::bind('Password')))->errors($errors)->prepare(); // The error should be 0 $this->assertEquals(2, count($errors)); // Error on TestStringArray3 $this->assertEquals('INVALID', $errors[0]->type()); $this->assertEquals('FORMAT', $errors[0]->code()); // Error on TestStringArray2 $this->assertEquals('INVALID', $errors[1]->type()); $this->assertEquals('FORMAT', $errors[1]->code()); }