Пример #1
0
 /**
  * @test
  * @covers SlimApp\Validator::validate
  * @covers SlimApp\Validator::hasErrors
  * @covers SlimApp\Validator::passed
  * @covers SlimApp\Validator::failed
  */
 public function validate_adds_errors_and_returns_validator_object_if_data_not_valid_and_no_exception_raised()
 {
     $validatorMethodsReturningTrue = ['required', 'min', 'max', 'matches', 'email', 'in', 'alpha', 'alphanum', 'url', 'unique'];
     $mock = $this->getMockBuilder('SlimApp\\Validator')->setMethods($validatorMethodsReturningTrue)->getMockForAbstractClass();
     foreach ($validatorMethodsReturningTrue as $methodName) {
         $mock->method($methodName)->will($this->returnValue('Error message from ' . $methodName));
     }
     $validator = new \SebastianBergmann\PeekAndPoke\Proxy($mock);
     // There were errors for the previous validation
     $previousErrors = ['username' => 'Username already exists.', 'password' => 'Password is too short'];
     $validator->errors = $previousErrors;
     $errorsFromPreviousValidation = $validator->getErrors();
     $fromPreviousHasErrors = $validator->hasErrors();
     $fromPreviousFailed = $validator->failed();
     $fromPreviousPassed = $validator->passed();
     $rules = ['username' => ['required' => true, 'min' => 4, 'max' => 24, 'alpha' => true, 'unique' => 'User'], 'password' => ['required' => true, 'min' => 4, 'max' => 24, 'alphanum' => true], 'passwordConfirm' => ['required' => true, 'matches' => 'password'], 'email' => ['required' => true, 'email' => true], 'website' => ['url' => ['schemeRequired', 'hostRequired']], 'gender' => ['in' => ['male', 'female']]];
     $validator->rules = $rules;
     $data = ['username' => null, 'password' => null, 'passwordConfirm' => null, 'email' => null, 'website' => null, 'gender' => null];
     $expectedErrors = ['username' => ['Error message from required', 'Error message from min', 'Error message from max', 'Error message from alpha', 'Error message from unique'], 'password' => ['Error message from required', 'Error message from min', 'Error message from max', 'Error message from alphanum'], 'passwordConfirm' => ['Error message from required', 'Error message from matches'], 'email' => ['Error message from required', 'Error message from email'], 'website' => ['Error message from url'], 'gender' => ['Error message from in']];
     $result = $validator->validate($data);
     $errorsAfterFailingValidation = $validator->getErrors();
     $afterFailingValidationHasErrors = $validator->hasErrors();
     $afterFailingValidationFailed = $validator->failed();
     $afterFailingValidationPassed = $validator->passed();
     $this->assertEquals($previousErrors, $errorsFromPreviousValidation);
     $this->assertTrue($fromPreviousHasErrors);
     $this->assertTrue($fromPreviousFailed);
     $this->assertFalse($fromPreviousPassed);
     $this->assertInstanceOf('\\SlimApp\\Validator', $result);
     $this->assertEquals($expectedErrors, $errorsAfterFailingValidation);
     $this->assertTrue($afterFailingValidationHasErrors);
     $this->assertTrue($afterFailingValidationFailed);
     $this->assertFalse($afterFailingValidationPassed);
 }