dolibarr 25.0.0-alpha
MyObjectTest.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
4 * Copyright (C) ---Replace with your own copyright and developer email---
5 * Copyright (C) 2026 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27global $conf, $user, $langs, $db;
28//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
29
30//require_once 'PHPUnit/Autoload.php';
31require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
32require_once dirname(__FILE__).'/../../htdocs/mymodule/class/myobject.class.php';
33
34if (empty($user->id)) {
35 print "Load permissions for admin user nb 1\n";
36 $user->fetch(1);
37 $user->loadRights();
38}
39$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
40
41$langs->load("main");
42
43
52class MyObjectTest extends PHPUnit\Framework\TestCase // @phan-suppress-current-line PhanUndeclaredExtendedClass
53{
54 // Note: Class must not inherit CommonClassTest like other core uit test as it is for a module store into a different dir.
58 protected $savconf;
62 protected $savuser;
66 protected $savlangs;
70 protected $savdb;
74 protected $savmysoc;
75
82 public function __construct($name = '')
83 {
84 parent::__construct($name); // @phan-suppress-current-line PhanUndeclaredClass
85
86 //$this->sharedFixture
87 global $conf, $user, $langs, $db, $mysoc;
88 $this->savconf = $conf;
89 $this->savuser = $user;
90 $this->savlangs = $langs;
91 $this->savdb = $db;
92 $this->savmysoc = $mysoc;
93
94 print __METHOD__." db->type=".$db->type." user->id=".$user->id;
95 //print " - db ".$db->db;
96 print "\n";
97 }
98
104 public static function setUpBeforeClass(): void
105 {
106 global $conf, $user, $langs, $db, $mysoc;
107 $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
108
109 print __METHOD__."\n";
110 }
111
117 protected function setUp(): void
118 {
119 global $conf, $user, $langs, $db, $mysoc;
120 $conf = $this->savconf;
121 $user = $this->savuser;
122 $langs = $this->savlangs;
123 $db = $this->savdb;
124 $mysoc = $this->savmysoc;
125
126 print __METHOD__."\n";
127 }
128
134 protected function tearDown(): void
135 {
136 print __METHOD__."\n";
137 }
138
144 public static function tearDownAfterClass(): void
145 {
146 global $conf, $user, $langs, $db, $mysoc;
147 $db->rollback();
148
149 print __METHOD__."\n";
150 }
151
152
159 public function testSomething()
160 {
161 global $conf, $user, $langs, $db, $mysoc;
162 $conf = $this->savconf;
163 $user = $this->savuser;
164 $langs = $this->savlangs;
165 $db = $this->savdb;
166 $mysoc = $this->savmysoc;
167
168 $result = true;
169
170 print __METHOD__." result=".((int) $result)."\n";
171 $this->assertTrue($result);
172
173 return $result;
174 }
175
182 public function testMyObjectCreate()
183 {
184 global $conf, $user, $langs, $db, $mysoc;
185 $conf = $this->savconf;
186 $user = $this->savuser;
187 $langs = $this->savlangs;
188 $db = $this->savdb;
189 $mysoc = $this->savmysoc;
190
191 $localobject = new MyObject($this->savdb);
192 $localobject->initAsSpecimen();
193 $result = $localobject->create($user);
194
195 print __METHOD__." result=".$result."\n";
196 $this->assertLessThan($result, 0);
197
198 return $result;
199 }
200
211 public function testMyObjectDelete($id)
212 {
213 global $conf, $user, $langs, $db, $mysoc;
214 $conf = $this->savconf;
215 $user = $this->savuser;
216 $langs = $this->savlangs;
217 $db = $this->savdb;
218 $mysoc = $this->savmysoc;
219
220 $localobject = new MyObject($this->savdb);
221 $result = $localobject->fetch($id);
222 $result = $localobject->delete($user);
223
224 print __METHOD__." id=".$id." result=".$result."\n";
225 $this->assertLessThan($result, 0);
226 return $result;
227 }
228} // @phan-suppress-current-line PhanUndeclaredClass
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Class for MyObject.
Class MyObjectTest.
__construct($name='')
Constructor We save global variables into local variables.
testMyObjectCreate()
testMyObjectCreate
static tearDownAfterClass()
Global test teardown.
static setUpBeforeClass()
Global test setup.
setUp()
Unit test setup.
testMyObjectDelete($id)
testMyObjectDelete
tearDown()
Unit test teardown.
testSomething()
A sample test.
global $mysoc
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined: