Completed
Push — master ( 2623ee...a82f7c )
by Jeroen De
06:13 queued 04:44
created

JsonBuilderTest::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace ModernTimeline\Tests\Unit;
6
7
use ModernTimeline\Event;
8
use ModernTimeline\JsonBuilder;
9
use ModernTimeline\ResultFacade\PropertyValueCollection;
10
use ModernTimeline\ResultFacade\Subject;
11
use ModernTimeline\SlidePresenter\SimpleSlidePresenter;
12
use PHPUnit\Framework\TestCase;
13
use SMW\DIWikiPage;
14
use SMW\Query\PrintRequest;
15
use SMWDITime;
16
use Title;
17
18
/**
19
 * @covers \ModernTimeline\JsonBuilder
20
 */
21
class JsonBuilderTest extends TestCase {
22
23
	private const PAGE_NAME = 'Some Page';
24
25
	public function testStartDate() {
26
		$json = $this->toJson( $this->newEventWithStartAndEndDate() );
27
28
		$this->assertSame(
29
			[
30
				'year' => 2019,
31
				'month' => 8,
32
				'day' => 2,
33
				'hour' => 16,
34
				'minute' => 7,
35
				'second' => 42,
36
			],
37
			$json['events'][0]['start_date']
38
		);
39
	}
40
41
	private function toJson( Event ...$events ): array {
42
		return ( new JsonBuilder( new SimpleSlidePresenter() ) )->eventsToTimelineJson( $events );
43
	}
44
45
	private function newEventWithStartAndEndDate(): Event {
46
		return new Event(
47
			$this->newSubjectWithStartAndEndDate(),
48
			$this->newStartDate(),
49
			$this->newEndDate()
50
		);
51
	}
52
53
	private function newSubjectWithStartAndEndDate(): Subject {
54
		return new Subject(
55
			$this->newDiWikiPage(),
56
			[
57
				$this->newStartDateValueCollection(),
58
				$this->newEndDateValueCollection()
59
			]
60
		);
61
	}
62
63
	private function newDiWikiPage( string $pageName = self::PAGE_NAME ): DIWikiPage {
64
		$page = $this->createMock( DIWikiPage::class );
65
66
		$page->method( 'getTitle' )->willReturn( Title::newFromText( $pageName ) );
67
68
		return $page;
69
	}
70
71
	private function newStartDateValueCollection(): PropertyValueCollection {
72
		return new PropertyValueCollection(
73
			$this->newDatePrintRequestWithLabel( 'Has date' ),
74
			[
75
				$this->newStartDate()
76
			]
77
		);
78
	}
79
80
	private function newStartDate(): SMWDITime {
81
		return new SMWDITime(
82
			SMWDITime::CM_GREGORIAN,
83
			2019,
84
			8,
85
			2,
86
			16,
87
			7,
88
			42
89
		);
90
	}
91
92
	private function newEndDateValueCollection(): PropertyValueCollection {
93
		return new PropertyValueCollection(
94
			$this->newDatePrintRequestWithLabel( 'End date' ),
95
			[
96
				$this->newEndDate()
97
			]
98
		);
99
	}
100
101
	private function newEndDate(): SMWDITime {
102
		return new SMWDITime(
103
			SMWDITime::CM_GREGORIAN,
104
			2019,
105
			8,
106
			5,
107
			17,
108
			39,
109
			23
110
		);
111
	}
112
113
	private function newDatePrintRequestWithLabel( string $label ): PrintRequest {
114
		$pr = $this->createMock( PrintRequest::class );
115
		$pr->method( 'getLabel' )->willReturn( $label );
116
		$pr->method( 'getTypeID' )->willReturn( '_dat' );
117
		return $pr;
118
	}
119
120
	public function testEndDate() {
121
		$json = $this->toJson( $this->newEventWithStartAndEndDate() );
122
123
		$this->assertSame(
124
			[
125
				'year' => 2019,
126
				'month' => 8,
127
				'day' => 5,
128
				'hour' => 17,
129
				'minute' => 39,
130
				'second' => 23,
131
			],
132
			$json['events'][0]['end_date']
133
		);
134
	}
135
136
	public function testHeadline() {
137
		$json = $this->toJson( $this->newEventWithStartAndEndDate() );
138
139
		$this->assertContains(
140
			self::PAGE_NAME,
141
			$json['events'][0]['text']['headline']
142
		);
143
	}
144
145
	public function testPageWithStartAndEndDateOnlyLeadsToOneEvent() {
146
		$this->assertCount(
147
			1,
148
			$this->toJson( $this->newEventWithStartAndEndDate() )['events']
149
		);
150
	}
151
152
	public function testEventWithoutImageHasNoMedia() {
153
		$this->assertArrayNotHasKey(
154
			'media',
155
			$this->toJson( $this->newEventWithStartAndEndDate() )['events'][0]
156
		);
157
	}
158
159
}
160