Skip to content

Commit 42dfa1a

Browse files
author
Jolita Grazyte
committed
testing file content
1 parent 5ac6005 commit 42dfa1a

File tree

11 files changed

+411
-54
lines changed

11 files changed

+411
-54
lines changed

config/laravel-feed.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
'feeds' => [
66
[
77
'items' => '', // Fill in the class with a method that returns a collection of items that must come in the feed. e.g.: 'App\Repositories\NewsItemRepository@getAllOnline'
8-
'url' => '', // feed url, on which the feeds would be shown
9-
'title' => 'This is feed 1 from the unit tests',
10-
'description' => 'This is feed 1 from the unit tests.',
8+
'url' => '', // feed url, on which the feeds would be shown
9+
'title' => 'This is feed 1 from the unit tests',
10+
'description' => 'This is feed 1 from the unit tests.',
1111
],
1212
],
1313

resources/views/rss.blade.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
{{ $item['summary'] }}
2828
</summary>
2929

30-
<updated>
31-
{{ $item['updated'] }}
32-
</updated>
30+
<updated>{{ $item['updated'] }}</updated>
3331

3432
</entry>
3533

src/Feed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function feed($feed) : HttpResponse
2929
'link' => $feed['url'],
3030
'description' => $feed['description'],
3131
'title' => $feed['title'],
32-
'updated' => $feed['updated']
32+
'updated' => $feed['updated'],
3333
];
3434

3535
return Response::view('laravel-feed::rss', $data, 200, ['Content-Type' => 'application/atom+xml; chartset=UTF-8']);

src/FeedItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ public function getFeedItemUpdated() : Carbon;
1717
public function getFeedItemSummary() : string;
1818

1919
public function getFeedItemLink() : string;
20-
}
20+
}

tests/DummyItem.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ public function getFeedItemSummary() : string
3535

3636
public function getFeedItemUpdated() : Carbon
3737
{
38-
return Carbon::now();
38+
return Carbon::create('2016', '02', '29', '16','06','18');
3939
}
4040

4141
public function getFeedItemLink() : string
4242
{
4343
return 'http://localhost/news/ducimus-ipsum-consequatur-vel-libero-debitis-quis-voluptatem';
4444
}
45-
46-
}
45+
}

tests/FeedTest.php

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Spatie\Feed\Test;
44

5+
use Carbon\Carbon;
56
use Illuminate\Filesystem\Filesystem as File;
7+
68
class FeedTest extends TestCase
79
{
810
/** @test */
@@ -15,44 +17,82 @@ public function it_registers_routes_where_feeds_will_be_available()
1517
/** @test */
1618
public function a_feed_contains_meta_data()
1719
{
18-
$content = $this->call('GET', '/feed1')->getContent();
19-
20-
$metaData = [
21-
'<description>This is feed 1 from the unit tests</description>',
22-
'<link href="http://localhost/feed1">',
23-
'<updated>',
24-
];
25-
26-
foreach ($metaData as $metaDataItem) {
27-
$this->assertContains($metaDataItem, $content);
20+
$contents = $this->getContent();
21+
$metaData = $this->getMetaData();
22+
for ($i = 0; $i < count($contents); ++$i) {
23+
foreach ($metaData[$i] as $metaDataItem) {
24+
$this->assertContains($metaDataItem, $contents[$i]);
25+
}
2826
}
2927
}
3028

3129
/** @test */
3230
public function a_feed_contains_xml_content()
3331
{
34-
$content = $this->call('GET', '/feed1')->getContent();
35-
$file = 'tests/feeds.xml';
36-
app(File::class)->put($file, $content);
37-
$xml_reader = new \XMLReader();
38-
$xml_reader->open($file);
39-
$xml_reader->setParserProperty($xml_reader::VALIDATE, true);
40-
$this->assertTrue($xml_reader->isValid());
32+
$this->validate_xml($this->getContent());
33+
}
34+
35+
private function validate_xml($contents)
36+
{
37+
for ($i = 0; $i < count($contents); ++$i) {
38+
$file = $this->makeXmlFiles($i, $contents[$i]);
39+
$xml_reader = new \XMLReader();
40+
$xml_reader->open($file);
41+
$xml_reader->setParserProperty($xml_reader::VALIDATE, true);
42+
$this->assertTrue($xml_reader->isValid());
43+
}
4144
}
4245

4346
/** @test */
4447
public function a_feed_contains_all_selected_models()
4548
{
46-
$content = $this->call('GET', '/feed1')->getContent();
47-
$this->assertEquals(5, substr_count($content, '<entry>'));
49+
foreach ($this->getContent() as $content) {
50+
$this->assertEquals(5, substr_count($content, '<entry>'));
51+
}
4852
}
4953

5054
/** @test */
51-
public function a_feed_contains_a_expected_data()
55+
public function all_feed_items_have_expected_data()
56+
{
57+
$this->check_if_feed_has_expected_content($this->getContent());
58+
}
59+
60+
private function check_if_feed_has_expected_content($contents){
61+
for($i = 0; $i < count($contents); ++$i){
62+
$this->makeXmlFiles($i, $contents[$i]);
63+
$saved_file = app(File::class)->get('tests/xml-files/feeds_'.$i.'.xml');
64+
$file = app(File::class)->get('tests/feeds_'.$i.'.xml');
65+
$this->assertEquals($file, $saved_file);
66+
}
67+
}
68+
private function getMetaData()
69+
{
70+
return [
71+
[
72+
'<description>This is feed 1 from the unit tests</description>',
73+
'<link href="http://localhost/feed1">',
74+
'<updated>',
75+
],
76+
[
77+
'<description>This is feed 2 from the unit tests</description>',
78+
'<link href="http://localhost/feed2">',
79+
'<updated>',
80+
],
81+
];
82+
}
83+
84+
private function getContent()
85+
{
86+
return [
87+
$this->call('GET', '/feed1')->getContent(),
88+
$this->call('GET', '/feed2')->getContent(),
89+
];
90+
}
91+
92+
private function makeXmlFiles($i, $content)
5293
{
53-
$content = $this->call('GET', '/feed1')->getContent();
54-
$file = 'tests/feeds.xml';
94+
$file = 'tests/feeds_'.$i.'.xml';
5595
app(File::class)->put($file, $content);
56-
app(File::class)->get($file);
96+
return $file;
5797
}
58-
}
98+
}

tests/TestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ protected function getEnvironmentSetUp($app)
1818
'url' => '/feed1',
1919
'title' => 'Feed 1',
2020
'description' => 'This is feed 1 from the unit tests',
21-
'updated' => \Carbon\Carbon::now()->toATOMString(),
21+
'updated' => \Carbon\Carbon::create('2016', '02', '29', '16','06','18')->toATOMString(),
2222
],
2323
[
2424
'items' => 'Spatie\Feed\Test\DummyRepository@getAllOnline',
2525
'url' => '/feed2',
2626
'title' => 'Feed 2',
2727
'description' => 'This is feed 2 from the unit tests',
28-
'updated' => \Carbon\Carbon::now()->toATOMString(),
28+
'updated' => \Carbon\Carbon::create('2016', '02', '29', '16','06','18')->toATOMString(),
2929
],
3030
];
3131

3232
$app['config']->set('laravel-feed.feeds', $feed);
3333
}
34-
}
34+
}

tests/feeds.xml renamed to tests/feeds_0.xml

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<title>Feed 1</title>
1010

11-
<updated>2016-02-29T13:10:50+00:00</updated>
11+
<updated>2016-02-29T16:06:18+00:00</updated>
1212

1313

1414

@@ -26,9 +26,7 @@
2626
Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.
2727
</summary>
2828

29-
<updated>
30-
2016-02-29 13:10:50
31-
</updated>
29+
<updated>2016-02-29 16:06:18</updated>
3230

3331
</entry>
3432

@@ -47,9 +45,7 @@
4745
Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.
4846
</summary>
4947

50-
<updated>
51-
2016-02-29 13:10:50
52-
</updated>
48+
<updated>2016-02-29 16:06:18</updated>
5349

5450
</entry>
5551

@@ -68,9 +64,7 @@
6864
Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.
6965
</summary>
7066

71-
<updated>
72-
2016-02-29 13:10:50
73-
</updated>
67+
<updated>2016-02-29 16:06:18</updated>
7468

7569
</entry>
7670

@@ -89,9 +83,7 @@
8983
Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.
9084
</summary>
9185

92-
<updated>
93-
2016-02-29 13:10:50
94-
</updated>
86+
<updated>2016-02-29 16:06:18</updated>
9587

9688
</entry>
9789

@@ -110,9 +102,7 @@
110102
Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.
111103
</summary>
112104

113-
<updated>
114-
2016-02-29 13:10:50
115-
</updated>
105+
<updated>2016-02-29 16:06:18</updated>
116106

117107
</entry>
118108

tests/feeds_1.xml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<feed xmlns="http://www.w3.org/2005/Atom">
4+
5+
<link href="http://localhost/feed2"></link>
6+
7+
<description>This is feed 2 from the unit tests</description>
8+
9+
<title>Feed 2</title>
10+
11+
<updated>2016-02-29T16:06:18+00:00</updated>
12+
13+
14+
15+
<entry>
16+
<title>
17+
Ducimus ipsum consequatur vel libero debitis quis voluptatem.
18+
</title>
19+
<link>http://localhost/news/ducimus-ipsum-consequatur-vel-libero-debitis-quis-voluptatem</link>
20+
21+
<id>
22+
1
23+
</id>
24+
25+
<summary>
26+
Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.
27+
</summary>
28+
29+
<updated>2016-02-29 16:06:18</updated>
30+
31+
</entry>
32+
33+
34+
<entry>
35+
<title>
36+
Ducimus ipsum consequatur vel libero debitis quis voluptatem.
37+
</title>
38+
<link>http://localhost/news/ducimus-ipsum-consequatur-vel-libero-debitis-quis-voluptatem</link>
39+
40+
<id>
41+
1
42+
</id>
43+
44+
<summary>
45+
Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.
46+
</summary>
47+
48+
<updated>2016-02-29 16:06:18</updated>
49+
50+
</entry>
51+
52+
53+
<entry>
54+
<title>
55+
Ducimus ipsum consequatur vel libero debitis quis voluptatem.
56+
</title>
57+
<link>http://localhost/news/ducimus-ipsum-consequatur-vel-libero-debitis-quis-voluptatem</link>
58+
59+
<id>
60+
1
61+
</id>
62+
63+
<summary>
64+
Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.
65+
</summary>
66+
67+
<updated>2016-02-29 16:06:18</updated>
68+
69+
</entry>
70+
71+
72+
<entry>
73+
<title>
74+
Ducimus ipsum consequatur vel libero debitis quis voluptatem.
75+
</title>
76+
<link>http://localhost/news/ducimus-ipsum-consequatur-vel-libero-debitis-quis-voluptatem</link>
77+
78+
<id>
79+
1
80+
</id>
81+
82+
<summary>
83+
Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.
84+
</summary>
85+
86+
<updated>2016-02-29 16:06:18</updated>
87+
88+
</entry>
89+
90+
91+
<entry>
92+
<title>
93+
Ducimus ipsum consequatur vel libero debitis quis voluptatem.
94+
</title>
95+
<link>http://localhost/news/ducimus-ipsum-consequatur-vel-libero-debitis-quis-voluptatem</link>
96+
97+
<id>
98+
1
99+
</id>
100+
101+
<summary>
102+
Officia aliquid rem repudiandae ut sed voluptatem non. Fuga libero omnis atque quam error. Iure dolorum labore ducimus temporibus.
103+
</summary>
104+
105+
<updated>2016-02-29 16:06:18</updated>
106+
107+
</entry>
108+
109+
110+
</feed>

0 commit comments

Comments
 (0)