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

Test Setup Failed
Pull Request — main (#5725)
by Pedro
25:49 queued 10:59
created

CrudPanelColumnsLinkToTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 95
c 1
b 1
f 0
dl 0
loc 158
rs 10
wmc 13

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testColumnArrayDefinitionLinkToRouteNameAndAdditionalParameters() 0 22 1
A testColumnLinkToWithCustomParameters() 0 7 1
A testColumnArrayDefinitionLinkToRouteAsClosure() 0 12 1
A setUp() 0 5 1
A testColumnArrayDefinitionLinkToRouteNameOnly() 0 12 1
A testColumnLinkToWithClosure() 0 9 1
A testColumnLinkToThrowsExceptionWhenNotAllRequiredParametersAreFilled() 0 5 1
A testItThrowsExceptionIfRouteNotFound() 0 6 1
A testColumnLinkToWithRouteNameAndAdditionalParameters() 0 11 1
A testColumnLinkToAutoInferAnySingleParameter() 0 9 1
A testColumnLinkToDontAutoInferParametersIfAllProvided() 0 9 1
A testColumnLinkToWithCustomClosureParameters() 0 9 1
A testColumnLinkToWithRouteNameOnly() 0 11 1
1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudColumn;
6
use Backpack\CRUD\Tests\config\Models\Article;
7
use Backpack\CRUD\Tests\config\Models\User;
8
9
/**
10
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Columns
11
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\ColumnsProtectedMethods
12
 * @covers Backpack\CRUD\app\Library\CrudPanel\CrudColumn
13
 * @covers Backpack\CRUD\app\Library\CrudPanel\CrudPanel
14
 */
15
class CrudPanelColumnsLinkToTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel
16
{
17
    /**
18
     * Setup the test environment.
19
     *
20
     * @return void
21
     */
22
    protected function setUp(): void
23
    {
24
        parent::setUp();
25
26
        $this->crudPanel->setOperation('list');
27
    }
28
29
    public function testColumnLinkToThrowsExceptionWhenNotAllRequiredParametersAreFilled()
30
    {
31
        $this->expectException(\Exception::class);
32
        $this->expectExceptionMessage('Route [article.show.detail] expects parameters [id, detail]. Insufficient parameters provided in column: [articles].');
33
        $this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['test' => 'testing']);
0 ignored issues
show
Bug introduced by
The method entity() does not exist on Backpack\CRUD\app\Library\CrudPanel\CrudColumn. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $this->crudPanel->column('articles')->/** @scrutinizer ignore-call */ entity('articles')->linkTo('article.show.detail', ['test' => 'testing']);
Loading history...
34
    }
35
36
    public function testItThrowsExceptionIfRouteNotFound()
37
    {
38
        $this->expectException(\Exception::class);
39
        $this->expectExceptionMessage('Route [users.route.doesnt.exist] not found while building the link for column [id].');
40
41
        CrudColumn::name('id')->linkTo('users.route.doesnt.exist')->toArray();
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on Backpack\CRUD\app\Library\CrudPanel\CrudColumn. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        CrudColumn::name('id')->linkTo('users.route.doesnt.exist')->/** @scrutinizer ignore-call */ toArray();
Loading history...
42
    }
43
44
    public function testColumnLinkToWithRouteNameOnly()
45
    {
46
        $this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show');
47
        $columnArray = $this->crudPanel->columns()['articles'];
48
        $reflection = new \ReflectionFunction($columnArray['wrapper']['href']);
49
        $arguments = $reflection->getClosureUsedVariables();
0 ignored issues
show
Bug introduced by
The method getClosureUsedVariables() does not exist on ReflectionFunction. Did you maybe mean getClosure()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        /** @scrutinizer ignore-call */ 
50
        $arguments = $reflection->getClosureUsedVariables();

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...
50
        $this->crudPanel->entry = Article::first();
51
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
52
        $this->assertEquals('articles.show', $arguments['route']);
53
        $this->assertCount(1, $arguments['parameters']);
54
        $this->assertEquals('http://localhost/admin/articles/1/show', $url);
55
    }
56
57
    public function testColumnLinkToWithRouteNameAndAdditionalParameters()
58
    {
59
        $this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show', ['test' => 'testing', 'test2' => 'testing2']);
60
        $columnArray = $this->crudPanel->columns()['articles'];
61
        $reflection = new \ReflectionFunction($columnArray['wrapper']['href']);
62
        $arguments = $reflection->getClosureUsedVariables();
63
        $this->assertEquals('articles.show', $arguments['route']);
64
        $this->assertCount(3, $arguments['parameters']);
65
        $this->crudPanel->entry = Article::first();
66
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
67
        $this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=testing2', $url);
68
    }
69
70
    public function testColumnLinkToWithCustomParameters()
71
    {
72
        $this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['detail' => 'testing', 'otherParam' => 'test']);
73
        $columnArray = $this->crudPanel->columns()['articles'];
74
        $this->crudPanel->entry = Article::first();
75
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
76
        $this->assertEquals('http://localhost/admin/articles/1/show/testing?otherParam=test', $url);
77
    }
78
79
    public function testColumnLinkToWithCustomClosureParameters()
80
    {
81
        $this->crudPanel->column('articles')
82
                        ->entity('articles')
83
                        ->linkTo('article.show.detail', ['detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]);
84
        $columnArray = $this->crudPanel->columns()['articles'];
85
        $this->crudPanel->entry = Article::first();
86
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
87
        $this->assertEquals('http://localhost/admin/articles/1/show/1?otherParam=Some%20Content', $url);
88
    }
89
90
    public function testColumnLinkToDontAutoInferParametersIfAllProvided()
91
    {
92
        $this->crudPanel->column('articles')
93
                        ->entity('articles')
94
                        ->linkTo('article.show.detail', ['id' => 123, 'detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]);
95
        $columnArray = $this->crudPanel->columns()['articles'];
96
        $this->crudPanel->entry = Article::first();
97
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
98
        $this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url);
99
    }
100
101
    public function testColumnLinkToAutoInferAnySingleParameter()
102
    {
103
        $this->crudPanel->column('articles')
104
                        ->entity('articles')
105
                        ->linkTo('article.show.detail', ['id' => 123, 'otherParam' => fn ($entry) => $entry->content]);
106
        $columnArray = $this->crudPanel->columns()['articles'];
107
        $this->crudPanel->entry = Article::first();
108
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
109
        $this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url);
110
    }
111
112
    public function testColumnLinkToWithClosure()
113
    {
114
        $this->crudPanel->column('articles')
115
                        ->entity('articles')
116
                        ->linkTo(fn ($entry) => route('articles.show', $entry->content));
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type string expected by parameter $routeName of Backpack\CRUD\app\Librar...el\CrudColumn::linkTo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
                        ->linkTo(/** @scrutinizer ignore-type */ fn ($entry) => route('articles.show', $entry->content));
Loading history...
117
        $columnArray = $this->crudPanel->columns()['articles'];
118
        $this->crudPanel->entry = Article::first();
119
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
120
        $this->assertEquals('http://localhost/admin/articles/Some%20Content/show', $url);
121
    }
122
123
    public function testColumnArrayDefinitionLinkToRouteAsClosure()
124
    {
125
        $this->crudPanel->setModel(User::class);
126
        $this->crudPanel->column([
127
            'name' => 'articles',
128
            'entity' => 'articles',
129
            'linkTo' => fn ($entry) => route('articles.show', ['id' => $entry->id, 'test' => 'testing']),
130
        ]);
131
        $columnArray = $this->crudPanel->columns()['articles'];
132
        $this->crudPanel->entry = Article::first();
133
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
134
        $this->assertEquals('http://localhost/admin/articles/1/show?test=testing', $url);
135
    }
136
137
    public function testColumnArrayDefinitionLinkToRouteNameOnly()
138
    {
139
        $this->crudPanel->setModel(User::class);
140
        $this->crudPanel->column([
141
            'name' => 'articles',
142
            'entity' => 'articles',
143
            'linkTo' => 'articles.show',
144
        ]);
145
        $columnArray = $this->crudPanel->columns()['articles'];
146
        $this->crudPanel->entry = Article::first();
147
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
148
        $this->assertEquals('http://localhost/admin/articles/1/show', $url);
149
    }
150
151
    public function testColumnArrayDefinitionLinkToRouteNameAndAdditionalParameters()
152
    {
153
        $this->crudPanel->setModel(User::class);
154
        $this->crudPanel->column([
155
            'name' => 'articles',
156
            'entity' => 'articles',
157
            'linkTo' => [
158
                'route' => 'articles.show',
159
                'parameters' => [
160
                    'test' => 'testing',
161
                    'test2' => fn ($entry) => $entry->content,
162
                ],
163
            ],
164
        ]);
165
        $columnArray = $this->crudPanel->columns()['articles'];
166
        $reflection = new \ReflectionFunction($columnArray['wrapper']['href']);
167
        $arguments = $reflection->getClosureUsedVariables();
168
        $this->assertEquals('articles.show', $arguments['route']);
169
        $this->assertCount(3, $arguments['parameters']);
170
        $this->crudPanel->entry = Article::first();
171
        $url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
172
        $this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=Some%20Content', $url);
173
    }
174
}
175