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.

LeadsTest::testSorting()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 2
eloc 14
nc 2
nop 1
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\Resource;
8
use prgTW\BaseCRM\Resource\ResourceCollection;
9
use prgTW\BaseCRM\Service\Enum\LeadsSortBy;
10
use prgTW\BaseCRM\Service\Lead;
11
use prgTW\BaseCRM\Tests\AbstractTest;
12
13
class LeadsTest extends AbstractTest
14
{
15
	public function testGet()
16
	{
17
		$client = \Mockery::mock(GuzzleClient::class);
18
		$client
19
			->shouldReceive('request')
20
			->once()
21
			->with('GET', sprintf('%s/%s/leads.json', Resource::ENDPOINT_LEADS, Resource::PREFIX), $this->getQuery([
22
				'query' => [
23
					'page' => 1
24
				]
25
			]))
26
			->andReturn($this->getResponse(200, '
27
				{
28
					"success": true,
29
					"metadata": {
30
						"count": 2
31
					},
32
					"items": [
33
						{
34
							"success": true,
35
							"lead": {
36
								"id": 1,
37
								"user_id": 2,
38
								"account_id": 3,
39
								"owner_id": 2,
40
								"first_name": "Lead",
41
								"last_name": "One",
42
								"company_name": null,
43
								"created_at": "2013-04-10T15:04:24+00:00",
44
								"state": null,
45
								"display_name": "Lead One",
46
								"conversion_name": "Lead One",
47
								"added_on": "2013-04-10T15:04:24+00:00"
48
							},
49
							"metadata": {
50
							}
51
						},
52
						{
53
							"success": true,
54
							"lead": {
55
								"id": 2,
56
								"user_id": 2,
57
								"account_id": 3,
58
								"owner_id": 2,
59
								"first_name": "Lead",
60
								"last_name": "Two",
61
								"company_name": null,
62
								"created_at": "2013-04-10T15:04:00+00:00",
63
								"state": null,
64
								"display_name": "Lead Two",
65
								"conversion_name": "Lead Two",
66
								"added_on": "2013-04-10T15:04:00+00:00"
67
							},
68
							"metadata": {
69
							}
70
						}
71
					]
72
				}
73
			'));
74
75
		$baseCrm = new BaseCrm('', $client);
76
		$leads   = $baseCrm->getLeads();
77
		$found   = 0;
78
		/** @var Lead $lead */
79
		foreach ($leads as $lead)
80
		{
81
			$this->assertInstanceOf(Lead::class, $lead);
82
			$this->assertEquals($found + 1, $lead->id);
83
			++$found;
84
		}
85
		$this->assertEquals(2, $found);
86
	}
87
88
	/**
89
	 * @param LeadsSortBy $sortBy
0 ignored issues
show
Documentation introduced by
Should the type for parameter $sortBy not be null|LeadsSortBy?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
90
	 *
91
	 * @dataProvider provideSortFields
92
	 */
93
	public function testSorting(LeadsSortBy $sortBy = null)
94
	{
95
		$query = $this->getQuery([
96
			'query' => [
97
				'page' => 1
98
			]
99
		]);
100
		if (null !== $sortBy)
101
		{
102
			$query['query']['sort_by'] = $sortBy->getValue();
103
		}
104
		$client = \Mockery::mock(GuzzleClient::class);
105
		$client
106
			->shouldReceive('request')
107
			->once()
108
			->with('GET', sprintf('%s/%s/leads.json', Resource::ENDPOINT_LEADS, Resource::PREFIX), $query)
109
			->andReturn($this->getResponse(200, '
110
				{
111
					"success": true,
112
					"metadata": {
113
						"count": 0
114
					},
115
					"items": [
116
					]
117
				}
118
			'));
119
120
		$baseCrm = new BaseCrm('', $client);
121
		$baseCrm->getLeads()->all(1, $sortBy);
122
	}
123
124
	/**
125
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<null|LeadsSortBy>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
126
	 */
127
	public function provideSortFields()
128
	{
129
		return [
130
			[null],
131
			[LeadsSortBy::ID_DESC()],
132
			[LeadsSortBy::ADDED_ON()],
133
			[LeadsSortBy::FIRST_NAME()],
134
			[LeadsSortBy::LAST_NAME()],
135
		];
136
	}
137
138
	public function testAutoPagination()
139
	{
140
		$client = \Mockery::mock(GuzzleClient::class);
141
		$client
142
			->shouldReceive('request')
143
			->twice()
144
			->with('GET', sprintf('%s/%s/leads.json', Resource::ENDPOINT_LEADS, Resource::PREFIX), \Mockery::any())
145
			->andReturn($this->getResponse(200, '
146
				{
147
					"success": true,
148
					"metadata": {
149
						"count": 1
150
					},
151
					"items": [
152
						{
153
							"success": true,
154
							"lead": {
155
								"id": 1
156
							},
157
							"metadata": {
158
							}
159
						}
160
					]
161
				}
162
			'));
163
		$client
164
			->shouldReceive('request')
165
			->once()
166
			->with('GET', sprintf('%s/%s/leads.json', Resource::ENDPOINT_LEADS, Resource::PREFIX), $this->getQuery([
167
				'query' => [
168
					'page' => 3
169
				]
170
			]))
171
			->andReturn($this->getResponse(200, '
172
				{
173
					"success": true,
174
					"metadata": {
175
						"count": 0
176
					},
177
					"items": [
178
					]
179
				}
180
			'));
181
182
		$baseCrm = new BaseCrm('', $client);
183
		$leads   = $baseCrm->getLeads();
184
		$leads->setAutoPagination(true);
185
		$found = 0;
186
		/** @var Lead $lead */
187
		foreach ($leads as $lead)
188
		{
189
			$this->assertInstanceOf(Lead::class, $lead);
190
			++$found;
191
		}
192
		$this->assertEquals(2, $found);
193
	}
194
195
	public function testTaggings()
196
	{
197
		$client = \Mockery::mock(GuzzleClient::class);
198
		$client
199
			->shouldReceive('request')
200
			->once()
201
			->with('GET', sprintf('%s/%s/leads/1.json', Resource::ENDPOINT_LEADS, Resource::PREFIX), $this->getQuery())
202
			->andReturn($this->getResponse(200, '
203
				{
204
					"success": true,
205
					"lead": {
206
						"id": 1,
207
						"user_id": 2,
208
						"account_id": 3,
209
						"owner_id": 2,
210
						"first_name": "Lead",
211
						"last_name": "One",
212
						"company_name": null,
213
						"created_at": "2013-04-10T15:04:24+00:00",
214
						"state": null,
215
						"display_name": "Lead One",
216
						"conversion_name": "Lead One",
217
						"added_on": "2013-04-10T15:04:24+00:00"
218
					},
219
					"metadata": {
220
					}
221
				}
222
			'));
223
		$baseCrm = new BaseCrm('', $client);
224
		/** @var Lead $lead */
225
		$lead = $baseCrm->getLeads()->get(1);
226
		$this->assertInstanceOf(Lead::class, $lead);
227
		$this->assertEquals(1, $lead->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<prgTW\BaseCRM\Service\Lead>. 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...
228
229
		$client
230
			->shouldReceive('request')
231
			->once()
232
			->with('POST', sprintf('%s/%s/taggings.json', Resource::ENDPOINT_TAGS, Resource::PREFIX), $this->getQuery([
233
				'query' => [
234
					'app_id'        => 5,
235
					'taggable_type' => 'Lead',
236
					'taggable_id'   => 1,
237
					'tag_list'      => 'tag1,tag2',
238
				],
239
			]))
240
			->andReturn($this->getResponse(200, '
241
				[
242
					{
243
						"tag": {
244
							"id": 1,
245
							"name": "tag1",
246
							"permissions_holder_id": 20
247
						}
248
					},
249
					{
250
						"tag": {
251
							"id": 2,
252
							"name": "tag2",
253
							"permissions_holder_id": 20
254
						}
255
					}
256
				]
257
			'));
258
259
		$tags = $lead->saveTags(['tag1', 'tag2']);
260
		$this->assertInstanceOf(ResourceCollection::class, $tags);
261
		$this->assertCount(2, $tags);
262
	}
263
}
264