dolibarr  20.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) 2024 MDW <mdeweerd@users.noreply.github.com>
5  * Copyright (C) ---Put here your own copyright and developer email---
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 
27 global $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';
31 require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
32 require_once dirname(__FILE__).'/../../htdocs/mymodule/class/myobject.class.php';
33 
34 if (empty($user->id)) {
35  print "Load permissions for admin user nb 1\n";
36  $user->fetch(1);
37  $user->getrights();
38 }
39 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
40 
41 $langs->load("main");
42 
43 
51 class MyObjectTest extends PHPUnit\Framework\TestCase
52 {
53  protected $savconf;
54  protected $savuser;
55  protected $savlangs;
56  protected $savdb;
57 
64  public function __construct($name = '')
65  {
66  parent::__construct($name);
67 
68  //$this->sharedFixture
69  global $conf, $user, $langs, $db;
70  $this->savconf = $conf;
71  $this->savuser = $user;
72  $this->savlangs = $langs;
73  $this->savdb = $db;
74 
75  print __METHOD__." db->type=".$db->type." user->id=".$user->id;
76  //print " - db ".$db->db;
77  print "\n";
78  }
79 
85  public static function setUpBeforeClass(): void
86  {
87  global $conf, $user, $langs, $db;
88  $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
89 
90  print __METHOD__."\n";
91  }
92 
98  protected function setUp(): void
99  {
100  global $conf, $user, $langs, $db;
101  $conf = $this->savconf;
102  $user = $this->savuser;
103  $langs = $this->savlangs;
104  $db = $this->savdb;
105 
106  print __METHOD__."\n";
107  }
108 
114  protected function tearDown(): void
115  {
116  print __METHOD__."\n";
117  }
118 
124  public static function tearDownAfterClass(): void
125  {
126  global $conf, $user, $langs, $db;
127  $db->rollback();
128 
129  print __METHOD__."\n";
130  }
131 
132 
138  public function testSomething()
139  {
140  global $conf, $user, $langs, $db;
141  $conf = $this->savconf;
142  $user = $this->savuser;
143  $langs = $this->savlangs;
144  $db = $this->savdb;
145 
146  $result = true;
147 
148  print __METHOD__." result=".$result."\n";
149  $this->assertTrue($result);
150 
151  return $result;
152  }
153 
159  public function testMyObjectCreate()
160  {
161  global $conf, $user, $langs, $db;
162  $conf = $this->savconf;
163  $user = $this->savuser;
164  $langs = $this->savlangs;
165  $db = $this->savdb;
166 
167  $localobject = new MyObject($this->savdb);
168  $localobject->initAsSpecimen();
169  $result = $localobject->create($user);
170 
171  print __METHOD__." result=".$result."\n";
172  $this->assertLessThan($result, 0);
173 
174  return $result;
175  }
176 
186  public function testMyObjectDelete($id)
187  {
188  global $conf, $user, $langs, $db;
189  $conf = $this->savconf;
190  $user = $this->savuser;
191  $langs = $this->savlangs;
192  $db = $this->savdb;
193 
194  $localobject = new MyObject($this->savdb);
195  $result = $localobject->fetch($id);
196  $result = $localobject->delete($user);
197 
198  print __METHOD__." id=".$id." result=".$result."\n";
199  $this->assertLessThan($result, 0);
200  return $result;
201  }
202 }
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.