dolibarr 18.0.6
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) ---Put here 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->getrights();
37}
38$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
39
40$langs->load("main");
41
42
50class MyObjectTest extends PHPUnit\Framework\TestCase
51{
52 protected $savconf;
53 protected $savuser;
54 protected $savlangs;
55 protected $savdb;
56
63 public function __construct($name = '')
64 {
65 parent::__construct($name);
66
67 //$this->sharedFixture
68 global $conf, $user, $langs, $db;
69 $this->savconf = $conf;
70 $this->savuser = $user;
71 $this->savlangs = $langs;
72 $this->savdb = $db;
73
74 print __METHOD__." db->type=".$db->type." user->id=".$user->id;
75 //print " - db ".$db->db;
76 print "\n";
77 }
78
84 public static function setUpBeforeClass()
85 {
86 global $conf, $user, $langs, $db;
87 $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
88
89 print __METHOD__."\n";
90 }
91
97 protected function setUp()
98 {
99 global $conf, $user, $langs, $db;
100 $conf = $this->savconf;
101 $user = $this->savuser;
102 $langs = $this->savlangs;
103 $db = $this->savdb;
104
105 print __METHOD__."\n";
106 }
107
113 protected function tearDown()
114 {
115 print __METHOD__."\n";
116 }
117
123 public static function tearDownAfterClass()
124 {
125 global $conf, $user, $langs, $db;
126 $db->rollback();
127
128 print __METHOD__."\n";
129 }
130
131
137 public function testSomething()
138 {
139 global $conf, $user, $langs, $db;
140 $conf = $this->savconf;
141 $user = $this->savuser;
142 $langs = $this->savlangs;
143 $db = $this->savdb;
144
145 $result = true;
146
147 print __METHOD__." result=".$result."\n";
148 $this->assertTrue($result);
149
150 return $result;
151 }
152
158 public function testMyObjectCreate()
159 {
160 global $conf, $user, $langs, $db;
161 $conf = $this->savconf;
162 $user = $this->savuser;
163 $langs = $this->savlangs;
164 $db = $this->savdb;
165
166 $localobject = new MyObject($this->savdb);
167 $localobject->initAsSpecimen();
168 $result = $localobject->create($user);
169
170 print __METHOD__." result=".$result."\n";
171 $this->assertLessThan($result, 0);
172
173 return $result;
174 }
175
185 public function testMyObjectDelete($id)
186 {
187 global $conf, $user, $langs, $db;
188 $conf = $this->savconf;
189 $user = $this->savuser;
190 $langs = $this->savlangs;
191 $db = $this->savdb;
192
193 $localobject = new MyObject($this->savdb);
194 $result = $localobject->fetch($id);
195 $result = $localobject->delete($user);
196
197 print __METHOD__." id=".$id." result=".$result."\n";
198 $this->assertLessThan($result, 0);
199 return $result;
200 }
201}
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.