dolibarr  16.0.5
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) ---Put here your own copyright and developer email---
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 global $conf, $user, $langs, $db;
26 //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
27 
28 //require_once 'PHPUnit/Autoload.php';
29 require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
30 require_once dirname(__FILE__).'/../../htdocs/mymodule/class/myobject.class.php';
31 
32 if (empty($user->id)) {
33  print "Load permissions for admin user nb 1\n";
34  $user->fetch(1);
35  $user->getrights();
36 }
37 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
38 
39 $langs->load("main");
40 
41 
49 class MyObjectTest extends PHPUnit\Framework\TestCase
50 {
51  protected $savconf;
52  protected $savuser;
53  protected $savlangs;
54  protected $savdb;
55 
62  public function __construct()
63  {
64  parent::__construct();
65 
66  //$this->sharedFixture
67  global $conf, $user, $langs, $db;
68  $this->savconf = $conf;
69  $this->savuser = $user;
70  $this->savlangs = $langs;
71  $this->savdb = $db;
72 
73  print __METHOD__." db->type=".$db->type." user->id=".$user->id;
74  //print " - db ".$db->db;
75  print "\n";
76  }
77 
83  public static function setUpBeforeClass()
84  {
85  global $conf, $user, $langs, $db;
86  $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
87 
88  print __METHOD__."\n";
89  }
90 
96  protected function setUp()
97  {
98  global $conf, $user, $langs, $db;
99  $conf = $this->savconf;
100  $user = $this->savuser;
101  $langs = $this->savlangs;
102  $db = $this->savdb;
103 
104  print __METHOD__."\n";
105  }
106 
112  protected function tearDown()
113  {
114  print __METHOD__."\n";
115  }
116 
122  public static function tearDownAfterClass()
123  {
124  global $conf, $user, $langs, $db;
125  $db->rollback();
126 
127  print __METHOD__."\n";
128  }
129 
130 
136  public function testSomething()
137  {
138  global $conf, $user, $langs, $db;
139  $conf = $this->savconf;
140  $user = $this->savuser;
141  $langs = $this->savlangs;
142  $db = $this->savdb;
143 
144  $result = true;
145 
146  print __METHOD__." result=".$result."\n";
147  $this->assertTrue($result);
148 
149  return $result;
150  }
151 
157  public function testMyObjectCreate()
158  {
159  global $conf, $user, $langs, $db;
160  $conf = $this->savconf;
161  $user = $this->savuser;
162  $langs = $this->savlangs;
163  $db = $this->savdb;
164 
165  $localobject = new MyObject($this->savdb);
166  $localobject->initAsSpecimen();
167  $result = $localobject->create($user);
168 
169  print __METHOD__." result=".$result."\n";
170  $this->assertLessThan($result, 0);
171 
172  return $result;
173  }
174 
184  public function testMyObjectDelete($id)
185  {
186  global $conf, $user, $langs, $db;
187  $conf = $this->savconf;
188  $user = $this->savuser;
189  $langs = $this->savlangs;
190  $db = $this->savdb;
191 
192  $localobject = new MyObject($this->savdb);
193  $result = $localobject->fetch($id);
194  $result = $localobject->delete($user);
195 
196  print __METHOD__." id=".$id." result=".$result."\n";
197  $this->assertLessThan($result, 0);
198  return $result;
199  }
200 }
MyObjectTest\testMyObjectDelete
testMyObjectDelete($id)
testMyObjectDelete
Definition: MyObjectTest.php:184
MyObjectTest\setUpBeforeClass
static setUpBeforeClass()
Global test setup.
Definition: MyObjectTest.php:83
MyObjectTest\setUp
setUp()
Unit test setup.
Definition: MyObjectTest.php:96
MyObjectTest\tearDownAfterClass
static tearDownAfterClass()
Global test teardown.
Definition: MyObjectTest.php:122
MyObjectTest\testSomething
testSomething()
A sample test.
Definition: MyObjectTest.php:136
MyObject
Class for MyObject.
Definition: myobject.class.php:33
MyObjectTest
Class MyObjectTest.
Definition: MyObjectTest.php:49
MyObjectTest\__construct
__construct()
Constructor We save global variables into local variables.
Definition: MyObjectTest.php:62
MyObjectTest\testMyObjectCreate
testMyObjectCreate()
testMyObjectCreate
Definition: MyObjectTest.php:157
MyObjectTest\tearDown
tearDown()
Unit test teardown.
Definition: MyObjectTest.php:112