Skip to content

Commit f582100

Browse files
committed
Code Coverage
refs #23
1 parent a78b236 commit f582100

File tree

2 files changed

+34
-39
lines changed

2 files changed

+34
-39
lines changed

src/Link.php

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public function __construct($object, FactoryManagerInterface $manager, AccessInt
4444
{
4545
if ( ! is_object($object) )
4646
{
47-
throw new ValidationException('Link has to be an object, "' . gettype($object) . '" given.');
47+
throw new ValidationException('Link has to be an object or string, "' . gettype($object) . '" given.');
4848
}
4949

5050
if ( ! array_key_exists('href', $object) )
5151
{
52-
throw new ValidationException('Link must habe a "href" attribute.');
52+
throw new ValidationException('Link must have a "href" attribute.');
5353
}
5454

5555
$this->parent = $parent;
@@ -58,14 +58,7 @@ public function __construct($object, FactoryManagerInterface $manager, AccessInt
5858

5959
$this->container = new DataContainer();
6060

61-
$object_vars = get_object_vars($object);
62-
63-
if ( count($object_vars) === 0 )
64-
{
65-
return $this;
66-
}
67-
68-
foreach ($object_vars as $name => $value)
61+
foreach (get_object_vars($object) as $name => $value)
6962
{
7063
$this->set($name, $value);
7164
}
@@ -101,7 +94,7 @@ public function get($key)
10194
*/
10295
protected function set($name, $link)
10396
{
104-
if ( $name === 'meta' and ! $this->parent instanceof ItemInterface )
97+
if ( $name === 'meta' )
10598
{
10699
$this->container->set($name, $this->manager->getFactory()->make(
107100
'Meta',
@@ -111,29 +104,13 @@ protected function set($name, $link)
111104
return $this;
112105
}
113106

114-
// from spec: an object ("link object") which can contain the following members:
115-
// - href: a string containing the link's URL.
116-
if ( $name === 'href' or ! is_object($link) )
107+
// every link must be an URL
108+
if ( ! is_string($link) )
117109
{
118-
if ( ! is_string($link) )
119-
{
120-
throw new ValidationException('Link has to be an object or string, "' . gettype($link) . '" given.');
121-
}
122-
123-
$this->container->set($name, strval($link));
124-
125-
return $this;
110+
throw new ValidationException('Every link attribute has to be a string, "' . gettype($link) . '" given.');
126111
}
127112

128-
// Now $link can only be an object
129-
// Create Link object if needed
130-
if ( ! ($link instanceof LinkInterface) )
131-
{
132-
$this->container->set($name, $this->manager->getFactory()->make(
133-
'Link',
134-
[$link, $this->manager, $this]
135-
));
136-
}
113+
$this->container->set($name, strval($link));
137114

138115
return $this;
139116
}

tests/unit/LinkTest.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,42 @@ public function testHrefHasToBeAString($input)
8787
return;
8888
}
8989

90-
$this->setExpectedException('Art4\JsonApiClient\Exception\ValidationException');
90+
$this->setExpectedException(
91+
'Art4\JsonApiClient\Exception\ValidationException',
92+
'Every link attribute has to be a string, "' . gettype($input) . '" given.'
93+
);
94+
95+
$link = new Link($object, $this->manager, $this->parent_link);
96+
}
97+
98+
/**
99+
* @test href attribute must be set
100+
*
101+
* - an object ("link object") which can contain the following members:
102+
* - href: a string containing the link's URL.
103+
*/
104+
public function testHrefAttributeMustBeSet()
105+
{
106+
$object = new \stdClass();
107+
$object->related = 'http://example.org/related';
108+
109+
$this->setExpectedException(
110+
'Art4\JsonApiClient\Exception\ValidationException',
111+
'Link must have a "href" attribute.'
112+
);
91113

92114
$link = new Link($object, $this->manager, $this->parent_link);
93115
}
94116

95117
/**
96118
* @test meta attribute will be parsed as Meta object inside Link
97119
*/
98-
public function testMetaIsParsedAsMetaInsideItem()
120+
public function testMetaIsParsedAsObject()
99121
{
100122
$object = new \stdClass();
101123
$object->meta = new \stdClass();
102124
$object->href = 'http://example.org/href';
103125

104-
// Mock parent link
105-
$this->parent_link = $this->getMockBuilder('Art4\JsonApiClient\LinkInterface')
106-
->getMock();
107-
108126
$link = new Link($object, $this->manager, $this->parent_link);
109127

110128
$this->assertTrue($link->has('meta'));
@@ -118,15 +136,15 @@ public function testMetaIsParsedAsMetaInsideItem()
118136
*/
119137
public function testCreateWithDataprovider($input)
120138
{
121-
// A link object could be empty
139+
// A link object must be an object
122140
if ( gettype($input) === 'object' )
123141
{
124142
return;
125143
}
126144

127145
$this->setExpectedException(
128146
'Art4\JsonApiClient\Exception\ValidationException',
129-
'Link has to be an object, "' . gettype($input) . '" given.'
147+
'Link has to be an object or string, "' . gettype($input) . '" given.'
130148
);
131149

132150
$link = new Link($input, $this->manager, $this->parent_link);

0 commit comments

Comments
 (0)