Repository navigation
Expand file tree
/
Copy pathAuthorTest.php
More file actions
122 lines (92 loc) · 3.86 KB
/
Copy pathAuthorTest.php
File metadata and controls
122 lines (92 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Created by PhpStorm.
* User: alex
*/
namespace FeedIo\Rule\Atom;
use FeedIo\Feed\Item;
use PHPUnit\Framework\TestCase;
class AuthorTest extends TestCase
{
/**
* @var \FeedIo\Rule\Author
*/
protected $object;
protected function setUp(): void
{
$this->object = new Author();
}
public function testGetNodeName()
{
$this->assertEquals('author', $this->object->getNodeName());
}
public function testSet()
{
$item = new Item();
$document = new \DOMDocument();
$author = $document->createElement('author');
$author->appendChild($document->createElement('name', 'John Doe'));
$author->appendChild($document->createElement('uri', 'http://localhost'));
$author->appendChild($document->createElement('email', 'john@localhost'));
$this->object->setProperty($item, $author);
$this->assertEquals('John Doe', $item->getAuthor()->getName());
$this->assertEquals('http://localhost', $item->getAuthor()->getUri());
$this->assertEquals('john@localhost', $item->getAuthor()->getEmail());
}
public function testNamespacedSet()
{
$item = new Item();
$ns = 'http://www.w3.org/2005/Atom';
$impl = new \DOMImplementation();
$document = $impl->createDocument($ns, 'feed');
$author = $document->createElementNS($ns, 'author');
$author->appendChild($document->createElementNS($ns, 'name', 'John Doe'));
$author->appendChild($document->createElementNS($ns, 'uri', 'http://localhost'));
$author->appendChild($document->createElementNS($ns, 'email', 'john@localhost'));
$this->object->setProperty($item, $author);
$this->assertEquals('John Doe', $item->getAuthor()->getName());
$this->assertEquals('http://localhost', $item->getAuthor()->getUri());
$this->assertEquals('john@localhost', $item->getAuthor()->getEmail());
}
public function testSetKeepsEveryAuthorAndGetAuthorReturnsTheLast()
{
$item = new Item();
$document = new \DOMDocument();
$first = $document->createElement('author');
$first->appendChild($document->createElement('name', 'John Doe'));
$second = $document->createElement('author');
$second->appendChild($document->createElement('name', 'Jane Roe'));
$this->object->setProperty($item, $first);
$this->object->setProperty($item, $second);
$this->assertEquals('Jane Roe', $item->getAuthor()->getName());
$names = array_map(fn ($author) => $author->getName(), [...$item->getAuthors()]);
$this->assertEquals(['John Doe', 'Jane Roe'], $names);
}
public function testGetChildValue()
{
$document = new \DOMDocument();
$author = $document->createElement('author');
$author->appendChild($document->createElement('name', 'John Doe'));
$this->assertEquals('John Doe', $this->object->getChildValue($author, 'name'));
}
public function testCreateElement()
{
$item = new Item();
$author = new \FeedIo\Feed\Item\Author();
$author->setName('John Doe');
$author->setUri('http://localhost');
$author->setEmail('john@localhost');
$item->setAuthor($author);
$document = new \DOMDocument();
$rootElement = $document->createElement('feed');
$this->object->apply($document, $rootElement, $item);
$addedElement = $rootElement->firstChild;
$this->assertInstanceOf('\DomElement', $addedElement);
$this->assertEquals('author', $addedElement->nodeName);
$document->appendChild($rootElement);
$this->assertXmlStringEqualsXmlString(
'<?xml version="1.0"?><feed><author><name>John Doe</name><uri>http://localhost</uri><email>john@localhost</email></author></feed>',
$document->saveXML()
);
}
}