dolibarr 21.0.0-beta
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 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26global $conf, $user, $langs, $db;
27//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
28
29//require_once 'PHPUnit/Autoload.php';
30require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
31require_once dirname(__FILE__).'/../../htdocs/mymodule/class/myobject.class.php';
32
33if (empty($user->id)) {
34 print "Load permissions for admin user nb 1\n";
35 $user->fetch(1);
36 $user->loadRights();
37}
38$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
39
40$langs->load("main");
41
42
51class MyObjectTest extends PHPUnit\Framework\TestCase // @phan-suppress-current-line PhanUndeclaredExtendedClass
52{
56 protected $savconf;
60 protected $savuser;
64 protected $savlangs;
68 protected $savdb;
69
76 public function __construct($name = '')
77 {
78 parent::__construct($name); // @phan-suppress-current-line PhanUndeclaredClass
79
80 //$this->sharedFixture
81 global $conf, $user, $langs, $db;
82 $this->savconf = $conf;
83 $this->savuser = $user;
84 $this->savlangs = $langs;
85 $this->savdb = $db;
86
87 print __METHOD__." db->type=".$db->type." user->id=".$user->id;
88 //print " - db ".$db->db;
89 print "\n";
90 }
91
97 public static function setUpBeforeClass(): void
98 {
99 global $conf, $user, $langs, $db;
100 $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
101
102 print __METHOD__."\n";
103 }
104
110 protected function setUp(): void
111 {
112 global $conf, $user, $langs, $db;
113 $conf = $this->savconf;
114 $user = $this->savuser;
115 $langs = $this->savlangs;
116 $db = $this->savdb;
117
118 print __METHOD__."\n";
119 }
120
126 protected function tearDown(): void
127 {
128 print __METHOD__."\n";
129 }
130
136 public static function tearDownAfterClass(): void
137 {
138 global $conf, $user, $langs, $db;
139 $db->rollback();
140
141 print __METHOD__."\n";
142 }
143
144
151 public function testSomething()
152 {
153 global $conf, $user, $langs, $db;
154 $conf = $this->savconf;
155 $user = $this->savuser;
156 $langs = $this->savlangs;
157 $db = $this->savdb;
158
159 $result = true;
160
161 print __METHOD__." result=".((int) $result)."\n";
162 $this->assertTrue($result);
163
164 return $result;
165 }
166
173 public function testMyObjectCreate()
174 {
175 global $conf, $user, $langs, $db;
176 $conf = $this->savconf;
177 $user = $this->savuser;
178 $langs = $this->savlangs;
179 $db = $this->savdb;
180
181 $localobject = new MyObject($this->savdb);
182 $localobject->initAsSpecimen();
183 $result = $localobject->create($user);
184
185 print __METHOD__." result=".$result."\n";
186 $this->assertLessThan($result, 0);
187
188 return $result;
189 }
190
201 public function testMyObjectDelete($id)
202 {
203 global $conf, $user, $langs, $db;
204 $conf = $this->savconf;
205 $user = $this->savuser;
206 $langs = $this->savlangs;
207 $db = $this->savdb;
208
209 $localobject = new MyObject($this->savdb);
210 $result = $localobject->fetch($id);
211 $result = $localobject->delete($user);
212
213 print __METHOD__." id=".$id." result=".$result."\n";
214 $this->assertLessThan($result, 0);
215 return $result;
216 }
217} // @phan-suppress-current-line PhanUndeclaredClass
$id
Definition account.php:48
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 $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79