Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — add-tests ( 194aa8 )
by Pedro
15:05
created

testCompactFakeFieldsFromRequestWithNoFakes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
use Backpack\CRUD\Tests\config\Models\Article;
6
use Backpack\CRUD\Tests\config\Models\User;
7
use Illuminate\Support\Facades\DB;
8
9
/**
10
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\FakeFields
11
 */
12
class CrudPanelFakeFieldsTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel
13
{
14
    private $fakeFieldsArray = [
15
        [
16
            'name' => 'field',
17
            'label' => 'Normal Field',
18
        ],
19
        [
20
            'name' => 'meta_title',
21
            'label' => 'Meta Title',
22
            'fake' => true,
23
            'store_in' => 'metas',
24
        ],
25
        [
26
            'name' => 'meta_description',
27
            'label' => 'Meta Description',
28
            'fake' => true,
29
            'store_in' => 'metas',
30
        ],
31
        [
32
            'name' => 'meta_keywords',
33
            'label' => 'Meta Keywords',
34
            'fake' => true,
35
            'store_in' => 'metas',
36
        ],
37
        [
38
            'name' => 'tags',
39
            'label' => 'Tags',
40
            'fake' => true,
41
            'store_in' => 'tags',
42
        ],
43
        [
44
            'name' => 'extra_details',
45
            'label' => 'Extra Details',
46
            'fake' => true,
47
        ],
48
        [
49
            'name' => 'cast_meta_title',
50
            'label' => 'Meta Title',
51
            'fake' => true,
52
            'store_in' => 'cast_metas',
53
        ],
54
        [
55
            'name' => 'cast_meta_description',
56
            'label' => 'Meta Description',
57
            'fake' => true,
58
            'store_in' => 'cast_metas',
59
        ],
60
        [
61
            'name' => 'cast_meta_keywords',
62
            'label' => 'Meta Keywords',
63
            'fake' => true,
64
            'store_in' => 'cast_metas',
65
        ],
66
        [
67
            'name' => 'cast_tags',
68
            'label' => 'Tags',
69
            'fake' => true,
70
            'store_in' => 'cast_tags',
71
        ],
72
        [
73
            'name' => 'cast_extra_details',
74
            'label' => 'Extra Details',
75
            'fake' => true,
76
            'store_in' => 'cast_extras',
77
        ],
78
    ];
79
80
    private $noFakeFieldsInputData = [
81
        'value1' => 'Value 1',
82
        'value2' => 'Value 2',
83
        'value3' => 'Value 3',
84
    ];
85
86
    private $fakeFieldsInputData = [
87
        'value1' => 'Value 1',
88
        'value2' => 'Value 2',
89
        'value3' => 'Value 3',
90
        'meta_title' => 'Meta Title Value',
91
        'meta_description' => 'Meta Description Value',
92
        'tags' => ['tag1', 'tag2', 'tag3'],
93
        'extra_details' => ['detail1', 'detail2', 'detail3'],
94
        'cast_meta_title' => 'Meta Title Value',
95
        'cast_meta_description' => 'Meta Description Value',
96
        'cast_tags' => ['tag1', 'tag2', 'tag3'],
97
        'cast_extra_details' => ['detail1', 'detail2', 'detail3'],
98
    ];
99
100
    private $expectedInputDataWithCompactedFakeFields = [
101
        'value1' => 'Value 1',
102
        'value2' => 'Value 2',
103
        'value3' => 'Value 3',
104
        'metas' => '{"meta_title":"Meta Title Value","meta_description":"Meta Description Value"}',
105
        'tags' => '{"tags":["tag1","tag2","tag3"]}',
106
        'extras' => '{"extra_details":["detail1","detail2","detail3"]}',
107
        'cast_metas' => [
108
            'cast_meta_title' => 'Meta Title Value',
109
            'cast_meta_description' => 'Meta Description Value',
110
        ],
111
        'cast_tags' => [
112
            'cast_tags' => ['tag1', 'tag2', 'tag3'],
113
        ],
114
        'cast_extras' => [
115
            'cast_extra_details' => ['detail1', 'detail2', 'detail3'],
116
        ],
117
    ];
118
119
    public function testCompactFakeFieldsFromCreateForm()
120
    {
121
        $this->crudPanel->addFields($this->fakeFieldsArray);
122
        $this->crudPanel->setModel(Article::class);
123
124
        $compactedFakeFields = $this->crudPanel->compactFakeFields($this->fakeFieldsInputData);
125
126
        $this->assertEquals($this->expectedInputDataWithCompactedFakeFields, $compactedFakeFields);
127
    }
128
129
    public function testCompactFakeFieldsFromUpdateForm()
130
    {
131
        $article = DB::table('articles')->where('id', 1)->first();
0 ignored issues
show
Unused Code introduced by
The assignment to $article is dead and can be removed.
Loading history...
132
        $this->crudPanel->setModel(Article::class);
133
        $this->crudPanel->addFields($this->fakeFieldsArray);
134
135
        $compactedFakeFields = $this->crudPanel->compactFakeFields($this->fakeFieldsInputData);
136
137
        $this->assertEquals($this->expectedInputDataWithCompactedFakeFields, $compactedFakeFields);
138
    }
139
140
    public function testCompactFakeFieldsFromUpdateFormWithoutId()
141
    {
142
        $this->crudPanel->setModel(Article::class);
143
        $this->crudPanel->setOperation('update');
144
        $this->crudPanel->addFields($this->fakeFieldsArray);
145
146
        $compactedFakeFields = $this->crudPanel->compactFakeFields($this->fakeFieldsInputData);
147
148
        $this->assertEquals($this->expectedInputDataWithCompactedFakeFields, $compactedFakeFields);
149
    }
150
151
    public function testCompactFakeFieldsFromUpdateFormWithUnknownId()
152
    {
153
        $unknownId = DB::getPdo()->lastInsertId() + 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $unknownId is dead and can be removed.
Loading history...
154
        $this->crudPanel->setModel(Article::class);
155
        $this->crudPanel->setOperation('update');
156
        $this->crudPanel->addFields($this->fakeFieldsArray);
157
158
        $compactedFakeFields = $this->crudPanel->compactFakeFields($this->fakeFieldsInputData);
159
160
        $this->assertEquals($this->expectedInputDataWithCompactedFakeFields, $compactedFakeFields);
161
    }
162
163
    public function testCompactFakeFieldsFromEmptyRequest()
164
    {
165
        $compactedFakeFields = $this->crudPanel->compactFakeFields([]);
166
167
        $this->assertEmpty($compactedFakeFields);
168
    }
169
170
    public function testCompactFakeFieldsFromRequestWithNoFakes()
171
    {
172
        $compactedFakeFields = $this->crudPanel->compactFakeFields($this->noFakeFieldsInputData);
173
174
        $this->assertEquals($this->noFakeFieldsInputData, $compactedFakeFields);
175
    }
176
    
177
    public function testCompactRelationshipSubfields()
178
    {
179
        $this->crudPanel->setModel(User::class);
180
        $this->crudPanel->addField([
181
            'name' => 'articles',
182
            'subfields' => [
183
                [
184
                    'name' => 'content',
185
                    'fake' => true,
186
                ],
187
                [
188
                    'name' => 'metas',
189
                    'fake' => true,
190
                ],
191
            ],
192
        ]);
193
194
        $compactedFakeFields = $this->crudPanel->compactFakeFields([     
195
            'content' => 'Content Value',
196
            'metas' => ['meta1', 'meta2', 'meta3'],
197
        ], Article::class);
198
199
        $this->assertEquals([
200
            'extras' => "{\"content\":\"Content Value\",\"metas\":[\"meta1\",\"meta2\",\"meta3\"]}"
201
          ], $compactedFakeFields);
202
    }
203
}
204