]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Status.php
Improve coding standards
[galette.git] / tests / Galette / Entity / tests / units / Status.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 namespace Galette\Entity\test\units;
23
24 use PHPUnit\Framework\TestCase;
25 use Laminas\Db\Adapter\Adapter;
26
27 /**
28 * Status tests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 */
32 class Status extends TestCase
33 {
34 private \Galette\Core\Db $zdb;
35 private array $remove = [];
36 private \Galette\Core\I18n $i18n;
37
38 /**
39 * Set up tests
40 *
41 * @return void
42 */
43 public function setUp(): void
44 {
45 $this->zdb = new \Galette\Core\Db();
46 $this->i18n = new \Galette\Core\I18n(
47 \Galette\Core\I18n::DEFAULT_LANG
48 );
49 }
50
51 /**
52 * Tear down tests
53 *
54 * @return void
55 */
56 public function tearDown(): void
57 {
58 if (TYPE_DB === 'mysql') {
59 $this->assertSame([], $this->zdb->getWarnings());
60 }
61 $this->deleteStatus();
62 }
63
64 /**
65 * Delete status
66 *
67 * @return void
68 */
69 private function deleteStatus(): void
70 {
71 if (is_array($this->remove) && count($this->remove) > 0) {
72 $delete = $this->zdb->delete(\Galette\Entity\Status::TABLE);
73 $delete->where->in(\Galette\Entity\Status::PK, $this->remove);
74 $this->zdb->execute($delete);
75 }
76
77 //Clean logs
78 $this->zdb->db->query(
79 'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
80 \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
81 );
82 }
83
84 /**
85 * Test status
86 *
87 * @return void
88 */
89 public function testStatus(): void
90 {
91 global $i18n; // globals :(
92 $i18n = $this->i18n;
93
94 $status = new \Galette\Entity\Status($this->zdb);
95
96 $this->assertSame(
97 -2,
98 $status->add('Active member', 81)
99 );
100
101 $this->assertTrue(
102 $status->add('Test status', 81)
103 );
104
105 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
106 $select->where(
107 array(
108 'text_orig' => 'Test status'
109 )
110 );
111 $results = $this->zdb->execute($select);
112 $result = (array)$results->current();
113
114 $this->assertSame(
115 'Test status',
116 $result['text_orig']
117 );
118
119 $this->remove[] = $status->id;
120 $id = $status->id;
121
122 $this->assertSame(
123 \Galette\Entity\Status::ID_NOT_EXITS,
124 $status->update(42, 'Active member', 81)
125 );
126
127 $this->assertTrue(
128 $status->update($id, 'Tested status', 81)
129 );
130
131 $this->assertSame(
132 'Tested status',
133 $status->getLabel($id)
134 );
135
136 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
137 $select->where(
138 array(
139 'text_orig' => 'Tested status'
140 )
141 );
142 $results = $this->zdb->execute($select);
143 $result = (array)$results->current();
144
145 $this->assertSame(
146 'Tested status',
147 $result['text_orig']
148 );
149
150 $this->assertSame(
151 \Galette\Entity\Status::ID_NOT_EXITS,
152 $status->delete(42)
153 );
154
155 $this->expectException(\RuntimeException::class);
156 $this->expectExceptionMessage('You cannot delete default status!');
157 $status->delete($status::DEFAULT_STATUS);
158
159 $this->assertTrue(
160 $status->delete($id)
161 );
162
163 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
164 $select->where(
165 array(
166 'text_orig' => 'Tested status'
167 )
168 );
169 $results = $this->zdb->execute($select);
170 $this->assertSame(0, $results->count());
171 }
172
173 /**
174 * Test getList
175 *
176 * @return void
177 */
178 public function testGetList(): void
179 {
180 $status = new \Galette\Entity\Status($this->zdb);
181
182 $list = $status->getList();
183 $this->assertCount(10, $list);
184
185 if ($this->zdb->isPostgres()) {
186 $select = $this->zdb->select($status::TABLE . '_id_seq');
187 $select->columns(['last_value']);
188 $results = $this->zdb->execute($select);
189 $result = $results->current();
190 $this->assertGreaterThanOrEqual(10, $result->last_value, 'Incorrect status sequence');
191
192 $this->zdb->db->query(
193 'SELECT setval(\'' . PREFIX_DB . $status::TABLE . '_id_seq\', 1)',
194 Adapter::QUERY_MODE_EXECUTE
195 );
196 }
197
198 //reinstall status
199 $status->installInit();
200
201 $list = $status->getList();
202 $this->assertCount(10, $list);
203
204 if ($this->zdb->isPostgres()) {
205 $select = $this->zdb->select($status::TABLE . '_id_seq');
206 $select->columns(['last_value']);
207 $results = $this->zdb->execute($select);
208 $result = $results->current();
209 $this->assertGreaterThanOrEqual(10, $result->last_value, 'Incorrect status sequence ' . $result->last_value);
210 }
211 }
212 }