Completed
Push — master ( b1aac2...36d657 )
by Avtandil
09:00
created

AllowCorsRequestsTest::getEnvironmentSetUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Tests\Unit\Middleware;
5
6
use Longman\LaravelLodash\Middlewares\AllowCorsRequests;
7
use Tests\Unit\TestCase;
8
9
use function config;
10
use function implode;
11
12
class AllowCorsRequestsTest extends TestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
13
{
14
    protected function getEnvironmentSetUp($app)
15
    {
16
        /** @var \Illuminate\Routing\Router $router */
17
        $router = $app['router'];
18
19
        $router->get('url1', static function () {
20
            return 'ok';
21
        })->middleware(AllowCorsRequests::class);
22
23
        $router->options('url1', static function () {
24
            return 'ok';
25
        })->middleware(AllowCorsRequests::class);
26
    }
27
28
    /** @test */
29
    public function it_should_return_correct_headers_on_options()
30
    {
31
        $origin = 'domain.com';
32
        config()->set('lodash.cors.allow_origins', [$origin]);
33
        $headers = [
34
            'Origin',
35
            'X-Requested-With',
36
            'Content-Type',
37
            'Accept',
38
            'Authorization',
39
            'CustomHeader1234',
40
        ];
41
        config()->set('lodash.cors.allow_headers', $headers);
42
        $methods = [
43
            'HEAD',
44
            'GET',
45
            'POST',
46
            'OPTIONS',
47
            'PUT',
48
            'PATCH',
49
            'DELETE',
50
            'CustomMethod1234',
51
        ];
52
        config()->set('lodash.cors.allow_methods', $methods);
53
54
        $response = $this->call('OPTIONS', 'url1', [], [], [], ['HTTP_Origin' => 'https://' . $origin]);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'https://' . $origin) is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
        $response->assertSeeText('Allowed');
56
        $response->assertHeader('Access-Control-Allow-Origin', 'https://' . $origin);
0 ignored issues
show
Bug introduced by
The method assertHeader() does not exist on Illuminate\Http\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
57
        $response->assertHeader('Access-Control-Allow-Credentials', 'true');
0 ignored issues
show
Bug introduced by
The method assertHeader() does not exist on Illuminate\Http\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
58
        $response->assertHeader('Access-Control-Allow-Methods', implode(',', $methods));
0 ignored issues
show
Bug introduced by
The method assertHeader() does not exist on Illuminate\Http\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
59
        $response->assertHeader('Access-Control-Allow-Headers', implode(',', $headers));
0 ignored issues
show
Bug introduced by
The method assertHeader() does not exist on Illuminate\Http\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
        $response->assertHeader('Access-Control-Max-Age', '1728000');
0 ignored issues
show
Bug introduced by
The method assertHeader() does not exist on Illuminate\Http\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
61
        $response->assertStatus(200);
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
62
    }
63
64
    /** @test */
65
    public function it_should_ignore_when_origin_missing()
66
    {
67
        $response = $this->call('GET', 'url1');
68
        $response->assertStatus(200);
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
69
    }
70
71
    /** @test */
72
    public function it_should_return_error_on_invalid_origin()
73
    {
74
        config()->set('lodash.cors.allow_origins', ['domain.com']);
75
76
        foreach (['GET', 'OPTIONS'] as $method) {
77
            // Invalid domain
78
            $response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'safsagsafsadsadsa']);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'safsagsafsadsadsa') is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
            $response->assertStatus(400); // Bad request
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
80
81
            // Without http://
82
            $response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'google.com']);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'google.com') is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
            $response->assertStatus(400); // Bad request
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
84
85
            // Valid domain, but not whitelisted
86
            $response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'http://google.com']);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'http://google.com') is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
            $response->assertStatus(405); // Method not allowed
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
88
89
            // Valid similar domain, but not whitelisted
90
            $response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'http://mydomain.com']);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'http://mydomain.com') is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
            $response->assertStatus(405); // Method not allowed
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
92
        }
93
    }
94
95
    /** @test */
96
    public function it_should_return_success_on_valid_origin()
97
    {
98
        config()->set('lodash.cors.allow_origins', ['domain.com']);
99
100
        $origins = config('lodash.cors.allow_origins');
101
102
        // Whitelisted origin
103
        $response = $this->call('GET', 'url1', [], [], [], ['HTTP_Origin' => 'http://' . $origins[0]]);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'http://' . $origins[0]) is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
        $response->assertStatus(200);
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
105
106
        // Whitelisted subdomain origin
107
        $response = $this->call('GET', 'url1', [], [], [], ['HTTP_Origin' => 'http://sub.' . $origins[0]]);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => '...://sub.' . $origins[0]) is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
        $response->assertStatus(200);
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
109
    }
110
111
    /** @test */
112
    public function it_should_return_success_on_project_url()
113
    {
114
        config()->set('lodash.cors.allow_origins', ['domain.com']);
115
116
        // Whitelisted origin
117
        $response = $this->call('GET', 'url1', [], [], [], ['HTTP_Origin' => config('app.url', 'http://localhost')]);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => \...', 'http://localhost')) is of type array<string,*,{"HTTP_Origin":"*"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118
        $response->assertStatus(200);
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
119
    }
120
121
}
122