Skip to content

Commit

Permalink
Some fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mekras committed Jul 12, 2016
1 parent 95d6c47 commit 65604c6
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require": {
"php": ">=5.6",
"mekras/interfaces": "^3.0",
"mekras/atompub": "~0.1.1",
"mekras/atompub": "~0.2.0",
"php-http/client-implementation": "^1.0"
},
"require-dev": {
Expand Down
27 changes: 19 additions & 8 deletions src/EDM/Primitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(Node $parent, $element, $type = null)
$this->nodeName = (string) $element;
parent::__construct($parent);
if ($type) {
$this->getDomElement()->setAttributeNS(OData::META, 'type', $type);
$this->setAttribute('m:type', $type);
}
}
}
Expand All @@ -75,11 +75,16 @@ public function __construct(Node $parent, $element, $type = null)
*/
public function __toString()
{
if (!is_scalar($this->getValue())) {
return '<Can not convert ' . gettype($this->getValue()) . ' to string>';
try {
$value = $this->getValue();
} catch (\InvalidArgumentException $e) {
return '(' . $e->getMessage() . ')';
}
if (!is_scalar($value)) {
return '(Can not convert ' . gettype($value) . ' to string)';
}

return (string) $this->getValue();
return (string) $value;
}

/**
Expand All @@ -99,14 +104,16 @@ public function getName()
*
* @return string
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function getType()
{
return $this->getCachedProperty(
'type',
function () {
return $this->getDomElement()->getAttributeNS(OData::META, 'type');
return $this->getAttribute('m:type');
}
);
}
Expand All @@ -116,11 +123,13 @@ function () {
*
* @param string $type
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function setType($type)
{
$this->getDomElement()->setAttributeNS(OData::META, 'type', $type);
$this->setAttribute('m:type', $type);
$this->setCachedProperty('type', $type);
}

Expand All @@ -129,14 +138,16 @@ public function setType($type)
*
* @return mixed
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function getValue()
{
return $this->getCachedProperty(
'value',
function () {
if ($this->getDomElement()->getAttributeNS(OData::META, 'null') === 'true') {
if ($this->getAttribute('m:null') === 'true') {
return null;
}
$value = trim($this->getDomElement()->textContent);
Expand Down Expand Up @@ -195,7 +206,7 @@ public function setValue($value)
{
$element = $this->getDomElement();
if (null === $value) {
$element->setAttributeNS(OData::META, 'null', 'true');
$this->setAttribute('m:null', 'true');
$element->nodeValue = '';
}
switch ($this->getType()) {
Expand Down
18 changes: 6 additions & 12 deletions src/Element/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
use Mekras\OData\Client\EDM\Primitive;
use Mekras\OData\Client\Exception\LogicException;
use Mekras\OData\Client\OData;
use Mekras\OData\Client\URI\Uri;

/*$elements = $this->query('atom:link[starts-with(@type, "application/atom+xml;")]');
foreach ($elements as $element) {
$link = new Link($this->getExtensions(), $element);
$result[$link->getTitle()] = $link;
}*/

/**
* OData Entry.
Expand Down Expand Up @@ -153,7 +146,7 @@ public function getRelations()
* Add relation to some resource.
*
* @param Entry|string $resource Entry or resource IRI.
* @param null $type Resource type if $resource is not an Entry.
* @param string|null $type Can be omitted if $resource is an instance of Entry.
*
* @return Link
*
Expand All @@ -168,19 +161,20 @@ public function addRelation($resource, $type = null)
$link = $this->getExtensions()->createElement($this, 'atom:link');
$link->setType('application/atom+xml;type=entry');
if ($resource instanceof Entry) {
$link->setRelation(OData::RELATED . '/' . $resource->getEntityType());
$link->setTitle($resource->getEntityType());
if (null === $type) {
$type = $resource->getEntityType();
}
$link->setUri($resource->getLink('self'));
} else {
if (null === $type) {
throw new \InvalidArgumentException(
'$type should be specified if $resource not an Entry'
);
}
$link->setRelation(OData::RELATED . '/' . $type);
$link->setTitle($type);
$link->setUri((string) $resource);
}
$link->setRelation(OData::RELATED . '/' . $type);
$link->setTitle($type);

return $link;
}
Expand Down
9 changes: 4 additions & 5 deletions src/Element/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
*/
namespace Mekras\OData\Client\Element;

use Mekras\Atom\Atom;
use Mekras\Atom\Node;
use Mekras\Atom\Element\Content;
use Mekras\OData\Client\EDM\Primitive;
use Mekras\OData\Client\Exception\LogicException;
use Mekras\OData\Client\OData;
Expand All @@ -30,19 +29,19 @@ class Properties extends Element implements \Iterator
/**
* Create node.
*
* @param Node $parent Parent node.
* @param Content $parent Parent node.
* @param \DOMElement|null $element DOM element.
*
* @since 1.0
*
* @throws \InvalidArgumentException
*/
public function __construct(Node $parent, $element = null)
public function __construct(Content $parent, $element = null)
{
parent::__construct($parent, $element);

if (null === $element) {
$parent->getDomElement()->setAttributeNS(Atom::NS, 'type', 'application/xml');
$parent->setAttribute('type', 'application/xml');
}

/** @var \DOMNodeList $nodes */
Expand Down
3 changes: 3 additions & 0 deletions src/ODataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Mekras\Atom\Atom;
use Mekras\Atom\Document\Document;
use Mekras\Atom\Element\Content;
use Mekras\Atom\Element\Element;
use Mekras\Atom\Extension\DocumentExtension;
use Mekras\Atom\Extension\ElementExtension;
Expand Down Expand Up @@ -87,6 +88,7 @@ public function parseElement(Node $parent, \DOMElement $element)
} elseif (OData::META === $element->namespaceURI) {
switch ($element->localName) {
case 'properties':
/** @var Content $parent */
return new Properties($parent, $element);
}
}
Expand All @@ -111,6 +113,7 @@ public function createElement(Node $parent, $name)
case 'atom:entry':
return new Entry($parent);
case 'm:properties':
/** @var Content $parent */
return new Properties($parent);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Element/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public function testAddRelationViaObject()
static::assertEquals(
'<entry>' .
'<link type="application/atom+xml;type=entry" ' .
'href="FooSet(123)" ' .
'rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Foo" ' .
'title="Foo" ' .
'href="FooSet(123)"/>' .
'title="Foo"/>' .
'</entry>',
$this->getXML($entry)
);
Expand All @@ -98,9 +98,9 @@ public function testAddRelationViaURI()
static::assertEquals(
'<entry>' .
'<link type="application/atom+xml;type=entry" ' .
'href="FooSet(123)" ' .
'rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Foo" ' .
'title="Foo" ' .
'href="FooSet(123)"/>' .
'title="Foo"/>' .
'</entry>',
$this->getXML($entry)
);
Expand Down
15 changes: 14 additions & 1 deletion tests/Element/PropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace Mekras\OData\Client\Tests\Element;

use Mekras\Atom\Element\Content;
use Mekras\OData\Client\EDM\Primitive;
use Mekras\OData\Client\Element\Properties;
use Mekras\OData\Client\Tests\TestCase;
Expand All @@ -18,13 +19,25 @@
*/
class PropertiesTest extends TestCase
{
/**
* Test creating new "m:properties" element.
*/
public function testCreate()
{
$content = new Content($this->createFakeNode());
new Properties($content);

static::assertEquals('application/xml', $content->getDomElement()->getAttribute('type'));
}

/**
*
*/
public function testIterator()
{
$document = $this->loadXML('Properties.xml');
$properties = new Properties($this->createFakeNode(), $document->firstChild);
$content = new Content($this->createFakeNode($document));
$properties = new Properties($content, $document->firstChild);
$names = [];
foreach ($properties as $name => $value) {
$names[] = $name;
Expand Down
10 changes: 7 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
/**
* Return new fake Node instance.
*
* @param \DOMDocument|null $document
*
* @return \PHPUnit_Framework_MockObject_MockObject|Node
*/
protected function createFakeNode()
protected function createFakeNode(\DOMDocument $document = null)
{
$doc = $this->createDocument();
if (null === $document) {
$document = $this->createDocument();
}

$node = $this->getMockBuilder(Node::class)->disableOriginalConstructor()
->setMethods(['getDomElement', 'getExtensions'])->getMock();
$node->expects(static::any())->method('getDomElement')
->willReturn($doc->documentElement);
->willReturn($document->documentElement);
$node->expects(static::any())->method('getExtensions')
->willReturn($this->createExtensions());

Expand Down

0 comments on commit 65604c6

Please sign in to comment.