Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/Http/GraphResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,19 @@ public function getResponseAsObject($returnType)

//If more than one object is returned
if (array_key_exists('value', $result)) {
$objArray = array();
$values = $result['value'];

//Check that this is an object array instead of a value called "value"
if ($values && is_array($values)) {
if (is_array($values)) {
$objArray = array();
foreach ($values as $obj) {
$objArray[] = new $class($obj);
}
} else {
return new $class($result);
return $objArray;
}
return $objArray;
} else {
return new $class($result);
}

return new $class($result);
}

/**
Expand Down Expand Up @@ -188,4 +186,4 @@ public function getDeltaLink()
}
return null;
}
}
}
18 changes: 17 additions & 1 deletion tests/Http/GraphResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public function setUp()
$body = json_encode($this->responseBody);
$multiBody = json_encode(array('value' => array('1' => array('givenName' => 'Bob'), '2' => array('givenName' => 'Drew'))));
$valueBody = json_encode(array('value' => 'Bob Barker'));
$emptyMultiBody = json_encode(array('value' => array()));

$mock = new GuzzleHttp\Handler\MockHandler([
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body),
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $body),
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $multiBody),
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $valueBody),
new GuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $emptyMultiBody),
]);
$handler = GuzzleHttp\HandlerStack::create($mock);
$this->client = new GuzzleHttp\Client(['handler' => $handler]);
Expand Down Expand Up @@ -120,6 +122,9 @@ public function testGetMultipleObjects()
$this->request->execute($this->client);
$hosts = $this->request->setReturnType(Model\User::class)->execute($this->client);

$this->assertInternalType('array', $hosts);
$this->assertContainsOnlyInstancesOf(Model\User::class, $hosts);
$this->assertSame(array_values($hosts), $hosts);
$this->assertEquals(2, count($hosts));
$this->assertEquals("Bob", $hosts[0]->getGivenName());
}
Expand All @@ -133,4 +138,15 @@ public function testGetValueObject()

$this->assertInstanceOf(Model\User::class, $response);
}
}

public function testGetZeroMultipleObjects()
{
$this->request->execute($this->client);
$this->request->execute($this->client);
$this->request->execute($this->client);
$this->request->execute($this->client);
$response = $this->request->setReturnType(Model\User::class)->execute($this->client);

$this->assertSame(array(), $response);
}
}