]> git.agnieray.net Git - galette.git/blob - tests/Galette/IO/tests/units/News.php
f225a430510118318e1f8113e64693a2fa12a320
[galette.git] / tests / Galette / IO / tests / units / News.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * News tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2017 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Core
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2017 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2017-03-07
36 */
37
38 namespace Galette\IO\test\units;
39
40 use atoum;
41
42 /**
43 * News tests class
44 *
45 * @category Core
46 * @name News
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2017 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since 2017-03-07
53 */
54 class News extends atoum
55 {
56 private $i18n;
57
58 /**
59 * Set up tests
60 *
61 * @param string $testMethod Method name
62 *
63 * @return void
64 */
65 public function beforeTestMethod($testMethod)
66 {
67 $this->i18n = new \Galette\Core\I18n();
68 global $i18n;
69 $i18n = $this->i18n;
70 }
71
72 /**
73 * Test news loading
74 *
75 * @return void
76 */
77 public function testLoadNews()
78 {
79 //ensure allow_url_fopen is on
80 ini_set('allow_url_fopen', true);
81 //load news without caching
82 $news = new \Galette\IO\News('https://galette.eu/site/feed.xml', true);
83 $posts = $news->getPosts();
84 $this->array($posts)
85 ->size->isGreaterThan(0);
86 }
87
88 /**
89 * Test news caching
90 *
91 * @return void
92 */
93 public function testCacheNews()
94 {
95 //will use default lang to build RSS URL
96 $file = GALETTE_CACHE_DIR . md5('https://galette.eu/site/fr/feed.xml') . '.cache';
97
98 //ensure file does not exists
99 $this->boolean(file_exists($file))->isFalse;
100
101 //load news with caching
102 $news = new \Galette\IO\News('https://galette.eu/site/feed.xml');
103
104 $posts = $news->getPosts();
105 $this->array($posts)
106 ->size->isGreaterThan(0);
107
108 //ensure file does exists
109 $this->boolean(file_exists($file))->isTrue;
110
111 $dformat = 'Y-m-d H:i:s';
112 $mdate = \DateTime::createFromFormat(
113 $dformat,
114 date(
115 $dformat,
116 filemtime($file)
117 )
118 );
119
120 $expired = $mdate->sub(
121 new \DateInterval('PT25H')
122 );
123 $touched = touch($file, $expired->getTimestamp());
124 $this->boolean($touched)->isTrue;
125
126 $news = new \Galette\IO\News('https://galette.eu/site/feed.xml');
127 $mnewdate = \DateTime::createFromFormat(
128 $dformat,
129 date(
130 $dformat,
131 filemtime($file)
132 )
133 );
134 $isnewdate = $mnewdate > $mdate;
135 $this->boolean($isnewdate)->isTrue;
136
137 //drop file finally
138 unlink($file);
139 }
140
141
142 /**
143 * Test news loading with allow_url_fopen off
144 *
145 * @return void
146 */
147 public function testLoadNewsWExeption()
148 {
149 $this->assert('News cannot be loaded')
150 ->if($this->function->ini_get = 0)
151 ->given($news = new \Galette\IO\News('https://galette.eu/site/feed.xml', true))
152 ->then
153 ->array($news->getPosts())
154 ->hasSize(0);
155 }
156 }