Skip to content
Snippets Groups Projects
HistoryControllerTest.php 3.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    /**
    * Copyright Maarch since 2008 under licence GPLv3.
    * See LICENCE.txt file at the root folder for more details.
    * This file is part of Maarch software.
    *
    */
    
    use PHPUnit\Framework\TestCase;
    
    Vinciane's avatar
    Vinciane committed
    use SrcCore\models\DatabaseModel;
    
    
    class HistoryControllerTest extends TestCase
    {
        public function testGetHistoryByUserId()
        {
            $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
            $request     = \Slim\Http\Request::createFromEnvironment($environment);
            $history     = new \History\controllers\HistoryController();
    
    
    Damien's avatar
    Damien committed
            $currentUser = \User\models\UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]);
    
            $response = $history->getByUserId($request, new \Slim\Http\Response(), ['userSerialId' => $currentUser['id']]);
    
    
            $responseBody = json_decode((string)$response->getBody());
    
    
            $this->assertNotEmpty($responseBody->histories);
    
        }
    
        public function testGetHistory()
        {
    
            $history     = new \History\controllers\HistoryController();
    
            //  GET
    
            $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
            $request     = \Slim\Http\Request::createFromEnvironment($environment);
    
    
            $aArgs = [
    
    Florian Azizian's avatar
    Florian Azizian committed
                'startDate' => date('Y-m-d H:i:s', 1521100000),
                'endDate'   => date('Y-m-d H:i:s', time())
    
            ];
            $fullRequest = $request->withQueryParams($aArgs);
    
            $response = $history->get($fullRequest, new \Slim\Http\Response());
    
            $responseBody = json_decode((string)$response->getBody(), true);
    
            $this->assertInternalType('array', $responseBody['history']);
            $this->assertNotEmpty($responseBody['history']);
    
        public function testGetBatchHistory()
    
            $batchHistory     = new \History\controllers\BatchHistoryController();
    
            //  GET
            $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
            $request     = \Slim\Http\Request::createFromEnvironment($environment);
    
            $aArgs = [
    
    Florian Azizian's avatar
    Florian Azizian committed
                'startDate' => date('Y-m-d H:i:s', 1521100000),
                'endDate'   => date('Y-m-d H:i:s', time())
    
            ];
            $fullRequest = $request->withQueryParams($aArgs);
    
            $response = $batchHistory->get($fullRequest, new \Slim\Http\Response());
    
            $responseBody = json_decode((string)$response->getBody());
    
    
            $this->assertInternalType('array', $responseBody->batchHistories);
            $this->assertInternalType('bool', $responseBody->limitExceeded);
            $this->assertNotNull($responseBody->batchHistories);
    
    Vinciane's avatar
    Vinciane committed
    
    
        public function testRealDelete()
        {
            $aResId = DatabaseModel::select([
    
    Vinciane's avatar
    Vinciane committed
                'select'    => ['res_id'],
                'table'     => ['res_letterbox'],
    
                'where'     => ['subject like ?','typist = ?', 'dest_user = ?'],
    
                'data'      => ['%Superman is alive - PHP unit', 19, 'bbain'],
    
    Vinciane's avatar
    Vinciane committed
                'order_by'  => ['res_id DESC']
    
    Vinciane's avatar
    Vinciane committed
            ]);
    
    
    Florian Azizian's avatar
    Florian Azizian committed
            $aNewResId = array_column($aResId, 'res_id');
    
    Vinciane's avatar
    Vinciane committed
            //  REAL DELETE
            \SrcCore\models\DatabaseModel::delete([
                'table' => 'res_letterbox',
    
                'where' => ['res_id in (?)'],
                'data'  => [$aNewResId]
    
    Vinciane's avatar
    Vinciane committed
            ]);
    
            //  READ
    
            foreach ($aNewResId as $resId) {
    
                $res = \Resource\models\ResModel::getById(['resId' => $resId, 'select' => ['*']]);
    
                $this->assertInternalType('array', $res);
                $this->assertEmpty($res);
            }
    
    Vinciane's avatar
    Vinciane committed
        }