Skip to content
This repository was archived by the owner on May 1, 2019. It is now read-only.

Commit 39c8611

Browse files
committed
Merge pull request #474 from localheinz/fix/json-decode
Fix: Do not json_decode API response to associative array
2 parents 94e4341 + d1eb136 commit 39c8611

File tree

4 files changed

+71
-29
lines changed

4 files changed

+71
-29
lines changed

module/Application/src/Application/Service/RepositoryRetriever.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getContributors($owner, $repo)
6464
{
6565
try {
6666
$contributors = $this->githubClient->api('repos')->contributors($owner, $repo);
67-
$data = json_decode($contributors, true);
67+
$data = json_decode($contributors);
6868

6969
return array_reverse($data);
7070
} catch (RuntimeException $e) {

module/Application/test/ApplicationTest/Integration/Controller/ContributorsControllerTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
use Application\Entity;
77
use Application\Service;
88
use ApplicationTest\Integration\Util\Bootstrap;
9+
use ApplicationTest\Util\FakerTrait;
910
use stdClass;
1011
use Zend\Http;
1112
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
1213

1314
class ContributorsControllerTest extends AbstractHttpControllerTestCase
1415
{
16+
use FakerTrait;
17+
1518
protected function setUp()
1619
{
1720
parent::setUp();
@@ -46,14 +49,16 @@ public function testContributorsActionCanBeAccessed()
4649
->getMock()
4750
;
4851

52+
$contributors = $this->contributors(5);
53+
4954
$repositoryRetriever
5055
->expects($this->once())
5156
->method('getContributors')
5257
->with(
5358
$this->equalTo($vendor),
5459
$this->equalTo($name)
5560
)
56-
->willReturn([])
61+
->willReturn($contributors)
5762
;
5863

5964
$metaData = new stdClass();
@@ -89,4 +94,39 @@ public function testContributorsActionCanBeAccessed()
8994
$this->assertActionName('index');
9095
$this->assertResponseStatusCode(Http\Response::STATUS_CODE_200);
9196
}
97+
98+
/**
99+
* @link https://developer.github.com/v3/repos/statistics/#response
100+
*
101+
* @return stdClass
102+
*/
103+
private function contributor()
104+
{
105+
$contributor = new stdClass();
106+
107+
$contributor->total = $this->faker()->randomNumber();
108+
109+
$contributor->author = new stdClass();
110+
111+
$contributor->author->login = $this->faker()->unique()->userName;
112+
$contributor->author->avatar_url = $this->faker()->unique()->url;
113+
$contributor->author->html_url = $this->faker()->unique()->url;
114+
115+
return $contributor;
116+
}
117+
118+
/**
119+
* @param int $count
120+
* @return stdClass[]
121+
*/
122+
private function contributors($count)
123+
{
124+
$contributors = [];
125+
126+
for ($i = 0; $i < $count; $i++) {
127+
array_push($contributors, $this->contributor());
128+
}
129+
130+
return $contributors;
131+
}
92132
}

module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,16 @@ public function testGetContributorsDecodesResponseToArray()
362362
$this->assertInternalType('array', $contributors);
363363

364364
array_walk($contributors, function ($contributor) {
365-
$this->assertInternalType('array', $contributor);
366-
$this->assertArrayHasKey('author', $contributor);
365+
$this->assertInstanceOf(stdClass::class, $contributor);
367366

368-
$author = $contributor['author'];
367+
$this->assertObjectHasAttribute('total', $contributor);
369368

370-
$this->assertInternalType('array', $author);
371-
$this->assertArrayHasKey('login', $author);
372-
$this->assertArrayHasKey('avatar_url', $author);
373-
$this->assertArrayHasKey('html_url', $author);
369+
$this->assertObjectHasAttribute('author', $contributor);
370+
$this->assertInstanceOf(stdClass::class, $contributor->author);
371+
372+
$this->assertObjectHasAttribute('login', $contributor->author);
373+
$this->assertObjectHasAttribute('avatar_url', $contributor->author);
374+
$this->assertObjectHasAttribute('html_url', $contributor->author);
374375
});
375376
}
376377

@@ -424,21 +425,22 @@ public function testGetContributorsReturnsContributorsInReverseOrder()
424425

425426
$expected = array_pop($contributorsAsReturned);
426427

427-
$this->assertInternalType('array', $contributor);
428-
$this->assertArrayHasKey('author', $contributor);
428+
$this->assertInstanceOf(stdClass::class, $contributor);
429429

430-
$author = $contributor['author'];
430+
$this->assertObjectHasAttribute('total', $contributor);
431+
$this->assertSame($expected->total, $contributor->total);
431432

432-
$this->assertInternalType('array', $author);
433+
$this->assertObjectHasAttribute('author', $contributor);
434+
$this->assertInstanceOf(stdClass::class, $contributor->author);
433435

434-
$this->assertArrayHasKey('login', $author);
435-
$this->assertSame($expected->author->login, $author['login']);
436+
$this->assertObjectHasAttribute('login', $contributor->author);
437+
$this->assertSame($expected->author->login, $contributor->author->login);
436438

437-
$this->assertArrayHasKey('avatar_url', $author);
438-
$this->assertSame($expected->author->avatar_url, $author['avatar_url']);
439+
$this->assertObjectHasAttribute('avatar_url', $contributor->author);
440+
$this->assertSame($expected->author->avatar_url, $contributor->author->avatar_url);
439441

440-
$this->assertArrayHasKey('html_url', $author);
441-
$this->assertSame($expected->author->html_url, $author['html_url']);
442+
$this->assertObjectHasAttribute('html_url', $contributor->author);
443+
$this->assertSame($expected->author->html_url, $contributor->author->html_url);
442444
});
443445
}
444446

@@ -487,15 +489,15 @@ public function testGetContributorsReturnsFalseIfRuntimeExceptionIsThrown()
487489
*/
488490
private function contributor()
489491
{
490-
$author = new stdClass();
492+
$contributor = new stdClass();
491493

492-
$author->login = $this->faker()->unique()->userName;
493-
$author->avatar_url = $this->faker()->unique()->url;
494-
$author->html_url = $this->faker()->unique()->url;
494+
$contributor->total = $this->faker()->randomNumber();
495495

496-
$contributor = new stdClass();
496+
$contributor->author = new stdClass();
497497

498-
$contributor->author = $author;
498+
$contributor->author->login = $this->faker()->unique()->userName;
499+
$contributor->author->avatar_url = $this->faker()->unique()->url;
500+
$contributor->author->html_url = $this->faker()->unique()->url;
499501

500502
return $contributor;
501503
}

module/Application/view/application/contributors/index.phtml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
</div>
1515
<?php foreach ($this->contributors as $contributor): ?>
1616
<div class="col-lg-1 col-md-2 col-sm-2 col-xs-3">
17-
<?php $url = $contributor['author']['html_url']; ?>
17+
<?php $url = $contributor->author->html_url; ?>
1818
<a href="<?php echo $this->escapeHtmlAttr($url); ?>" class="thumbnail img-responsive" target="_blank">
19-
<?php $src = $contributor['author']['avatar_url']; ?>
20-
<?php $alt = $contributor['author']['login']; ?>
21-
<?php $title = sprintf('%s with %d contributions', $alt, $contributor['total']); ?>
19+
<?php $src = $contributor->author->avatar_url; ?>
20+
<?php $alt = $contributor->author->login; ?>
21+
<?php $title = sprintf('%s with %d contributions', $alt, $contributor->total); ?>
2222
<img src="<?php echo $this->escapeHtmlAttr($src); ?>" alt="<?php echo $this->escapeHtmlAttr($alt); ?>" title="<?php echo $this->escapeHtmlAttr($title); ?>">
2323
</a>
2424
</div>

0 commit comments

Comments
 (0)