]> git.agnieray.net Git - galette.git/blob - tests/Galette/Controllers/tests/units/PdfController.php
Check MySQL warning in tests suite
[galette.git] / tests / Galette / Controllers / tests / units / PdfController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * PDF model tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Entity
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2020 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
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2020-11-21
36 */
37
38 namespace Galette\Controllers\test\units;
39
40 use atoum;
41
42 /**
43 * PDF controller tests
44 *
45 * @category Controllers
46 * @name PdfController
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2020 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since 2020-12-06
53 */
54 class PdfController extends atoum
55 {
56 private $zdb;
57 private $preferences;
58 private $login;
59 private $remove = [];
60 private $i18n;
61 private $container;
62 private $history;
63
64 private $adh;
65 private $contrib;
66 private $members_fields;
67
68 private $mocked_router;
69 private $session;
70 private $flash_data = [];
71 private $flash;
72
73 /**
74 * Set up tests
75 *
76 * @param string $method Calling method
77 *
78 * @return void
79 */
80 public function beforeTestMethod($method)
81 {
82 $this->zdb = new \Galette\Core\Db();
83 $this->preferences = new \Galette\Core\Preferences($this->zdb);
84 $this->i18n = new \Galette\Core\I18n(
85 \Galette\Core\I18n::DEFAULT_LANG
86 );
87 $this->login = new \Galette\Core\Login($this->zdb, $this->i18n);
88
89 $models = new \Galette\Repository\PdfModels($this->zdb, $this->preferences, $this->login);
90 $res = $models->installInit(false);
91 $this->boolean($res)->isTrue();
92
93 $this->mocked_router = new \mock\Slim\Router();
94 $this->calling($this->mocked_router)->pathFor = function ($name, $params) {
95 return $name;
96 };
97
98 $this->session = new \RKA\Session();
99 $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
100 $flash_data = [];
101 $this->flash_data = &$flash_data;
102 $this->flash = new \Slim\Flash\Messages($flash_data);
103
104 $_SERVER['HTTP_HOST'] = '';
105
106 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
107 $this->members_fields = $members_fields;
108
109 $this->adh = new \Galette\Entity\Adherent($this->zdb);
110 $this->adh->setDependencies(
111 $this->preferences,
112 $this->members_fields,
113 $this->history
114 );
115
116 $app = new \Slim\App(['router' => $this->mocked_router, 'flash' => $this->flash]);
117 $this->container = $app->getContainer();
118
119 $this->container['view'] = null;
120 $this->container['zdb'] = $this->zdb;
121 $this->container['login'] = $this->login;
122 $this->container['session'] = $this->session;
123 $this->container['preferences'] = $this->preferences;
124 $this->container['logo'] = null;
125 $this->container['print_logo'] = null;
126 $this->container['plugins'] = null;
127 $this->container['history'] = $this->history;
128 $this->container['i18n'] = null;
129 $this->container['fields_config'] = null;
130 $this->container['lists_config'] = null;
131 $this->container['l10n'] = null;
132 $this->container['members_fields'] = $this->members_fields;
133 $members_form_fields = $this->members_fields;
134 foreach ($members_form_fields as $k => $field) {
135 if ($field['position'] == -1) {
136 unset($members_form_fields[$k]);
137 }
138 }
139 //$this->members_form_fields = $members_form_fields;
140 $this->container['members_form_fields'] = $members_form_fields;
141
142 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields_cats.php';
143 $this->container['members_fields_cats'] = $members_fields_cats;
144
145 global $container, $zdb, $i18n, $login, $hist;
146 $zdb = $this->zdb; //globals '(
147 $container = $this->container; //globals '(
148 $i18n = $this->i18n;
149 $login = $this->login;
150 $hist = $this->history;
151 }
152
153 /**
154 * Tear down tests
155 *
156 * @param string $method Calling method
157 *
158 * @return void
159 */
160 public function afterTestMethod($method)
161 {
162 if (TYPE_DB === 'mysql') {
163 $this->array($this->zdb->getWarnings())->isIdenticalTo([]);
164 }
165 }
166
167 /**
168 * Test store models
169 *
170 * @return void
171 */
172 public function testStoreModels()
173 {
174 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
175 $this->string($model->title)->isIdenticalTo('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}');
176
177 $environment = \Slim\Http\Environment::mock(
178 [
179 'REQUEST_METHOD' => 'POST',
180 'REQUEST_URI' => '/models/pdf'
181 ]
182 );
183 $uri = \Slim\Http\Uri::createFromEnvironment($environment);
184 $headers = \Slim\Http\Headers::createFromEnvironment($environment);
185 $cookies = [];
186 $serverParams = $environment->all();
187
188 $body = new \Slim\Http\RequestBody();
189 $body->write(
190 json_encode([
191 'store' => true,
192 'models_id' => \Galette\Entity\PdfModel::INVOICE_MODEL,
193 'model_type' => \Galette\Entity\PdfModel::INVOICE_MODEL,
194 'model_title' => 'DaTitle'
195 ])
196 );
197
198 $request = new \Slim\Http\Request('POST', $uri, $headers, $cookies, $serverParams, $body);
199 $request = $request->withHeader('Content-Type', 'application/json');
200 $response = new \Slim\Http\Response();
201 $controller = new \Galette\Controllers\PdfController($this->container);
202
203 $test_response = $controller->storeModels($request, $response);
204 $this->array($this->flash_data['slimFlash'])->isIdenticalTo([
205 'success_detected' => [
206 'Model has been successfully stored!'
207 ]
208 ]);
209
210 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
211 $this->string($model->title)->isIdenticalTo('DaTitle');
212 }
213 }