4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
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.
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.
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/>.
22 namespace Galette\Entity\test\units
;
24 use PHPUnit\Framework\TestCase
;
25 use Laminas\Db\Adapter\Adapter
;
30 * @author Johan Cwiklinski <johan@x-tnd.be>
32 class Status
extends TestCase
34 private \Galette\Core\Db
$zdb;
35 private array $remove = [];
36 private \Galette\Core\I18n
$i18n;
43 public function setUp(): void
45 $this->zdb
= new \Galette\Core\
Db();
46 $this->i18n
= new \Galette\Core\
I18n(
47 \Galette\Core\I18n
::DEFAULT_LANG
56 public function tearDown(): void
58 if (TYPE_DB
=== 'mysql') {
59 $this->assertSame([], $this->zdb
->getWarnings());
61 $this->deleteStatus();
69 private function deleteStatus(): void
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);
78 $this->zdb
->db
->query(
79 'TRUNCATE TABLE ' . PREFIX_DB
. \Galette\Core\History
::TABLE
,
80 \Laminas\Db\Adapter\Adapter
::QUERY_MODE_EXECUTE
89 public function testStatus(): void
91 global $i18n; // globals :(
94 $status = new \Galette\Entity\
Status($this->zdb
);
98 $status->add('Active member', 81)
102 $status->add('Test status', 81)
105 $select = $this->zdb
->select(\Galette\Core\L10n
::TABLE
);
108 'text_orig' => 'Test status'
111 $results = $this->zdb
->execute($select);
112 $result = (array)$results->current();
119 $this->remove
[] = $status->id
;
123 \Galette\Entity\Status
::ID_NOT_EXITS
,
124 $status->update(42, 'Active member', 81)
128 $status->update($id, 'Tested status', 81)
133 $status->getLabel($id)
136 $select = $this->zdb
->select(\Galette\Core\L10n
::TABLE
);
139 'text_orig' => 'Tested status'
142 $results = $this->zdb
->execute($select);
143 $result = (array)$results->current();
151 \Galette\Entity\Status
::ID_NOT_EXITS
,
155 $this->expectException(\RuntimeException
::class);
156 $this->expectExceptionMessage('You cannot delete default status!');
157 $status->delete($status::DEFAULT_STATUS
);
163 $select = $this->zdb
->select(\Galette\Core\L10n
::TABLE
);
166 'text_orig' => 'Tested status'
169 $results = $this->zdb
->execute($select);
170 $this->assertSame(0, $results->count());
178 public function testGetList(): void
180 $status = new \Galette\Entity\
Status($this->zdb
);
182 $list = $status->getList();
183 $this->assertCount(10, $list);
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');
192 $this->zdb
->db
->query(
193 'SELECT setval(\'' . PREFIX_DB
. $status::TABLE
. '_id_seq\', 1)',
194 Adapter
::QUERY_MODE_EXECUTE
199 $status->installInit();
201 $list = $status->getList();
202 $this->assertCount(10, $list);
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
);