Completed
Branch 2.0 (f08412)
by Zhengchao
01:19
created

AffiliateTrackingTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 88
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testAffiliateLink() 0 8 1
A testGetAffiliateIdFromCookieAndWithoutCookie() 0 20 1
A testGetAffiliateIdFromCookieAndWithCookie() 0 19 1
A testCreateReferral() 0 18 1
A mockingCookieFacade() 0 10 1
1
<?php
2
3
/*
4
 * This file is part of questocat/laravel-referral package.
5
 *
6
 * (c) questocat <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Tests;
13
14
use Illuminate\Http\Request;
15
use Illuminate\Support\Facades\Cookie;
16
use Mockery as m;
17
use Questocat\Referral\Http\Middleware\AffiliateTracking;
18
use Questocat\Referral\Referral;
19
use Tests\Stubs\ApplicationStub;
20
use Tests\Stubs\UserStub;
21
22
class AffiliateTrackingTest extends TestCase
23
{
24
    public function testAffiliateLink()
25
    {
26
        $user = UserStub::find(1);
27
        $url = config('app.url');
28
29
        $this->assertEquals($url.'/?ref='.$user->affiliate_id, $user->getAffiliateLink());
30
        $this->assertEquals($url.'/register/?ref='.$user->affiliate_id, $user->getAffiliateLink('/register'));
31
    }
32
33
    public function testGetAffiliateIdFromCookieAndWithoutCookie()
34
    {
35
        $user = UserStub::find(1);
36
        $request = m::mock(Request::class);
37
        $cookieName = config('referral.ref_cookie');
38
39
        $request->shouldReceive('query')->once()->andReturn($user->affiliate_id);
40
        $this->mockingCookieFacade($request, $cookieName, null);
41
42
        $middleware = new AffiliateTracking();
43
        $response = $middleware->handle($request, function ($request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
            return response('');
45
        });
46
47
        $cookie = $response->headers->getCookies()[0];
48
49
        $this->assertEquals(200, $response->getStatusCode());
50
        $this->assertEquals($cookieName, $cookie->getName());
51
        $this->assertEquals($user->affiliate_id, $cookie->getValue());
52
    }
53
54
    public function testGetAffiliateIdFromCookieAndWithCookie()
55
    {
56
        $user = UserStub::find(1);
57
        $request = m::mock(Request::class);
58
        $cookieName = config('referral.ref_cookie');
59
60
        $request->shouldReceive('query')->once()->andReturn($user->affiliate_id);
61
        $this->mockingCookieFacade($request, $cookieName, $user->affiliate_id);
62
63
        $middleware = new AffiliateTracking();
64
        $response = $middleware->handle($request, function ($request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
            return response('');
66
        });
67
68
        $cookie = $response->headers->getCookies();
69
70
        $this->assertEquals(200, $response->getStatusCode());
71
        $this->assertEmpty($cookie);
72
    }
73
74
    public function testCreateReferral()
75
    {
76
        $user = UserStub::find(1);
77
        $cookieName = config('referral.ref_cookie');
78
        $request = m::mock(Request::class);
79
        $this->mockingCookieFacade($request, $cookieName, $user->affiliate_id);
80
81
        $referral = UserStub::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on Tests\Stubs\UserStub. Did you maybe mean created()?

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...
82
            'name' => 'cat',
83
            'email' => '[email protected]',
84
            'password' => bcrypt('secret'),
85
        ]);
86
87
        $result = Referral::whereReferrerId($user->id)->first();
0 ignored issues
show
Bug introduced by
The method whereReferrerId() does not exist on Questocat\Referral\Referral. Did you maybe mean referrer()?

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
        $this->assertEquals($referral->id, $result->referral_id);
90
        $this->assertEquals($user->id, $result->referrer_id);
91
    }
92
93
    /**
94
     * @param Request $request
95
     * @param         $cookieName
96
     * @param         $value
97
     * @param null    $default
98
     */
99
    protected function mockingCookieFacade(Request $request, $cookieName, $value, $default = null)
100
    {
101
        $request->shouldReceive('cookie')
102
            ->times(1)
103
            ->withArgs([$cookieName, $default])
104
            ->andReturn($value);
105
        $app = new ApplicationStub();
106
        $app->setAttributes(['request' => $request]);
107
        Cookie::setFacadeApplication($app);
0 ignored issues
show
Documentation introduced by
$app is of type object<Tests\Stubs\ApplicationStub>, but the function expects a object<Illuminate\Contra...Foundation\Application>.

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
    }
109
}
110