3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2018-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
27 * @category Repository
28 * @package GaletteTests
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2018-2023 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
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Entity\test\units
;
40 use PHPUnit\Framework\TestCase
;
41 use Laminas\Db\Adapter\Adapter
;
48 * @package GaletteTests
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2018-2023 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
55 class Status
extends TestCase
57 private \Galette\Core\Db
$zdb;
58 private array $remove = [];
59 private \Galette\Core\I18n
$i18n;
66 public function setUp(): void
68 $this->zdb
= new \Galette\Core\
Db();
69 $this->i18n
= new \Galette\Core\
I18n(
70 \Galette\Core\I18n
::DEFAULT_LANG
79 public function tearDown(): void
81 if (TYPE_DB
=== 'mysql') {
82 $this->assertSame([], $this->zdb
->getWarnings());
84 $this->deleteStatus();
92 private function deleteStatus()
94 if (is_array($this->remove
) && count($this->remove
) > 0) {
95 $delete = $this->zdb
->delete(\Galette\Entity\Status
::TABLE
);
96 $delete->where
->in(\Galette\Entity\Status
::PK
, $this->remove
);
97 $this->zdb
->execute($delete);
101 $this->zdb
->db
->query(
102 'TRUNCATE TABLE ' . PREFIX_DB
. \Galette\Core\History
::TABLE
,
103 \Laminas\Db\Adapter\Adapter
::QUERY_MODE_EXECUTE
112 public function testStatus()
114 global $i18n; // globals :(
117 $status = new \Galette\Entity\
Status($this->zdb
);
121 $status->add('Active member', 81)
125 $status->add('Test status', 81)
128 $select = $this->zdb
->select(\Galette\Core\L10n
::TABLE
);
131 'text_orig' => 'Test status'
134 $results = $this->zdb
->execute($select);
135 $result = (array)$results->current();
142 $this->remove
[] = $status->id
;
146 \Galette\Entity\Entitled
::ID_NOT_EXITS
,
147 $status->update(42, 'Active member', 81)
151 $status->update($id, 'Tested status', 81)
156 $status->getLabel($id)
159 $select = $this->zdb
->select(\Galette\Core\L10n
::TABLE
);
162 'text_orig' => 'Tested status'
165 $results = $this->zdb
->execute($select);
166 $result = (array)$results->current();
174 \Galette\Entity\Entitled
::ID_NOT_EXITS
,
178 $this->expectException(\RuntimeException
::class);
179 $this->expectExceptionMessage('You cannot delete default status!');
180 $status->delete($status::DEFAULT_STATUS
);
186 $select = $this->zdb
->select(\Galette\Core\L10n
::TABLE
);
189 'text_orig' => 'Tested status'
192 $results = $this->zdb
->execute($select);
193 $this->assertSame(0, $results->count());
201 public function testGetList()
203 $status = new \Galette\Entity\
Status($this->zdb
);
205 $list = $status->getList();
206 $this->assertCount(10, $list);
208 if ($this->zdb
->isPostgres()) {
209 $select = $this->zdb
->select($status::TABLE
. '_id_seq');
210 $select->columns(['last_value']);
211 $results = $this->zdb
->execute($select);
212 $result = $results->current();
213 $this->assertGreaterThanOrEqual(10, $result->last_value
, 'Incorrect status sequence');
215 $this->zdb
->db
->query(
216 'SELECT setval(\'' . PREFIX_DB
. $status::TABLE
. '_id_seq\', 1)',
217 Adapter
::QUERY_MODE_EXECUTE
222 $status->installInit();
224 $list = $status->getList();
225 $this->assertCount(10, $list);
227 if ($this->zdb
->isPostgres()) {
228 $select = $this->zdb
->select($status::TABLE
. '_id_seq');
229 $select->columns(['last_value']);
230 $results = $this->zdb
->execute($select);
231 $result = $results->current();
232 $this->assertGreaterThanOrEqual(10, $result->last_value
, 'Incorrect status sequence ' . $result->last_value
);