]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/History.php
Switch to PSR12, phpcbf fix
[galette.git] / tests / Galette / Core / tests / units / History.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * History tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2016 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 2016 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 2016-11-26
36 */
37
38 namespace Galette\Core\test\units;
39
40 use atoum;
41
42 /**
43 * History tests class
44 *
45 * @category Core
46 * @name History
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2016 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 2016-11-26
53 */
54 class History extends atoum
55 {
56 private $history = null;
57 private $login;
58 private $zdb;
59 private $i18n;
60 private $session;
61
62 /**
63 * Set up tests
64 *
65 * @param string $testMethod Method name
66 *
67 * @return void
68 */
69 public function beforeTestMethod($testMethod)
70 {
71 $this->zdb = new \Galette\Core\Db();
72 $this->i18n = new \Galette\Core\I18n();
73 $this->session = new \RKA\Session();
74 $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
75 $this->history = new \Galette\Core\History($this->zdb, $this->login);
76 }
77
78 /**
79 * Test class constants
80 *
81 * @return void
82 */
83 public function testConstants()
84 {
85 $this->string(\Galette\Core\History::TABLE)->isIdenticalTo('logs');
86 $this->string(\Galette\Core\History::PK)->isIdenticalTo('id_log');
87 }
88
89 /**
90 * Test history workflow
91 *
92 * @return void
93 */
94 public function testHistoryFlow()
95 {
96 //nothing in the logs at the begining
97 $list = $this->history->getHistory();
98 $this->array($list)->hasSize(0);
99
100 //add some entries
101 $add = $this->history->add(
102 'Test',
103 'Something was added from tests'
104 );
105 $this->boolean($add)->isTrue();
106
107 $add = $this->history->add(
108 'Test',
109 'Something else was added from tests',
110 'SELECT * FROM none WHERE non ORDER BY none'
111 );
112 $this->boolean($add)->isTrue();
113
114 $add = $this->history->add(
115 'AnotherTest',
116 'And something else, again'
117 );
118 $this->boolean($add)->isTrue();
119
120 //check what has been stored
121 $list = $this->history->getHistory();
122 $this->array($list)->hasSize(3);
123
124 $actions = $this->history->getActionsList();
125 $this->array($actions)
126 ->hasSize(2)
127 ->string[0]->isIdenticalTo('AnotherTest')
128 ->string[1]->isIdenticalTo('Test');
129
130 //some filtering
131 $this->history->filters->action_filter = 'Test';
132 $list = $this->history->getHistory();
133 $this->array($list)->hasSize(2);
134
135 $this->history->filters->start_date_filter = date('Y-m-d');
136 $this->history->filters->end_date_filter = date('Y-m-d');
137 $list = $this->history->getHistory();
138 $this->array($list)->hasSize(2);
139
140 //let's clean now
141 $cleaned = $this->history->clean();
142 $this->boolean($cleaned)->isTrue();
143
144 $list = $this->history->getHistory();
145 $this->array($list)->hasSize(1);
146
147 $this->zdb->db->query(
148 'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
149 \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
150 );
151 }
152
153 /**
154 * Test add that throws an exception
155 *
156 * @return void
157 */
158 public function testAddWException()
159 {
160 $this->zdb = new \mock\Galette\Core\Db();
161 $this->calling($this->zdb)->execute = function ($o) {
162 throw new \LogicException('Error executing query!', 123);
163 };
164
165 $this->history = new \Galette\Core\History($this->zdb, $this->login);
166 $add = $this->history->add('Test');
167 $this->boolean($add)->isFalse();
168 }
169
170 /**
171 * Test getHistory that throws an exception
172 *
173 * @return void
174 */
175 public function testGetHistoryWException()
176 {
177 $this->zdb = new \mock\Galette\Core\Db();
178 $this->calling($this->zdb)->execute = function ($o) {
179 throw new \LogicException('Error executing query!', 123);
180 };
181
182 $this->history = new \Galette\Core\History($this->zdb, $this->login);
183 $list = $this->history->getHistory();
184 $this->boolean($list)->isFalse();
185 }
186 }