Skip to content

Commit

Permalink
Merge pull request #27 from gios-asu/ivan-develop
Browse files Browse the repository at this point in the history
Request fixes
  • Loading branch information
idmontie committed Sep 18, 2015
2 parents 4a00fd6 + f9a4f3c commit c8a1ebb
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/requests/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ abstract class Request {
public function validate( $error_callback ) {
$rules = $this->validation_rules();

if ( is_array( $error_callback ) ) {
return call_user_func_array( $error_callback, [ $rules, $this ] );
$results = [];

foreach ( $rules as $name => $check ) {
if ( true !== $check ) {
if ( is_array( $error_callback ) ) {
$results[] = call_user_func_array( $error_callback, [ $check, $this ] );
} else {
$results[] = $error_callback( $check, $this );
}
}
}

return $error_callback( $rules, $this );
if ( count( $results ) > 0 ) {
return $results;
}
}

abstract public function validation_rules();
Expand Down
82 changes: 82 additions & 0 deletions tests/unit/request-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Nectary\Tests;

use Nectary\Request;

/**
* @group request
*/
class Request_Test extends \PHPUnit_Framework_TestCase {
function test_error_callback_is_not_called_when_validation_passes() {
$request = $this->getMockForAbstractClass( Request::class );
$request->expects( $this->once() )
->method( 'validation_rules' )
->will( $this->returnValue( [ 'test' => true ] ) );


$mock_function = $this->getMockBuilder( 'Object' )
->setMethods( ['no_call_function'] )
->getMock();

$mock_function->expects( $this->exactly( 0 ) )
->method( 'no_call_function' )
->will($this->returnValue( false ) );

$request->validate( array( $mock_function, 'no_call_function' ) );
}

function test_error_callback_is_called_with_failures() {
$request = $this->getMockForAbstractClass( Request::class );
$request->expects( $this->once() )
->method( 'validation_rules' )
->will( $this->returnValue( [ 'test' => 'my error message' ] ) );


$mock_function = $this->getMockBuilder( 'Object' )
->setMethods( ['error_function'] )
->getMock();

$mock_function->expects( $this->exactly( 1 ) )
->method( 'error_function' )
->will($this->returnValue( 'my failure object' ) );

$result = $request->validate( array( $mock_function, 'error_function' ) );

$this->assertCount( 1, $result );
$this->assertEquals( 'my failure object', $result[0] );
}

function test_error_callback_is_called_with_many_failures() {
$request = $this->getMockForAbstractClass( Request::class );
$request->expects( $this->once() )
->method( 'validation_rules' )
->will( $this->returnValue(
array(
'test' => 'my error message',
'test2' => 'another error message',
)
)
);


$mock_function = $this->getMockBuilder( 'Object' )
->setMethods( ['error_function'] )
->getMock();

$mock_function->expects( $this->exactly( 2 ) )
->method( 'error_function' )
->will(
$this->onConsecutiveCalls(
'my failure object',
'another failure object'
)
);

$result = $request->validate( array( $mock_function, 'error_function' ) );

$this->assertCount( 2, $result );
$this->assertEquals( 'my failure object', $result[0] );
$this->assertEquals( 'another failure object', $result[1] );
}
}

0 comments on commit c8a1ebb

Please sign in to comment.