File: MicroformatsWikiExamplesTest.php

package info (click to toggle)
php-mf2 0.3.0-0.2
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,896 kB
  • sloc: php: 2,584; xml: 8; makefile: 2
file content (188 lines) | stat: -rw-r--r-- 4,984 bytes parent folder | download | duplicates (2)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php

/**
 * Tests of the parsing methods within mf2\Parser
 */

namespace Mf2\Parser\Test;

use Mf2\Parser;
use PHPUnit_Framework_TestCase;

/**
 * Microformats Wiki Examples
 * 
 * Contains tests built directly from examples given on the microformats wiki pages about µf2.
 * 
 * These tend to compare the JSON-encoded output of Parser::parse() with the JSON strings given on the wiki.
 * If the given JSON is not within an `items` key of the root object, I add it and mark as so.
 * 
 * @author Barnaby Walters waterpigs.co.uk <barnaby@waterpigs.co.uk>
 */
class MicroformatsWikiExamplesTest extends PHPUnit_Framework_TestCase {

	public function setUp() {
		date_default_timezone_set('Europe/London');
	}
		
	public function testHandlesEmptyStringsCorrectly() {
		$input = '';
		$expected = '{
	"rels": {},
	"items": []
}';
		
		$parser = new Parser($input, '', true);
		$output = $parser->parse();
		
		$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
	}
	
	public function testHandlesNullCorrectly() {
		$input = Null;
		$expected = '{
	"rels": {},
	"items": []
}';
		
		$parser = new Parser($input, '', true);
		$parser->jsonMode = true;
		$output = $parser->parse();
		
		$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
	}
		
	/**
	 * From http://microformats.org/wiki/microformats-2
	 */
	public function testSimplePersonReference() {
		$input = '<span class="h-card">Frances Berriman</span>';
		$expected = '{
	"rels": {},
  "items": [{ 
	"type": ["h-card"],
	"properties": {
	  "name": ["Frances Berriman"] 
	}
  }]
}';
		$parser = new Parser($input, '', true);
		$output = $parser->parse();

		$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
	}

	/**
	 * From http://microformats.org/wiki/microformats-2
	 */
	public function testSimpleHyperlinkedPersonReference() {
		$input = '<a class="h-card" href="http://benward.me">Ben Ward</a>';
		$expected = '{
	"rels": {},
  "items": [{ 
	"type": ["h-card"],
	"properties": {
	  "name": ["Ben Ward"],
	  "url": ["http://benward.me"]
	}
  }]
}';
		$parser = new Parser($input, '', true);
		$output = $parser->parse();

		$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
	}

	/**
	 * From http://microformats.org/wiki/microformats-2-implied-properties
	 */
	public function testSimplePersonImage() {
		$input = '<img class="h-card" src="http://example.org/pic.jpg" alt="Chris Messina" />';
		// Added root items key
		$expected = '{
	"rels": {},
	"items": [{ 
  "type": ["h-card"],
  "properties": {
	"name": ["Chris Messina"],
	"photo": ["http://example.org/pic.jpg"]
  }
}]}';
		$parser = new Parser($input, '', true);
		$output = $parser->parse();

		$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
	}

	/**
	 * From http://microformats.org/wiki/microformats-2-implied-properties
	 */
	public function testHyperlinkedImageNameAndPhotoProperties() {
		$input = '<a class="h-card" href="http://rohit.khare.org/">
 <img alt="Rohit Khare"
	  src="https://s3.amazonaws.com/twitter_production/profile_images/53307499/180px-Rohit-sq_bigger.jpg" />
</a>';
		// Added root items key
		$expected = '{
	"rels": {},
	"items": [{ 
  "type": ["h-card"],
  "properties": {
	"name": ["Rohit Khare"],
	"url": ["http://rohit.khare.org/"],
	"photo": ["https://s3.amazonaws.com/twitter_production/profile_images/53307499/180px-Rohit-sq_bigger.jpg"]
  }
}]}';
		$parser = new Parser($input, '', true);
		$output = $parser->parse();

		$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
	}

	/**
	 * From http://microformats.org/wiki/microformats-2
	 */
	public function testMoreDetailedPerson() {
		$input = '<div class="h-card">
  <img class="u-photo" alt="photo of Mitchell"
	   src="https://webfwd.org/content/about-experts/300.mitchellbaker/mentor_mbaker.jpg"/>
  <a class="p-name u-url"
	 href="http://blog.lizardwrangler.com/" 
	>Mitchell Baker</a>
 (<a class="u-url" 
	 href="https://twitter.com/MitchellBaker"
	>@MitchellBaker</a>)
  <span class="p-org">Mozilla Foundation</span>
  <p class="p-note">
	Mitchell is responsible for setting the direction and scope of the Mozilla Foundation and its activities.
  </p>
  <span class="p-category">Strategy</span>
  <span class="p-category">Leadership</span>
</div>';

		$expected = '{
  "rels": {},
	"items": [{ 
	"type": ["h-card"],
	"properties": {
	  "photo": ["https://webfwd.org/content/about-experts/300.mitchellbaker/mentor_mbaker.jpg"],
	  "name": ["Mitchell Baker"],
	  "url": [
		"http://blog.lizardwrangler.com/",
		"https://twitter.com/MitchellBaker"
	  ],
	  "org": ["Mozilla Foundation"],
	  "note": ["Mitchell is responsible for setting the direction and scope of the Mozilla Foundation and its activities."],
	  "category": [
		"Strategy",
		"Leadership"
	  ]
	}
  }]
}';
		$parser = new Parser($input, '', true);
		$output = $parser->parse();

		$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
	}
}