GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SourcesTest::testCustomFields()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 82
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 82
rs 8.3768
cc 5
eloc 36
nc 5
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
namespace prgTW\BaseCRM\Tests\Service;
4
5
use prgTW\BaseCRM\BaseCrm;
6
use prgTW\BaseCRM\Client\GuzzleClient;
7
use prgTW\BaseCRM\Resource\CustomFieldsCollection;
8
use prgTW\BaseCRM\Resource\Resource;
9
use prgTW\BaseCRM\Service\Detached\Source as DetachedSource;
10
use prgTW\BaseCRM\Service\Source;
11
use prgTW\BaseCRM\Tests\AbstractTest;
12
13
class SourcesTest extends AbstractTest
14
{
15
	public function testAll()
16
	{
17
		$client = \Mockery::mock(GuzzleClient::class);
18
		$client
19
			->shouldReceive('request')
20
			->once()
21
			->with('GET', sprintf('%s/%s/sources.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery())
22
			->andReturn($this->getResponse(200, '
23
				[
24
					{
25
					"source": {
26
						"name": "test",
27
						"id": 1
28
					}
29
				},
30
				{
31
					"source": {
32
						"name": "test",
33
						"id": 2
34
					}
35
				},
36
				{
37
					"source": {
38
						"name": "test",
39
						"id": 3
40
					}
41
				}
42
				]
43
			'));
44
		$baseCrm = new BaseCrm('', $client);
45
		$sources = $baseCrm->getSources();
46
		$this->assertCount(3, $sources);
47
48
		$i = 1;
49
		/** @var Source $source */
50
		foreach ($sources as $source)
51
		{
52
			$this->assertInstanceOf(Source::class, $source);
53
			$this->assertEquals($i, $source->id);
54
			$this->assertEquals('test', $source->name);
55
56
			$client
57
				->shouldReceive('request')
58
				->once()
59
				->with('PUT', sprintf('%s/%s/sources/%d.json', Resource::ENDPOINT_SALES, Resource::PREFIX, $source->id), $this->getQuery([
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
60
					'query' => [
61
						'source' => [
62
							'name' => 'test',
63
						],
64
					],
65
				]))
66
				->andReturn($this->getResponse(200, '
67
					{
68
						"source": {
69
							"name": "test",
70
							"id": ' . $i . '
71
						}
72
					}
73
				'));
74
			$source->save([
75
				'name',
76
			]);
77
			++$i;
78
		}
79
	}
80
81
	public function testAllWithOther()
82
	{
83
		$client = \Mockery::mock(GuzzleClient::class);
84
		$client
85
			->shouldReceive('request')
86
			->once()
87
			->with('GET', sprintf('%s/%s/sources.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery([
88
				'query' => [
89
					'other' => 1,
90
				]
91
			]))
92
			->andReturn($this->getResponse(200, '
93
				[
94
					{
95
					"source": {
96
						"name": "test",
97
						"id": 1
98
					}
99
				},
100
				{
101
					"source": {
102
						"name": "test",
103
						"id": 2
104
					}
105
				},
106
				{
107
					"source": {
108
						"name": "test",
109
						"id": 3
110
					}
111
				}
112
				]
113
			'));
114
		$baseCrm = new BaseCrm('', $client);
115
		$baseCrm->getSources()->all(true);
116
	}
117
118
	public function testGet()
119
	{
120
		$client = \Mockery::mock(GuzzleClient::class);
121
		$client
122
			->shouldReceive('request')
123
			->once()
124
			->with('GET', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery())
125
			->andReturn($this->getResponse(200, '
126
				{
127
					"source": {
128
						"name": "test",
129
						"id": 123
130
					}
131
				}
132
			'));
133
		$baseCrm = new BaseCrm('', $client);
134
		$sources = $baseCrm->getSources();
135
		/** @var Source $source */
136
		$source = $sources->get(123);
137
138
		$this->assertInstanceOf(Source::class, $source);
139
		$this->assertEquals(123, $source->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<prgTW\BaseCRM\Service\Source>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
140
		$this->assertEquals('test', $source->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<prgTW\BaseCRM\Service\Source>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
141
142
		$client
143
			->shouldReceive('request')
144
			->once()
145
			->with('PUT', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery([
146
				'query' => [
147
					'source' => [
148
						'name' => 'modified',
149
					]
150
				],
151
			]))
152
			->andReturn($this->getResponse(200, '
153
				{
154
					"source": {
155
						"name": "modified",
156
						"id": 123
157
					}
158
				}
159
			'));
160
161
		$source->name = 'modified';
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<prgTW\BaseCRM\Service\Source>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
162
		$source->save([
163
			'name',
164
		]);
165
		$this->assertEquals('modified', $source->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<prgTW\BaseCRM\Service\Source>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
166
	}
167
168
	public function testCreate()
169
	{
170
		$client = \Mockery::mock(GuzzleClient::class);
171
		$client
172
			->shouldReceive('request')
173
			->once()
174
			->with('POST', sprintf('%s/%s/sources.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery([
175
				'query' => [
176
					'source' => [
177
						'name'                => 'test',
178
						'custom_field_values' => [],
179
					],
180
				],
181
			]))
182
			->andReturn($this->getResponse(200, '
183
				{
184
					"source": {
185
						"name": "test",
186
						"id": 405
187
					}
188
				}
189
			'));
190
		$baseCrm = new BaseCrm('', $client);
191
		$sources = $baseCrm->getSources();
192
		/** @var Source $source */
193
		$newSource       = (new DetachedSource);
194
		$newSource->name = 'test';
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<prgTW\BaseCRM\Service\Detached\Source>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
195
		$source          = $sources->create($newSource, [
196
			'name',
197
		]);
198
		$this->assertInstanceOf(Source::class, $source);
199
		$this->assertEquals(405, $source->id);
200
		$this->assertEquals('test', $source->name);
201
	}
202
203
	public function testDelete()
204
	{
205
		$client = \Mockery::mock(GuzzleClient::class);
206
		$client
207
			->shouldReceive('request')
208
			->once()
209
			->with('DELETE', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery())
210
			->andReturn($this->getResponse(200, ''));
211
		$baseCrm = new BaseCrm('', $client);
212
		$sources = $baseCrm->getSources();
213
		$result  = $sources->delete(123);
214
		$this->assertTrue($result);
215
	}
216
217
	public function testCustomFields()
218
	{
219
		$client = \Mockery::mock(GuzzleClient::class);
220
		$client
221
			->shouldReceive('request')
222
			->once()
223
			->with('GET', sprintf('%s/%s/sources.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery())
224
			->andReturn($this->getResponse(200, '
225
				[
226
					{
227
					"source": {
228
						"name": "test",
229
						"id": 1,
230
						"custom_fields": []
231
					}
232
				},
233
				{
234
					"source": {
235
						"name": "test",
236
						"id": 2,
237
						"custom_fields": {
238
							"custom1": {
239
								"id": null
240
							}
241
						}
242
					}
243
				},
244
				{
245
					"source": {
246
						"name": "test",
247
						"id": 3,
248
						"custom_fields": {
249
							"custom1": {
250
								"id": null
251
							},
252
							"custom2": {
253
								"id": 12345,
254
								"value": "some value"
255
							}
256
						}
257
					}
258
				}
259
				]
260
			'));
261
		$baseCrm = new BaseCrm('', $client);
262
		$sources = $baseCrm->getSources()->all();
263
264
		$i = 1;
265
		/** @var Source $source */
266
		foreach ($sources as $source)
267
		{
268
			$customFieldsCollection = $source->getCustomFields();
0 ignored issues
show
Bug introduced by
The method getCustomFields cannot be called on $source (of type resource|false).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
269
			$this->assertInstanceOf(CustomFieldsCollection::class, $customFieldsCollection);
270
			switch ($i)
271
			{
272
				case 1:
273
					$this->assertCount(0, $customFieldsCollection);
274
					$this->assertArrayNotHasKey('custom1', $customFieldsCollection);
275
					$this->assertEquals([], $customFieldsCollection->toArray());
276
					break;
277
278
				case 2:
279
					$this->assertCount(1, $customFieldsCollection);
280
					$this->assertArrayHasKey('custom1', $customFieldsCollection);
281
					$this->assertNull($customFieldsCollection['custom1']->getId());
282
					$this->assertArrayNotHasKey('custom2', $customFieldsCollection);
283
					$this->assertEquals(['custom1' => null], $customFieldsCollection->toArray());
284
					break;
285
286
				case 3:
287
					$this->assertCount(2, $customFieldsCollection);
288
					$this->assertArrayHasKey('custom1', $customFieldsCollection);
289
					$this->assertArrayHasKey('custom2', $customFieldsCollection);
290
					$this->assertEquals('custom2', $customFieldsCollection['custom2']->getName());
291
					$this->assertEquals('12345', $customFieldsCollection['custom2']->getId());
292
					$this->assertEquals('some value', $customFieldsCollection['custom2']->getValue());
293
					$this->assertEquals(['custom1' => null, 'custom2' => 'some value'], $customFieldsCollection->toArray());
294
					break;
295
			}
296
			++$i;
297
		}
298
	}
299
300
	public function testLazyLoadingWithCustomFields()
301
	{
302
		$client  = \Mockery::mock(GuzzleClient::class);
303
		$baseCrm = new BaseCrm('', $client);
304
		/** @var Source $source */
305
		$source = $baseCrm->getSources()->get(123);
306
307
		$client
308
			->shouldReceive('request')
309
			->once()
310
			->with('GET', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery())
311
			->andReturn($this->getResponse(200, '
312
				{
313
					"source": {
314
						"name": "test",
315
						"id": 123,
316
						"custom_fields": {
317
							"custom1": {
318
								"id": null
319
							},
320
							"custom2": {
321
								"id": 12345,
322
								"value": "some value"
323
							}
324
						}
325
					}
326
				}
327
			'));
328
329
		$this->assertTrue($source->hasCustomField('custom1'));
330
		$this->assertTrue($source->hasCustomField('custom2'));
331
	}
332
333
	public function testSavingCustomFields()
334
	{
335
		$client  = \Mockery::mock(GuzzleClient::class);
336
		$baseCrm = new BaseCrm('', $client);
337
		/** @var Source $source */
338
		$source = $baseCrm->getSources()->get(123);
339
340
		$client
341
			->shouldReceive('request')
342
			->once()
343
			->with('GET', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery())
344
			->andReturn($this->getResponse(200, '
345
				{
346
					"source": {
347
						"name": "test",
348
						"id": 123,
349
						"custom_fields": {
350
							"custom1": {
351
								"id": null
352
							},
353
							"custom2": {
354
								"id": 12345,
355
								"value": "some value"
356
							}
357
						}
358
					}
359
				}
360
			'));
361
362
		$source->setCustomField('custom1', 'new_value1');
363
		$source->setCustomField('custom2', 'new_value2');
364
365
		$client
366
			->shouldReceive('request')
367
			->once()
368
			->with('PUT', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery([
369
				'query' => [
370
					'source' => [
371
						'name'          => 'test',
372
						'custom_field_values' => [
373
							'custom1' => 'new_value1',
374
							'custom2' => 'new_value2',
375
						],
376
					],
377
				],
378
			]))
379
			->andReturn($this->getResponse(200, '
380
				{
381
					"source": {
382
						"name": "test",
383
						"id": 123,
384
						"custom_fields": {
385
							"custom1": {
386
								"id": 123,
387
								"value": "new_value1"
388
							},
389
							"custom2": {
390
								"id": 456,
391
								"value": "new_value2"
392
							}
393
						}
394
					}
395
				}
396
			'));
397
398
		$source->save();
399
	}
400
}
401