Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit db2bddd9 authored by Arnau Vàzquez's avatar Arnau Vàzquez
Browse files

Merge branch '16-add-unit-tests' into 'master'

phpUnit test

See merge request !30
parents a8ea24be d89b7f8d
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -97,6 +97,23 @@ I am not sure how much this may help anyway, but this must be used as an extreme

Feel free to use this [simple script](https://github.com/julianxhokaxhiu/LineageOTAUnitTest) made with NodeJS. Instructions are included.

## LineageOTA Unit Testing

You can cerate new `{FILENAME}Test.php` in `tests` folder. Use `vendor/bin/phpunit tests` command in CLI to run the tests.
In every test file, in `setUp()` function, you must set below details for valid test results :

```properties
$this->romType = '{device}'; 
$this->deviceType = '{type}';
$this->incrementalVersion = '{incr}';

{device} - Device name
{type} - Build type
{incr} - Incremental version

```


## How to integrate within your ROM

In order to integrate this REST Server within your ROM you have two possibilities: you can make use of the `build.prop` ( highly suggested ), or you can patch directly the `android_packages_apps_CMUpdater` package ( not suggested ).
+3 −0
Original line number Diff line number Diff line
@@ -57,5 +57,8 @@
  },
  "scripts": {
    "serve": "php -S 0.0.0.0:8000"
  },
  "require-dev": {
    "phpunit/phpunit": "^9"
  }
}
+2 −1
Original line number Diff line number Diff line
@@ -216,4 +216,5 @@
            }
            $this->logger->debug('Total execution  time  of getBuilds in seconds');
        }

}
 No newline at end of file

tests/ApiTest.php

0 → 100755
+81 −0
Original line number Diff line number Diff line
<?php

require_once 'vendor/autoload.php';
use flight\Engine; 

use flight\net\Request;
use flight\net\Router;
use flight\core\Dispatcher;
use PHPUnit\Framework\TestCase;
use \JX\CmOta\CmOta;
use \JX\CmOta\Helpers\Builds;
use Monolog\Logger;

class ApiTest extends TestCase {
 
    /**
     * @var Engine
     */
    private $app;
    private Router $router;
    private Request $request;
    private Dispatcher $dispatcher;

    protected function setUp(): void
    {
        
        $this->router = new Router();
        $this->request = new Request();
        $this->dispatcher = new Dispatcher();

        $this->romType = 'dev';
        $this->deviceType = 'Amber';
        $this->incrementalVersion = '0b971351f3';

        $this->customData = array(
            'params' => array(
              'device' => $this->deviceType,
              'channels' => array(
                $this->romType,
              ),
              'source_incremental' => $this->incrementalVersion,
            ),
        );

        $this->logger = new Logger('main');
        $this->getCmotaInstance();
    }

    public function getCmotaInstance(){
        Flight::register('cmota', '\JX\CmOta\CmOta',array($this->logger));
        return Flight::cmota();
    }
    public function getBuildInstance(){
        Flight::register('builds', '\JX\CmOta\Helpers\Builds',array($this->logger));
        return Flight::builds();
    }

    public function testCheckIfBuildValid() {
        $builds = $this->getBuildInstance();
        $builds->setPostData($this->customData);
        $records = $builds->get();
        $this->assertGreaterThan( 0, sizeof($records),'Builds are not valid!'); 
    }

     public function testCheckIfCorrectAttribute() {
        $attributes = array("timestamp" , "md5sum" , "url");  // You can change attributes here

        $builds = $this->getBuildInstance();
        $builds->setPostData($this->customData);
        $records = $builds->get();
        if(sizeof($records)){
            foreach ($attributes as $attribute) {
                $this->assertArrayHasKey($attribute, $records[0], "Builds doesn't contains '".$attribute."' as key");
            }
        }else{
            $this->assertFalse('Builds');
        }
    }
 

}
 No newline at end of file

tests/BuildTest.php

0 → 100755
+82 −0
Original line number Diff line number Diff line
<?php

require_once 'vendor/autoload.php';
use flight\Engine; 

use flight\net\Request;
use flight\net\Router;
use flight\core\Dispatcher;
use PHPUnit\Framework\TestCase;
use \JX\CmOta\CmOta;
use \JX\CmOta\Helpers\Builds;
use Monolog\Logger;

class BuildTest extends TestCase {
 
    /**
     * @var Engine
     */
    private $app;
    private Router $router;
    private Request $request;
    private Dispatcher $dispatcher;

    protected function setUp(): void
    {
        
        $this->router = new Router();
        $this->request = new Request();
        $this->dispatcher = new Dispatcher();

        $this->romType = 'dev';
        $this->deviceType = 'Amber';
        $this->incrementalVersion = '0b971351f3';

        $this->customData = array(
            'params' => array(
              'device' => $this->deviceType,
              'channels' => array(
                $this->romType,
              ),
              'source_incremental' => $this->incrementalVersion,
            ),
        );

        $this->logger = new Logger('main');
        $this->getCmotaInstance();
    }

    public function getCmotaInstance(){
        Flight::register('cmota', '\JX\CmOta\CmOta',array($this->logger));
        return Flight::cmota();
    }
    public function getBuildInstance(){
        Flight::register('builds', '\JX\CmOta\Helpers\Builds',array($this->logger));
        return Flight::builds();
    }
    
    // newer than (timestamp check)
    public function testCheckTimestamp() { 
        $builds = $this->getBuildInstance();
        $builds->setPostData($this->customData);
        $records = $builds->get();
        $currentTimestamp = strtotime(date('Y-m-d H:i:s'));
         
        foreach ($records as $record) {
           $this->assertGreaterThan($record['timestamp'], $currentTimestamp, 'Build is not new!'); 
        }
        
    }

   // check Android version if it is same or not
    public function testCheckAndroidVersion() { 
        $builds = $this->getBuildInstance();
        $builds->setPostData($this->customData);
        $records = $builds->get();
        $expectedVersion = '0.16';
        foreach ($records as $record) {
            $this->assertEquals($expectedVersion , $record['version'] ,$expectedVersion.' & '.$record['version'] .' Android Versions are different!');
        }
    }

}
 No newline at end of file