1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Test\Unit; |
4
|
|
|
|
5
|
|
|
use Test\Models\Post; |
6
|
|
|
use Test\Models\User; |
7
|
|
|
use Test\TestCase; |
8
|
|
|
use Taskforcedev\CrudApi\Helpers\CrudApi; |
9
|
|
|
|
10
|
|
|
class FieldHelperTest extends TestCase |
11
|
|
|
{ |
12
|
|
View Code Duplication |
public function testFieldHelperReturnsPrimaryFieldOfNameWhenNoDefaultSpecified() |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
$options = [ |
15
|
|
|
'namespace' => 'Test\\Models', |
16
|
|
|
'model' => 'post' |
17
|
|
|
]; |
18
|
|
|
$crudApi = new CrudApi($options); |
19
|
|
|
$fieldHelper = $crudApi->fieldHelper; |
20
|
|
|
|
21
|
|
|
$item = new Post(); |
22
|
|
|
|
23
|
|
|
$config = require __DIR__ . '/../../config/crudapi.php'; |
24
|
|
|
|
25
|
|
|
$primary_field = $fieldHelper->getPrimaryField($item, $config); |
26
|
|
|
|
27
|
|
|
$this->assertEquals('name', $primary_field); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testFieldHelperReturnsOverriddenModelDefaultField() |
31
|
|
|
{ |
32
|
|
|
$options = [ |
33
|
|
|
'namespace' => 'Test\\Models', |
34
|
|
|
'model' => 'user' |
35
|
|
|
]; |
36
|
|
|
$crudApi = new CrudApi($options); |
37
|
|
|
$fieldHelper = $crudApi->fieldHelper; |
38
|
|
|
|
39
|
|
|
$item = new User(); |
40
|
|
|
|
41
|
|
|
$config = [ |
42
|
|
|
'models' => [ |
43
|
|
|
'fields' => [ |
44
|
|
|
'default' => 'name', |
45
|
|
|
'primary' => [ |
46
|
|
|
'User' => 'forename,surname' |
47
|
|
|
], |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$primary_field = $fieldHelper->getPrimaryField($item, $config); |
53
|
|
|
|
54
|
|
|
$this->assertEquals('forename,surname', $primary_field); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testFieldsForFormCreate() |
58
|
|
|
{ |
59
|
|
|
$fields = ['name', 'password', 'post_code']; |
60
|
|
|
$options = [ |
61
|
|
|
'namespace' => null, |
62
|
|
|
'model' => 'user' |
63
|
|
|
]; |
64
|
|
|
$crudApi = new CrudApi($options); |
65
|
|
|
$render = $crudApi->fieldHelper->formCreate($fields); |
66
|
|
|
|
67
|
|
|
$this->assertNotFalse(strpos($render, 'name')); |
68
|
|
|
$this->assertNotFalse(strpos($render, 'password')); |
69
|
|
|
$this->assertNotFalse(strpos($render, 'post_code')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testFieldsForTableHeadings() |
73
|
|
|
{ |
74
|
|
|
$fields = ['name', 'password', 'post_code']; |
75
|
|
|
$options = [ |
76
|
|
|
'namespace' => null, |
77
|
|
|
'model' => 'user' |
78
|
|
|
]; |
79
|
|
|
$crudApi = new CrudApi($options); |
80
|
|
|
$render = $crudApi->fieldHelper->tableHeadings($fields); |
81
|
|
|
$this->assertEquals('<th>Name</th><th>Password</th><th>Post_code</th>', $render); |
82
|
|
|
} |
83
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.