Completed
Push — master ( b0fced...cc1e9e )
by AJ
06:36
created

ShopifyAPIComponentTest::testShopDomain()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 76
rs 8.9667
c 4
b 0
f 0
cc 1
eloc 42
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * CakePHPify : CakePHP Plugin for Shopify API Authentication
4
 * Copyright (c) Multidimension.al (http://multidimension.al)
5
 * Github : https://github.com/multidimension-al/cakephpify
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE file
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright (c) Multidimension.al (http://multidimension.al)
12
 * @link      https://github.com/multidimension-al/cakephpify CakePHPify Github
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
16
namespace Multidimensional\Cakephpify\Test\TestCase\Controller\Component;
17
18
use Cake\Controller\ComponentRegistry;
19
use Cake\Controller\Controller;
20
use Cake\Core\Configure;
21
use Cake\Event\Event;
22
use Cake\Network\Request;
23
use Cake\Network\Response;
24
use Cake\TestSuite\TestCase;
25
use Multidimensional\Cakephpify\Controller\Component\ShopifyAPIComponent;
26
use Multidimensional\Cakephpify\Test\Fixture\AccessTokensFixture;
27
use Multidimensional\Cakephpify\Test\Fixture\ShopsFixture;
28
29
class ShopifyAPIComponentTest extends TestCase
30
{
31
32
    public $component = null;
33
    public $controller = null;
34
    public $fixtures = ['plugin.Multidimensional/Cakephpify.Shops', 'plugin.Multidimensional/Cakephpify.AccessTokens'];
35
36
    public function setUp()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
37
    {
38
        parent::setUp();
39
        
40
        Configure::write('Multidimensional/Cakephpify', [
41
                'abc123' =>
42
                    [
43
                    'sharedSecret' => 'abc123',
44
                    'scope' => '',
45
                    'privateApp' => false,
46
                    'privateAppPassword' => NULL]
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
47
                    ]);
48
        
49
        $request = new Request();
50
        $response = new Response();
51
        $this->controller = $this->getMockBuilder('Cake\Controller\Controller')
52
            ->setConstructorArgs([$request, $response])
53
            ->setMethods(null)
54
            ->getMock();
55
        $registry = new ComponentRegistry($this->controller);
56
        $this->component = new ShopifyAPIComponent($registry, ['apiKey' => 'abc123']);
57
        $event = new Event('Controller.startup', $this->controller);
58
        $this->component->startup($event);
59
    }
60
61
    public function tearDown()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
62
    {
63
        parent::tearDown();
64
        unset($this->component, $this->controller);
65
    }
66
67
    public function testShopDomain()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
68
    {
69
        $shopDomain = 'test.myshopify.com';
70
        $return = $this->component->setShopDomain($shopDomain);
71
        $this->assertSame($return, $shopDomain);
72
        $return = $this->component->getShopDomain();
73
        $this->assertSame($return, $shopDomain);
74
        
75
        $shopDomain = 'random.myshopify.com';
76
        $this->component->setShopDomain($shopDomain);
77
        $return = $this->component->setShopDomain($shopDomain);
78
        $this->assertSame($return, $shopDomain);
79
        $return = $this->component->getShopDomain();
80
        $this->assertSame($return, $shopDomain);
81
        
82
        $shopDomain = null;
83
		$this->assertNull($shopDomain);
84
        $return = $this->component->setShopDomain($shopDomain);
85
        $this->assertNull($return);
86
        $return = $this->component->getShopDomain();
87
        $this->assertNull($return);
88
        
89
        $shopDomain = false;
90
		$this->assertFalse($shopDomain);
91
        $return = $this->component->setShopDomain($shopDomain);
0 ignored issues
show
Documentation introduced by
$shopDomain is of type boolean, but the function expects a string.

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...
92
        $this->assertFalse($return);
93
        $return = $this->component->getShopDomain();
94
        $this->assertFalse($return);
95
        
96
        $shopDomain = true;
97
		$this->assertTrue($shopDomain);
98
        $return = $this->component->setShopDomain($shopDomain);
0 ignored issues
show
Documentation introduced by
$shopDomain is of type boolean, but the function expects a string.

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...
99
        $this->assertTrue($return);
100
        $return = $this->component->getShopDomain();
101
        $this->assertTrue($return);
102
        
103
        $shopDomain = 'test.myshopify.com';
104
        $return = $this->component->validDomain($shopDomain);
105
        $this->assertTrue($return);
106
        
107
        $shopDomain = 'TEST.MYshopify.COM';
108
        $return = $this->component->validDomain($shopDomain);
109
        $this->assertTrue($return);
110
        
111
        $shopDomain = 'random.myshopify.com';
112
        $return = $this->component->validDomain($shopDomain);
113
        $this->assertTrue($return);
114
        
115
        $shopDomain = 'www.myshopify.com';
116
        $return = $this->component->validDomain($shopDomain);
117
        $this->assertTrue($return);
118
        
119
        /*$shopDomain = 'test.myshopify.net';
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
        $return = $this->component->validDomain($shopDomain);
121
        $this->assertFalse($return);
122
        
123
        $shopDomain = 'http://test.myshopify.com/';
124
        $return = $this->component->validDomain($shopDomain);
125
        $this->assertFalse($return);
126
        
127
        $shopDomain = 'google.com';
128
        $return = $this->component->validDomain($shopDomain);
129
        $this->assertFalse($return);
130
        
131
        $shopDomain = null;
132
        $return = $this->component->validDomain($shopDomain);
133
        $this->assertFalse($return);
134
        
135
        $shopDomain = false;
136
        $return = $this->component->validDomain($shopDomain);
137
        $this->assertFalse($return);
138
        
139
        $shopDomain = true;
140
        $return = $this->component->validDomain($shopDomain);
141
        $this->assertFalse($return);*/
142
    }
143
144
    public function testSetAccessToken()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
145
    {
146
        $this->markTestIncomplete('Not implemented yet.');
147
    }
148
149
    public function testGetAuthorizeUrl()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
150
    {
151
        $this->markTestIncomplete('Not implemented yet.');
152
    }
153
154
    public function testGetAccessToken()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
155
    {
156
        $this->markTestIncomplete('Not implemented yet.');
157
    }
158
159
    public function testNonce()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
160
    {
161
        $shopDomain = "test.myshopify.com"; //339fdccae930940993141bde32be560f
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal test.myshopify.com does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
162
        $return = $this->component->setNonce($shopDomain);
163
        $this->assertSame($return, '339fdccae930940993141bde32be560f');
164
        $return = $this->component->getNonce();
165
        $this->assertSame($return, '339fdccae930940993141bde32be560f');
166
        
167
        $shopDomain = "TeSt.MySHoPiFy.CoM"; //339fdccae930940993141bde32be560f
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal TeSt.MySHoPiFy.CoM does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
168
        $return = $this->component->setNonce($shopDomain);
169
        $this->assertSame($return, '339fdccae930940993141bde32be560f');
170
        $return = $this->component->getNonce();
171
        $this->assertSame($return, '339fdccae930940993141bde32be560f');
172
        
173
		$shopDomain = "example.com"; //5ababd603b22780302dd8d83498e5172
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal example.com does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
174
        $return = $this->component->setNonce($shopDomain);
175
        $this->assertSame($return, '5ababd603b22780302dd8d83498e5172');
176
        $return = $this->component->getNonce();
177
        $this->assertSame($return, '5ababd603b22780302dd8d83498e5172');
178
		
179
        $shopDomain = null; //d41d8cd98f00b204e9800998ecf8427e
180
		$this->assertNull($shopDomain);
181
        $return = $this->component->setNonce($shopDomain);
182
        $this->assertSame($return, 'd41d8cd98f00b204e9800998ecf8427e');
183
        $return = $this->component->getNonce();
184
        $this->assertSame($return, 'd41d8cd98f00b204e9800998ecf8427e');
185
        
186
        $shopDomain = false; //d41d8cd98f00b204e9800998ecf8427e
187
		$this->assertFalse($shopDomain);
188
        $return = $this->component->setNonce($shopDomain);
0 ignored issues
show
Documentation introduced by
$shopDomain is of type boolean, but the function expects a string.

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...
189
        $this->assertSame($return, 'd41d8cd98f00b204e9800998ecf8427e');
190
        $return = $this->component->getNonce();
191
        $this->assertSame($return, 'd41d8cd98f00b204e9800998ecf8427e');
192
        
193
        $shopDomain = true; //c4ca4238a0b923820dcc509a6f75849b
194
		$this->assertTrue($shopDomain);
195
        $return = $this->component->setNonce($nonce);
0 ignored issues
show
Bug introduced by
The variable $nonce does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
196
        $this->assertSame($return, 'c4ca4238a0b923820dcc509a6f75849b');
197
        $return = $this->component->getNonce();
198
        $this->assertSame($return, 'c4ca4238a0b923820dcc509a6f75849b');
199
    }
200
	
201
	public function testValidateHMAC()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
202
	{
203
		$return = $this->component->validateHMAC(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
204
		$this->assertFalse($return);
205
		
206
		$return = $this->component->validateHMAC([]);
207
		$this->assertFalse($return);
208
		
209
		$return = $this->component->validateHMAC(['hmac' => null]);
210
		$this->assertFalse($return);
211
		
212
		$return = $this->component->validateHMAC(['hmac' => 'string']);
213
		$this->assertFalse($return);
214
	}
215
}
216