Completed
Push — master ( 0930e2...1db464 )
by Angus
08:04
created

MY_Form_validation_test::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
class MY_Form_validation_test extends TestCase {
4
	private $form_validation;
5
6
	public function __construct() {
7
		parent::__construct();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TestCase as the method __construct() does only exist in the following sub-classes of TestCase: MY_Form_validation_test. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
8
	}
9
10
	public function setUp() {
11
		$this->resetInstance();
0 ignored issues
show
Bug introduced by
The method resetInstance() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
12
		$this->CI->load->library('form_validation');
0 ignored issues
show
Bug introduced by
The property CI does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
13
		$this->form_validation = $this->CI->form_validation;
14
	}
15
16
	// A valid username is between 4 & 15 characters, and contains only [a-zA-Z0-9_-]
17
	public function test_valid_username_pass() {
18
		//username is valid, return true
19
		$result = $this->form_validation->valid_username('FooBar');
20
		$this->assertTrue($result);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
	}
22
	public function test_valid_username_fail_case_1() {
23
		//username is invalid (too short), return false
24
		$result = $this->form_validation->valid_username('Foo');
25
		$this->assertFalse($result);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
	}
27
	public function test_valid_username_fail_case_2() {
28
		//username is invalid (too long), return false
29
		$result = $this->form_validation->valid_username('FooBarFooBarFooBar');
30
		$this->assertFalse($result);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
	}
32
	public function test_valid_username_fail_case_3() {
33
		//username is invalid (invalid characters), return false
34
		$result = $this->form_validation->valid_username('フーバル');
35
		$this->assertFalse($result);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
	}
37
38
	//A valid password is between 6 & 64 characters, any characters are allowed.
39
	public function test_valid_password_pass() {
40
		//password is valid, return true
41
		$result = $this->form_validation->valid_password('FooBar2');
42
		$this->assertTrue($result);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
	}
44
	public function test_valid_password_fail_1() {
45
		//password is invalid (too short), return false
46
		$result = $this->form_validation->valid_password('Foo');
47
		$this->assertEquals('The password is too short!', $this->get_error_message('valid_password'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
		$this->assertFalse($result);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
	}
50
	public function test_valid_password_fail_2() {
51
		//password is invalid (too long), return false
52
		$result = $this->form_validation->valid_password('FooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBar');
53
		$this->assertEquals('The password is too long!', $this->get_error_message('valid_password'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
		$this->assertFalse($result);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
	}
56
57 View Code Duplication
	public function test_is_unique_username_pass() {
58
		//username is unique, return true
59
60
		$validation = $this->getDouble(
0 ignored issues
show
Bug introduced by
The method getDouble() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
				'MY_Form_validation',
62
				['is_unique' => TRUE]
63
		);
64
		$this->verifyInvokedOnce($validation, 'is_unique');
0 ignored issues
show
Bug introduced by
The method verifyInvokedOnce() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
66
		$result = $validation->is_unique_username('FooBar');
67
		$this->assertTrue($result);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
	}
69 View Code Duplication
	public function test_is_unique_username_fail() {
70
		//username already exists, return false
71
72
		$validation = $this->getDouble(
0 ignored issues
show
Bug introduced by
The method getDouble() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
				'MY_Form_validation',
74
				['is_unique' => FALSE]
75
		);
76
		$this->verifyInvokedOnce($validation, 'is_unique');
0 ignored issues
show
Bug introduced by
The method verifyInvokedOnce() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
78
		$result = $validation->is_unique_username('FooBar');
79
		$this->assertEquals('The username already exists in our database.', $this->get_error_message('is_unique_username', $validation));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
		$this->assertFalse($result);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
	}
82
83 View Code Duplication
	public function test_is_unique_email_pass() {
84
		//email is unique, return true
85
86
		$validation = $this->getDouble(
0 ignored issues
show
Bug introduced by
The method getDouble() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
				'MY_Form_validation',
88
				['is_unique' => TRUE]
89
		);
90
		$this->verifyInvokedOnce($validation, 'is_unique');
0 ignored issues
show
Bug introduced by
The method verifyInvokedOnce() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
92
		$result = $validation->is_unique_email('[email protected]');
93
		$this->assertTrue($result);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
	}
95 View Code Duplication
	public function test_is_unique_email_fail() {
96
		//email already exists, return false
97
98
		$validation = $this->getDouble(
0 ignored issues
show
Bug introduced by
The method getDouble() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
				'MY_Form_validation',
100
				['is_unique' => FALSE]
101
		);
102
		$this->verifyInvokedOnce($validation, 'is_unique');
0 ignored issues
show
Bug introduced by
The method verifyInvokedOnce() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
104
		$result = $validation->is_unique_email('[email protected]');
105
		$this->assertEquals('The email already exists in our database.', $this->get_error_message('is_unique_email', $validation));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
		$this->assertFalse($result);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
	}
108
109 View Code Duplication
	public function test_isRuleValid_pass() {
110
		//rule exists and has no errors, returns true
111
112
		//FIXME: This really isn't the best way to check this...
113
		$validation = $this->getDouble(
0 ignored issues
show
Bug introduced by
The method getDouble() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
				'MY_Form_validation',
115
				['has_rule' => TRUE, 'error_array' => []]
116
		);
117
		$this->verifyInvokedOnce($validation, 'has_rule');
0 ignored issues
show
Bug introduced by
The method verifyInvokedOnce() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
119
		$result = $validation->isRuleValid('valid_username');
120
		$this->assertTrue($result);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
	}
122
	public function test_isRuleValid_fail_1() {
123
		//rule exists but has errors, returns false
124
125
		//FIXME: This really isn't the best way to check this...
126
		$validation = $this->getDouble(
0 ignored issues
show
Bug introduced by
The method getDouble() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
				'MY_Form_validation',
128
				['has_rule' => TRUE, 'error_array' => ['valid_username' => 'Username is invalid format.']]
129
		);
130
		$this->verifyInvokedOnce($validation, 'has_rule');
0 ignored issues
show
Bug introduced by
The method verifyInvokedOnce() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
		$this->verifyInvokedOnce($validation, 'error_array');
0 ignored issues
show
Bug introduced by
The method verifyInvokedOnce() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
133
		$result = $validation->isRuleValid('valid_username');
134
		$this->assertFalse($result);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
	}
136 View Code Duplication
	public function test_isRuleValid_fail_2() {
137
		//rule does not exist, returns false
138
139
		//FIXME: This really isn't the best way to check this...
140
		$validation = $this->getDouble(
0 ignored issues
show
Bug introduced by
The method getDouble() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
				'MY_Form_validation',
142
				['has_rule' => FALSE]
143
		);
144
		$this->verifyInvokedOnce($validation, 'has_rule');
0 ignored issues
show
Bug introduced by
The method verifyInvokedOnce() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
146
		$result = $validation->isRuleValid('valid_username');
147
		$this->assertFalse($result);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<MY_Form_validation_test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
	}
149
150
	//utility functions
151
	function get_error_message(/*str*/$field, /*obj*/ $customObj = FALSE) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
		$obj = $customObj ?: $this->form_validation;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
153
		$error_messages = ReflectionHelper::getPrivateProperty(
154
				$obj,
155
				'_error_messages'
156
		);
157
158
		return $error_messages[$field];
159
	}
160
}
161