2015-03-21 15:31:11 +01:00
< ? php
/* Copyright ( C ) 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
2023-05-07 14:31:35 +02:00
* Copyright ( C ) 2023 Alexandre Janniaux < alexandre . janniaux @ gmail . com >
2024-11-14 00:16:43 +01:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2015-03-21 15:31:11 +01: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 />.
* or see https :// www . gnu . org /
2015-03-21 15:31:11 +01:00
*/
/**
* \file test / phpunit / MouvementStockTest . php
* \ingroup test
* \brief PHPUnit test
* \remarks To run this script as CLI : phpunit filename . php
*/
global $conf , $user , $langs , $db ;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
//require_once 'PHPUnit/Autoload.php';
require_once dirname ( __FILE__ ) . '/../../htdocs/master.inc.php' ;
require_once dirname ( __FILE__ ) . '/../../htdocs/product/stock/class/mouvementstock.class.php' ;
require_once dirname ( __FILE__ ) . '/../../htdocs/product/stock/class/entrepot.class.php' ;
require_once dirname ( __FILE__ ) . '/../../htdocs/product/class/product.class.php' ;
2024-02-16 23:26:32 +01:00
require_once dirname ( __FILE__ ) . '/CommonClassTest.class.php' ;
2015-03-21 15:31:11 +01:00
2021-01-14 15:09:08 +01:00
if ( empty ( $user -> id )) {
2015-03-21 15:31:11 +01:00
print " Load permissions for admin user nb 1 \n " ;
$user -> fetch ( 1 );
2024-11-14 00:16:43 +01:00
$user -> loadRights ();
2015-03-21 15:31:11 +01:00
}
2024-02-19 15:28:21 +01:00
$conf -> global -> MAIN_DISABLE_ALL_MAILS = 1 ;
2015-03-21 15:31:11 +01:00
/**
* Class for PHPUnit tests
*
* @ backupGlobals disabled
* @ backupStaticAttributes enabled
* @ remarks backupGlobals must be disabled to have db , conf , user and lang not erased .
*/
2024-02-16 23:26:32 +01:00
class MouvementStockTest extends CommonClassTest
2015-03-21 15:31:11 +01:00
{
/**
* Init phpunit tests
*
* @ return void
*/
2022-09-21 17:55:04 +02:00
protected function setUp () : void
2021-01-14 15:09:08 +01:00
{
global $conf , $user , $langs , $db ;
2024-02-19 15:28:21 +01:00
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2015-03-21 15:31:11 +01:00
2023-04-15 03:16:53 +02:00
if ( ! isModEnabled ( 'productbatch' )) {
2023-12-04 11:22:28 +01:00
print " \n " . __METHOD__ . " module Lot/Serial must be enabled. \n " ;
die ( 1 );
2021-01-14 15:09:08 +01:00
}
2020-12-16 14:53:04 +01:00
2015-03-21 15:31:11 +01:00
print __METHOD__ . " \n " ;
2021-01-14 15:09:08 +01:00
}
2024-02-17 15:50:51 +01:00
2021-01-14 15:09:08 +01:00
/**
* testMouvementCreate
*
* @ return MouvementStock
*/
public function testMouvementCreate ()
{
global $conf , $user , $langs , $db ;
2024-02-19 15:28:21 +01:00
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2015-03-21 15:31:11 +01:00
// We create a product for tests
2024-02-19 15:28:21 +01:00
$product0 = new Product ( $db );
2020-12-16 12:26:01 +01:00
$product0 -> initAsSpecimen ();
2024-02-19 15:28:21 +01:00
$product0 -> ref .= ' phpunit 0' ;
$product0 -> label .= ' phpunit 0' ;
2020-12-16 12:26:01 +01:00
$product0 -> status_batch = 1 ;
2024-02-19 15:28:21 +01:00
$product0id = $product0 -> create ( $user );
2020-12-16 12:26:01 +01:00
2023-04-11 19:36:19 +02:00
print __METHOD__ . " product0id= " . $product0id . " \n " ;
$this -> assertGreaterThan ( 0 , $product0id , 'Failed to create product' );
2024-02-19 15:28:21 +01:00
$product1 = new Product ( $db );
2015-03-21 15:31:11 +01:00
$product1 -> initAsSpecimen ();
2024-02-19 15:28:21 +01:00
$product1 -> ref .= ' phpunit 1' ;
$product1 -> label .= ' phpunit 1' ;
$product1id = $product1 -> create ( $user );
2015-03-21 15:31:11 +01:00
2024-02-19 15:28:21 +01:00
$product2 = new Product ( $db );
2015-03-21 15:31:11 +01:00
$product2 -> initAsSpecimen ();
2024-02-19 15:28:21 +01:00
$product2 -> ref .= ' phpunit 2' ;
$product2 -> label .= ' phpunit 2' ;
$product2id = $product2 -> create ( $user );
2015-03-21 15:31:11 +01:00
// We create a product for tests
2024-02-19 15:28:21 +01:00
$warehouse0 = new Entrepot ( $db );
2020-12-16 12:26:01 +01:00
$warehouse0 -> initAsSpecimen ();
2024-02-19 15:28:21 +01:00
$warehouse0 -> label .= ' phpunit 0' ;
$warehouse0 -> description .= ' phpunit 0' ;
2020-12-16 12:26:01 +01:00
$warehouse0 -> statut = 0 ;
2024-02-19 15:28:21 +01:00
$warehouse0id = $warehouse0 -> create ( $user );
2020-12-16 12:26:01 +01:00
2024-02-19 15:28:21 +01:00
$warehouse1 = new Entrepot ( $db );
2015-03-21 15:31:11 +01:00
$warehouse1 -> initAsSpecimen ();
2024-02-19 15:28:21 +01:00
$warehouse1 -> label .= ' phpunit 1' ;
$warehouse1 -> description .= ' phpunit 1' ;
$warehouse1id = $warehouse1 -> create ( $user );
2015-03-21 15:31:11 +01:00
2024-02-19 15:28:21 +01:00
$warehouse2 = new Entrepot ( $db );
2015-03-21 15:31:11 +01:00
$warehouse2 -> initAsSpecimen ();
2024-02-19 15:28:21 +01:00
$warehouse2 -> label .= ' phpunit 2' ;
$warehouse2 -> description .= ' phpunit 2' ;
$warehouse2id = $warehouse2 -> create ( $user );
2015-03-21 15:31:11 +01:00
2024-02-19 15:28:21 +01:00
$localobject = new MouvementStock ( $db );
2015-03-21 15:31:11 +01:00
2020-12-16 12:26:01 +01:00
$datetest1 = dol_mktime ( 0 , 0 , 0 , 1 , 1 , 2000 );
$datetest2 = dol_mktime ( 0 , 0 , 0 , 1 , 2 , 2000 );
2023-04-11 19:36:19 +02:00
// Create an input movement movement (type = 3) with value for eatby date and a lot $datetest1
2024-02-19 15:28:21 +01:00
$result = $localobject -> reception ( $user , $product0id , $warehouse0id , 5 , 999 , 'Movement for unit test with batch' , $datetest1 , $datetest1 , 'anotyetuselotnumberA' , '' , 0 , 'Inventory Code Test with batch' );
2020-12-16 12:26:01 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
2023-04-11 19:36:19 +02:00
$this -> assertGreaterThan ( 0 , $result , 'Failed to create a movement with a lot number ' . $datetest1 . ' for product id=' . $product0id . ' with status_batch=1' );
2020-12-16 12:26:01 +01:00
2024-02-19 15:28:21 +01:00
$result = $localobject -> reception ( $user , $product0id , $warehouse0id , 5 , 999 , 'Movement for unit test with batch' , $datetest1 , $datetest1 , 'anotyetuselotnumberB' , '' , 0 , 'Inventory Code Test with batch' );
2020-12-16 12:26:01 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertGreaterThan ( 0 , $result , 'Test to check we can create a movement a eatby dare different when lot number is different' );
// Create same input movement movement (type = 3) with same lot but a different value of eatby date
2024-02-19 15:28:21 +01:00
$result = $localobject -> reception ( $user , $product0id , $warehouse0id , 5 , 999 , 'Movement for unit test with batch' , $datetest2 , $datetest1 , 'anotyetuselotnumberA' , '' , 0 , 'Inventory Code Test with batch' );
2020-12-16 12:26:01 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertEquals ( - 3 , $result , 'Test to check we can t create a movement for a lot with a different eatby date' );
2021-01-14 15:09:08 +01:00
// Do a list of movement into warehouse 1
2015-03-21 15:31:11 +01:00
2021-01-14 15:09:08 +01:00
// Create an input movement (type = 3) of price 9.9 -> should update PMP to 9.9
2024-02-19 15:28:21 +01:00
$result = $localobject -> reception ( $user , $product1id , $warehouse1id , 10 , 9.9 , 'Movement for unit test 1' , '' , '' , '' , '' , 0 , 'Inventory Code Test' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertGreaterThan ( 0 , $result , 'Return code of 0 was expected for the reception test 1' );
// Create an input movement (type = 3) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
2024-02-19 15:28:21 +01:00
$result = $localobject -> reception ( $user , $product1id , $warehouse1id , 10 , 9.7 , 'Movement for unit test 2' , '' , '' , '' , '' , 0 , 'Inventory Code Test' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertGreaterThan ( 0 , $result );
// Create an output movement (type = 2) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
2024-02-19 15:28:21 +01:00
$result = $localobject -> livraison ( $user , $product1id , $warehouse1id , 5 , 999 , 'Movement for unit test 3' , '' , '' , '' , '' , 0 , 'Inventory Code Test' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertGreaterThan ( 0 , $result );
// Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
2024-02-19 15:28:21 +01:00
$result = $localobject -> _create ( $user , $product1id , $warehouse1id , 1 , 0 , 0 , 'Input from transfer' , 'Transfert X' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertGreaterThan ( 0 , $result );
// Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
2024-02-19 15:28:21 +01:00
$result = $localobject -> _create ( $user , $product1id , $warehouse1id , - 2 , 1 , 0 , 'Output from transfer' , 'Transfert Y' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertGreaterThan ( 0 , $result );
// Do same but into warehouse 2
// Create an input movement (type = 3) of price 9.9 -> should update PMP to 9.9
2024-02-19 15:28:21 +01:00
$result = $localobject -> reception ( $user , $product1id , $warehouse2id , 10 , 9.9 , 'Movement for unit test 1 wh 2' , '' , '' , '' , '' , 0 , 'Inventory Code Test 2' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertGreaterThan ( 0 , $result );
// Create an input movement (type = 3) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
2024-02-19 15:28:21 +01:00
$result = $localobject -> reception ( $user , $product1id , $warehouse2id , 10 , 9.7 , 'Movement for unit test 2 wh 2' , '' , '' , '' , '' , 0 , 'Inventory Code Test 2' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertGreaterThan ( 0 , $result );
// Create an output movement (type = 2) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
2024-02-19 15:28:21 +01:00
$result = $localobject -> livraison ( $user , $product1id , $warehouse2id , 5 , 999 , 'Movement for unit test 3 wh 2' , '' , '' , '' , '' , 0 , 'Inventory Code Test 2' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
$this -> assertGreaterThan ( 0 , $result );
// Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
2024-02-19 15:28:21 +01:00
$result = $localobject -> _create ( $user , $product1id , $warehouse2id , 1 , 0 , 0 , 'Input from transfer wh 2' , 'Transfert X 2' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
2023-04-11 19:36:19 +02:00
$this -> assertGreaterThan ( 0 , $result , 'Test create A' );
2021-01-14 15:09:08 +01:00
// Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
2024-02-19 15:28:21 +01:00
$result = $localobject -> _create ( $user , $product1id , $warehouse2id , - 2 , 1 , 0 , 'Output from transfer wh 2' , 'Transfert Y 2' );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " result= " . $result . " \n " ;
2023-04-11 19:36:19 +02:00
$this -> assertGreaterThan ( 0 , $result , 'Test create B' );
2021-01-14 15:09:08 +01:00
return $localobject ;
}
/**
* testMouvementCheck
*
* @ param MouvementStock $localobject Movement object we created
* @ return MouvementStock
*
* @ depends testMouvementCreate
* The depends says test is run only if previous is ok
*/
public function testMouvementCheck ( $localobject )
{
global $conf , $user , $langs , $db ;
2024-02-19 15:28:21 +01:00
$conf = $this -> savconf ;
$user = $this -> savuser ;
$langs = $this -> savlangs ;
$db = $this -> savdb ;
2015-03-21 15:31:11 +01:00
$productid = $localobject -> product_id ;
$warehouseid = $localobject -> entrepot_id ;
print __METHOD__ . " productid= " . $productid . " \n " ;
$producttotest = new Product ( $db );
$producttotest -> fetch ( $productid );
2021-01-14 15:09:08 +01:00
print __METHOD__ . " producttotest->stock_reel= " . $producttotest -> stock_reel . " \n " ;
$this -> assertEquals ( $producttotest -> stock_reel , 28 ); // 28 is result of stock movement defined into testMouvementCreate
2015-03-21 15:31:11 +01:00
2021-01-14 15:09:08 +01:00
print __METHOD__ . " producttotest->pmp= " . $producttotest -> pmp . " \n " ;
$this -> assertEquals ( $producttotest -> pmp , 9.8 );
2015-03-21 15:31:11 +01:00
2021-01-14 15:09:08 +01:00
return $localobject ;
}
2015-03-21 15:31:11 +01:00
}