]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/PaymentType.php
Improve coding standards
[galette.git] / tests / Galette / Entity / tests / units / PaymentType.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
26 /**
27 * Payment type tests
28 *
29 * @author Johan Cwiklinski <johan@x-tnd.be>
30 */
31 class PaymentType extends TestCase
32 {
33 private \Galette\Core\Db $zdb;
34 private \Galette\Core\Preferences $preferences;
35 private \Galette\Core\Login $login;
36 private array $remove = [];
37 private \Galette\Core\I18n $i18n;
38
39 /**
40 * Set up tests
41 *
42 * @return void
43 */
44 public function setUp(): void
45 {
46 $this->zdb = new \Galette\Core\Db();
47 $this->preferences = new \Galette\Core\Preferences($this->zdb);
48 $this->i18n = new \Galette\Core\I18n(
49 \Galette\Core\I18n::DEFAULT_LANG
50 );
51 $this->login = new \Galette\Core\Login($this->zdb, $this->i18n);
52
53 $types = new \Galette\Repository\PaymentTypes($this->zdb, $this->preferences, $this->login);
54 $res = $types->installInit(false);
55 $this->assertTrue($res);
56 }
57
58 /**
59 * Tear down tests
60 *
61 * @return void
62 */
63 public function tearDown(): void
64 {
65 if (TYPE_DB === 'mysql') {
66 $this->assertSame([], $this->zdb->getWarnings());
67 }
68 $this->deletePaymentType();
69 }
70
71 /**
72 * Delete payment type
73 *
74 * @return void
75 */
76 private function deletePaymentType(): void
77 {
78 if (is_array($this->remove) && count($this->remove) > 0) {
79 $delete = $this->zdb->delete(\Galette\Entity\PaymentType::TABLE);
80 $delete->where->in(\Galette\Entity\PaymentType::PK, $this->remove);
81 $this->zdb->execute($delete);
82 }
83
84 //Clean logs
85 $this->zdb->db->query(
86 'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
87 \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
88 );
89 }
90
91 /**
92 * Test payment type
93 *
94 * @return void
95 */
96 public function testPaymentType(): void
97 {
98 global $i18n; // globals :(
99 $i18n = $this->i18n;
100
101 $type = new \Galette\Entity\PaymentType($this->zdb);
102
103 $type->name = 'Test payment type';
104 $this->assertTrue($type->store());
105
106 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
107 $select->where(
108 array(
109 'text_orig' => 'Test payment type'
110 )
111 );
112 $results = $this->zdb->execute($select);
113 $result = (array)$results->current();
114
115 $this->assertSame('Test payment type', $result['text_orig']);
116
117 $id = $type->id;
118 $this->remove[] = $id;
119
120 $type = new \Galette\Entity\PaymentType($this->zdb, $id);
121 $type->name = 'Changed test payment type';
122 $this->assertTrue($type->store());
123
124 $type = new \Galette\Entity\PaymentType($this->zdb, $id);
125 $this->assertSame('Changed test payment type', $type->getName());
126
127 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
128 $select->where(
129 array(
130 'text_orig' => 'Changed test payment type'
131 )
132 );
133 $results = $this->zdb->execute($select);
134 $this->assertSame(count($this->i18n->getArrayList()), count($results));
135
136 $type = new \Galette\Entity\PaymentType($this->zdb, \Galette\Entity\PaymentType::CASH);
137 $this->expectException(\RuntimeException::class);
138 $this->expectExceptionMessage('You cannot delete system payment types!');
139 $type->remove();
140
141 $type = new \Galette\Entity\PaymentType($this->zdb, $id);
142 $this->assertTrue($type->remove());
143
144 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
145 $select->where(
146 array(
147 'text_orig' => 'Test payment type'
148 )
149 );
150 $results = $this->zdb->execute($select);
151 $this->assertSame(0, $results->count());
152 }
153 }