dolibarr/htdocs/modulebuilder/template/test/phpunit/MyObjectTest.php

218 lines
5.0 KiB
PHP
Raw Permalink Normal View History

2017-05-08 21:00:23 +02:00
<?php
/* Copyright (C) 2007-2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
* Copyright (C) ---Replace with your own copyright and developer email---
2017-05-08 21:00:23 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-05-08 21:00:23 +02:00
*/
/**
2020-09-16 03:15:31 +02:00
* \file test/phpunit/MyObjectTest.php
2017-05-08 21:00:23 +02:00
* \ingroup mymodule
2017-05-27 13:46:34 +02:00
* \brief PHPUnit test for MyObject class.
2017-05-08 21:00:23 +02:00
*/
global $conf, $user, $langs, $db;
2020-09-16 03:15:31 +02:00
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
2022-03-28 14:52:46 +02:00
2020-09-16 03:15:31 +02:00
//require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/mymodule/class/myobject.class.php';
if (empty($user->id)) {
print "Load permissions for admin user nb 1\n";
$user->fetch(1);
$user->loadRights();
2020-09-16 03:15:31 +02:00
}
$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
2020-09-16 03:15:31 +02:00
$langs->load("main");
2017-05-08 21:00:23 +02:00
/**
2017-05-27 13:46:34 +02:00
* Class MyObjectTest
2021-06-23 00:58:27 +02:00
*
* @backupGlobals disabled
* @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
* @phan-file-suppress PhanCompatibleVoidTypePHP70
2017-05-08 21:00:23 +02:00
*/
class MyObjectTest extends PHPUnit\Framework\TestCase // @phan-suppress-current-line PhanUndeclaredExtendedClass
2017-05-08 21:00:23 +02:00
{
/**
* @var Conf Saved configuration object
*/
2020-09-16 03:15:31 +02:00
protected $savconf;
/**
* @var User Saved User object
*/
2020-09-16 03:15:31 +02:00
protected $savuser;
/**
* @var Translate Saved translations object (from $langs)
*/
2020-09-16 03:15:31 +02:00
protected $savlangs;
/**
* @var DoliDB Saved database object
*/
2020-09-16 03:15:31 +02:00
protected $savdb;
2017-05-08 21:00:23 +02:00
/**
2020-09-16 03:15:31 +02:00
* Constructor
* We save global variables into local variables
*
* @param string $name Name
2017-05-08 21:00:23 +02:00
*/
public function __construct($name = '')
2017-05-08 21:00:23 +02:00
{
parent::__construct($name); // @phan-suppress-current-line PhanUndeclaredClass
2020-09-16 03:15:31 +02:00
//$this->sharedFixture
global $conf, $user, $langs, $db;
$this->savconf = $conf;
$this->savuser = $user;
$this->savlangs = $langs;
$this->savdb = $db;
2020-09-16 03:15:31 +02:00
print __METHOD__." db->type=".$db->type." user->id=".$user->id;
//print " - db ".$db->db;
print "\n";
2017-05-08 21:00:23 +02:00
}
/**
2020-09-16 03:15:31 +02:00
* Global test setup
2021-06-23 00:58:27 +02:00
*
* @return void No return value
2017-05-08 21:00:23 +02:00
*/
public static function setUpBeforeClass(): void
2017-05-08 21:00:23 +02:00
{
global $conf, $user, $langs, $db;
2020-09-16 03:15:31 +02:00
$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
2017-05-08 21:00:23 +02:00
2020-09-16 03:15:31 +02:00
print __METHOD__."\n";
2017-05-08 21:00:23 +02:00
}
/**
2020-09-16 03:15:31 +02:00
* Unit test setup
2021-06-23 00:58:27 +02:00
*
* @return void No return value
2017-05-08 21:00:23 +02:00
*/
protected function setUp(): void
2017-05-08 21:00:23 +02:00
{
global $conf, $user, $langs, $db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
2020-09-16 03:15:31 +02:00
print __METHOD__."\n";
2017-05-08 21:00:23 +02:00
}
/**
* Unit test teardown
2021-06-23 00:58:27 +02:00
*
* @return void No return value
2017-05-08 21:00:23 +02:00
*/
protected function tearDown(): void
2017-05-08 21:00:23 +02:00
{
2020-09-16 03:15:31 +02:00
print __METHOD__."\n";
2017-05-08 21:00:23 +02:00
}
/**
* Global test teardown
2021-06-23 00:58:27 +02:00
*
* @return void No return value
2017-05-08 21:00:23 +02:00
*/
public static function tearDownAfterClass(): void
2017-05-08 21:00:23 +02:00
{
global $conf, $user, $langs, $db;
2020-09-16 03:15:31 +02:00
$db->rollback();
print __METHOD__."\n";
2017-05-08 21:00:23 +02:00
}
2020-09-16 03:15:31 +02:00
2017-05-08 21:00:23 +02:00
/**
2020-09-16 03:15:31 +02:00
* A sample test
2020-11-25 15:07:26 +01:00
*
2020-09-16 03:15:31 +02:00
* @return bool
* @phan-suppress PhanUndeclaredMethod
2017-05-08 21:00:23 +02:00
*/
2020-09-16 03:15:31 +02:00
public function testSomething()
2017-05-08 21:00:23 +02:00
{
global $conf, $user, $langs, $db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
2020-09-16 03:15:31 +02:00
$result = true;
print __METHOD__." result=".((int) $result)."\n";
2020-09-16 03:15:31 +02:00
$this->assertTrue($result);
return $result;
2017-05-08 21:00:23 +02:00
}
2020-11-25 15:07:26 +01:00
/**
* testMyObjectCreate
*
* @return int
* @phan-suppress PhanUndeclaredMethod
2020-11-25 15:07:26 +01:00
*/
public function testMyObjectCreate()
{
global $conf, $user, $langs, $db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
2020-11-25 15:07:26 +01:00
$localobject = new MyObject($this->savdb);
2020-11-25 15:07:26 +01:00
$localobject->initAsSpecimen();
$result = $localobject->create($user);
2020-11-25 15:07:26 +01:00
print __METHOD__." result=".$result."\n";
$this->assertLessThan($result, 0);
return $result;
}
/**
* testMyObjectDelete
*
* @param int $id Id of object
2020-11-25 18:45:25 +01:00
* @return int
2020-11-25 15:07:26 +01:00
*
* @depends testMyObjectCreate
* The depends says test is run only if previous is ok
* @phan-suppress PhanUndeclaredMethod
2020-11-25 15:07:26 +01:00
*/
public function testMyObjectDelete($id)
{
global $conf, $user, $langs, $db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
$localobject = new MyObject($this->savdb);
$result = $localobject->fetch($id);
$result = $localobject->delete($user);
2020-11-25 15:07:26 +01:00
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0);
return $result;
}
} // @phan-suppress-current-line PhanUndeclaredClass