]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/ContributionsTypes.php
Improve coding standards
[galette.git] / tests / Galette / Entity / tests / units / ContributionsTypes.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 * Contributions types tests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 */
32 class ContributionsTypes 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->deleteTypes();
62 }
63
64 /**
65 * Delete contributions types
66 *
67 * @return void
68 */
69 private function deleteTypes(): void
70 {
71 if (is_array($this->remove) && count($this->remove) > 0) {
72 $delete = $this->zdb->delete(\Galette\Entity\ContributionsTypes::TABLE);
73 $delete->where->in(\Galette\Entity\ContributionsTypes::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 contributions types
86 *
87 * @return void
88 */
89 public function testContributionsTypes(): void
90 {
91 global $i18n; // globals :(
92 $i18n = $this->i18n;
93
94 $ctype = new \Galette\Entity\ContributionsTypes($this->zdb);
95
96 $this->assertSame(
97 -2,
98 $ctype->add('annual fee', 10, false)
99 );
100
101 $this->assertTrue(
102 $ctype->add('Test contribution type', null, false)
103 );
104
105 $this->remove[] = $ctype->id;
106 $id = $ctype->id;
107
108 $ctype_id = $ctype->getIdByLabel('Test contribution type');
109 $this->assertGreaterThan(0, $ctype_id);
110
111 $test_ctype = $ctype->get($ctype_id);
112 $this->assertInstanceOf(\ArrayObject::class, $test_ctype);
113
114 $this->assertSame('Test contribution type', $test_ctype['libelle_type_cotis']);
115 $this->assertNull($test_ctype['amount']);
116 $this->assertSame($this->zdb->isPostgres() ? false : 0, $test_ctype['cotis_extension']);
117
118 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
119 $select->where(
120 array(
121 'text_orig' => 'Test contribution type'
122 )
123 );
124 $results = $this->zdb->execute($select);
125 $result = (array)$results->current();
126
127 $this->assertSame(
128 'Test contribution type',
129 $result['text_orig']
130 );
131
132 $this->assertSame(
133 \Galette\Entity\ContributionsTypes::ID_NOT_EXITS,
134 $ctype->update(42, 'annual fee', 10, false)
135 );
136
137 $this->assertTrue(
138 $ctype->update($id, 'Tested contribution type', 42, true)
139 );
140
141 $this->assertSame(
142 'Tested contribution type',
143 $ctype->getLabel($id)
144 );
145
146 $test_ctype = $ctype->get($id);
147 $this->assertInstanceOf(\ArrayObject::class, $test_ctype);
148
149 $this->assertSame('Tested contribution type', $test_ctype['libelle_type_cotis']);
150 $this->assertSame(42.0, (float)$test_ctype['amount']);
151 $this->assertSame($this->zdb->isPostgres() ? true : 1, $test_ctype['cotis_extension']);
152
153 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
154 $select->where(
155 array(
156 'text_orig' => 'Tested contribution type'
157 )
158 );
159 $results = $this->zdb->execute($select);
160 $result = (array)$results->current();
161
162 $this->assertSame(
163 'Tested contribution type',
164 $result['text_orig']
165 );
166
167 $this->assertSame(
168 \Galette\Entity\ContributionsTypes::ID_NOT_EXITS,
169 $ctype->delete(42)
170 );
171
172 /*$this->expectException(\RuntimeException::class);
173 $this->expectExceptionMessage('You cannot delete default contribution type!');
174 $status->delete($status::DEFAULT_STATUS);*/
175
176 $this->assertTrue(
177 $ctype->delete($id)
178 );
179
180 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
181 $select->where(
182 array(
183 'text_orig' => 'Tested contribution type'
184 )
185 );
186 $results = $this->zdb->execute($select);
187 $this->assertSame(0, $results->count());
188 }
189
190 /**
191 * Test getList
192 *
193 * @return void
194 */
195 public function testGetList(): void
196 {
197 $ctypes = new \Galette\Entity\ContributionsTypes($this->zdb);
198
199 $list = $ctypes->getList();
200 $this->assertCount(7, $list);
201
202 if ($this->zdb->isPostgres()) {
203 $select = $this->zdb->select($ctypes::TABLE . '_id_seq');
204 $select->columns(['last_value']);
205 $results = $this->zdb->execute($select);
206 $result = $results->current();
207 $this->assertGreaterThanOrEqual(7, $result->last_value, 'Incorrect contributions types sequence');
208
209 $this->zdb->db->query(
210 'SELECT setval(\'' . PREFIX_DB . $ctypes::TABLE . '_id_seq\', 1)',
211 Adapter::QUERY_MODE_EXECUTE
212 );
213 }
214
215 //reinstall status
216 $ctypes->installInit();
217
218 $list = $ctypes->getList();
219 $this->assertCount(7, $list);
220
221 if ($this->zdb->isPostgres()) {
222 $select = $this->zdb->select($ctypes::TABLE . '_id_seq');
223 $select->columns(['last_value']);
224 $results = $this->zdb->execute($select);
225 $result = $results->current();
226 $this->assertGreaterThanOrEqual(7, $result->last_value, 'Incorrect contributions types sequence ' . $result->last_value);
227 }
228 }
229 }