Completed
Push — master ( 988d81...745db2 )
by Conrad
01:36
created

ValidatorTest::testErrors()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 0
1
<?php
0 ignored issues
show
Coding Style introduced by
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
3
use AdvancedLearning\InputValidator\Exceptions\InputValidationException;
4
use AdvancedLearning\InputValidator\InputValidator;
5
use Respect\Validation\Validator as v;
6
7
/**
8
 * Test validator
9
 */
10
class ValidatorTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
    public function setUp()
13
    {
14
        parent::setUp();
15
    }
16
17
    public function testErrors()
18
    {
19
        $data = [
20
            'FirstName' => '',
21
            'LastName' => '',
22
            'DateOfBirth' => '2017-09-20'
23
        ];
24
25
        $validator = new TestInputValidator();
26
        $validator->setMessages([
27
            'notBlank' => 'Please enter a value'
28
        ]);
29
30
        try {
31
            $validator->valid($data);
32
        } catch (InputValidationException $e) {
33
            $errors = $e->getMessages();
34
        }
35
36
        $this->assertEquals(4, count($errors), 'There should be four errors');
0 ignored issues
show
Bug introduced by
The variable $errors does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
37
        $this->assertNotEmpty($errors['FirstName'], 'FirstName should be in validation errors');
38
        $this->assertEquals(
39
            $errors['FirstName'][0],
40
            'Please enter a value',
41
            'Not blank message should have been updated'
42
        );
43
    }
44
}
45
46
class TestInputValidator extends InputValidator
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
47
{
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function getRules()
53
    {
54
        return [
55
            'FirstName' => v::notBlank()->setName('First Name'),
56
            'LastName' => v::notBlank()->setName('Last Name'),
57
            'NHI' => v::notBlank()->setName('NHI'),
58
            'DateOfBirth' => v::date('Y-m-d')
59
                ->notEmpty()
60
                ->between('1900-01-01', (new \DateTime('2017-08-01'))->format('Y-m-d'))
61
                ->setName('Date of Birth')
62
        ];
63
    }
64
}
65