From 820eab81b182930b439e62c06ec09e43d4e00678 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Thu, 24 Feb 2022 22:54:07 +0530 Subject: [PATCH 1/9] phpUnit test --- composer.json | 3 +++ src/Helpers/Builds.php | 19 +++++++++++++ tests/ApiTest.php | 60 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100755 tests/ApiTest.php diff --git a/composer.json b/composer.json index 6d66bce..61e6358 100644 --- a/composer.json +++ b/composer.json @@ -57,5 +57,8 @@ }, "scripts": { "serve": "php -S 0.0.0.0:8000" + }, + "require-dev": { + "phpunit/phpunit": "^9" } } diff --git a/src/Helpers/Builds.php b/src/Helpers/Builds.php index b85773b..0458838 100644 --- a/src/Helpers/Builds.php +++ b/src/Helpers/Builds.php @@ -216,4 +216,23 @@ } $this->logger->debug('Total execution time of getBuilds in seconds'); } + + public function checkPath($postData) + { + + $params = $postData['params']; + + $device = isset($params['device']) ? $params['device'] : ''; + $channels = isset($params['channels']) ? $params['channels'][0] : ''; + + // Get physical paths of where the files resides + $path = Flight::cfg()->get('realBasePath') . '/builds/full'; + + $dir = $path . '/' . $channels . '/' . $device; + if(is_dir($dir)){ + return TRUE; + }else{ + return FALSE; + } + } } diff --git a/tests/ApiTest.php b/tests/ApiTest.php new file mode 100755 index 0000000..927eb58 --- /dev/null +++ b/tests/ApiTest.php @@ -0,0 +1,60 @@ +romType = 'dev'; + $this->deviceType = 'jalebi'; + $this->incrementalVersion = '0b971351f3'; + + // $this->app = new Engine(); + // Flight::register('cmota', '\JX\CmOta'); + // $user = Flight::cmota('false'); + } + + public function testCheckPostdata() { + + + $customData = array( + 'params' => array( + 'device' => $this->deviceType, + 'channels' => array( + $this->romType, + ), + 'source_incremental' => $this->incrementalVersion, + ), + ); + + $build = new Builds(''); + Builds::checkPath($customData); + + + + // $this->app->register('reg1', '\JX\CmOta\Helpers\Builds', ['Bob']); + // $builds = $this->app->reg1(); + + + $this->assertTrue(true); + + + } +} \ No newline at end of file -- GitLab From 23f07a3bb6d2ae572f22e76149d9477778f16b02 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 25 Feb 2022 20:21:38 +0530 Subject: [PATCH 2/9] Changes in apitest --- src/Helpers/Builds.php | 20 +------------ tests/ApiTest.php | 68 +++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/Helpers/Builds.php b/src/Helpers/Builds.php index 0458838..fb2ce2b 100644 --- a/src/Helpers/Builds.php +++ b/src/Helpers/Builds.php @@ -217,22 +217,4 @@ $this->logger->debug('Total execution time of getBuilds in seconds'); } - public function checkPath($postData) - { - - $params = $postData['params']; - - $device = isset($params['device']) ? $params['device'] : ''; - $channels = isset($params['channels']) ? $params['channels'][0] : ''; - - // Get physical paths of where the files resides - $path = Flight::cfg()->get('realBasePath') . '/builds/full'; - - $dir = $path . '/' . $channels . '/' . $device; - if(is_dir($dir)){ - return TRUE; - }else{ - return FALSE; - } - } - } +} \ No newline at end of file diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 927eb58..79b85d3 100755 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -3,38 +3,36 @@ 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; -use \JX\CmOta\Helpers\Build; +use \JX\CmOta\CmOta; use \JX\CmOta\Helpers\Builds; +use Monolog\Logger; class ApiTest extends TestCase { -// a build returned is an upgrade(version is higher and it is a newer build) -// a build returned follows the allowed migration paths -// a build returned contains all correct attributes - -/** + /** * @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 = 'jalebi'; $this->incrementalVersion = '0b971351f3'; - // $this->app = new Engine(); - // Flight::register('cmota', '\JX\CmOta'); - // $user = Flight::cmota('false'); - } - - public function testCheckPostdata() { - - - $customData = array( + $this->customData = array( 'params' => array( 'device' => $this->deviceType, 'channels' => array( @@ -43,18 +41,40 @@ class ApiTest extends TestCase { 'source_incremental' => $this->incrementalVersion, ), ); - - $build = new Builds(''); - Builds::checkPath($customData); - + $this->logger = new Logger('main'); + $this->getCmotaInstance(); + } - // $this->app->register('reg1', '\JX\CmOta\Helpers\Builds', ['Bob']); - // $builds = $this->app->reg1(); + 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(); + } - - $this->assertTrue(true); + 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() { + $key = "timestamp"; + $builds = $this->getBuildInstance(); + $builds->setPostData($this->customData); + $records = $builds->get(); + if(sizeof($records)){ + $this->assertArrayHasKey($key, $records[0], "Builds doesn't contains '".$key."' as key"); + }else{ + $this->assertFalse('Builds'); + } + } + + } \ No newline at end of file -- GitLab From c505a8e823d611480e29f7a2163c2ba753e3ee6d Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 28 Feb 2022 14:40:54 +0530 Subject: [PATCH 3/9] added instructions in README file --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 4f69b13..ec74adb 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,10 @@ 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 to run the tests. + ## 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 ). -- GitLab From f35740b3fa84c3fc303c57aafbd39ff62492f399 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 28 Feb 2022 14:47:08 +0530 Subject: [PATCH 4/9] added instructions in readme fike --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ec74adb..15a143b 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ Feel free to use this [simple script](https://github.com/julianxhokaxhiu/Lineage ## LineageOTA Unit Testing -You can cerate new `{FILENAME}Test.php` in `tests` folder. use `vendor/bin/phpunit tests` command to run the tests. +You can cerate new `{FILENAME}Test.php` in `tests` folder. use `vendor/bin/phpunit tests` command to run the tests. ## How to integrate within your ROM -- GitLab From 649cf8d20746e60d634cdd586a385afff928a66a Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 28 Feb 2022 17:21:44 +0530 Subject: [PATCH 5/9] Change logic onn checkattr method --- tests/ApiTest.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 79b85d3..07990aa 100755 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -29,7 +29,7 @@ class ApiTest extends TestCase { $this->dispatcher = new Dispatcher(); $this->romType = 'dev'; - $this->deviceType = 'jalebi'; + $this->deviceType = 'Amber'; $this->incrementalVersion = '0b971351f3'; $this->customData = array( @@ -62,18 +62,19 @@ class ApiTest extends TestCase { $this->assertGreaterThan( 0, sizeof($records),'Builds are not valid!'); } - public function testCheckIfCorrectAttribute() { - $key = "timestamp"; + 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)){ - $this->assertArrayHasKey($key, $records[0], "Builds doesn't contains '".$key."' as key"); + foreach ($attributes as $attribute) { + $this->assertArrayHasKey($attribute, $records[0], "Builds doesn't contains '".$attribute."' as key"); + } }else{ $this->assertFalse('Builds'); } - } -- GitLab From 6359c5101ca6a61d53d9ee82a1270772a253114a Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Tue, 1 Mar 2022 00:01:24 +0530 Subject: [PATCH 6/9] Added BuildTest file to perform test-cases on Build results --- tests/BuildTest.php | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 tests/BuildTest.php diff --git a/tests/BuildTest.php b/tests/BuildTest.php new file mode 100755 index 0000000..490b0de --- /dev/null +++ b/tests/BuildTest.php @@ -0,0 +1,82 @@ +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, 'Builds are not valid!'); + } + + } + + // 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 -- GitLab From aaf9daed344670e4d83e349965db579fb33f48f2 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Tue, 1 Mar 2022 00:02:04 +0530 Subject: [PATCH 7/9] Added BuildTest file to perform test-cases on Build results --- tests/BuildTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/BuildTest.php b/tests/BuildTest.php index 490b0de..71a9b61 100755 --- a/tests/BuildTest.php +++ b/tests/BuildTest.php @@ -63,7 +63,7 @@ class BuildTest extends TestCase { $currentTimestamp = strtotime(date('Y-m-d H:i:s')); foreach ($records as $record) { - $this->assertGreaterThan($record['timestamp'], $currentTimestamp, 'Builds are not valid!'); + $this->assertGreaterThan($record['timestamp'], $currentTimestamp, 'Build is not new!'); } } -- GitLab From abba2849f0baa39ebe5fa0456d08aec40a0f6cb1 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Tue, 1 Mar 2022 20:23:43 +0530 Subject: [PATCH 8/9] updated readme file --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 15a143b..4321efd 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,18 @@ Feel free to use this [simple script](https://github.com/julianxhokaxhiu/Lineage ## LineageOTA Unit Testing You can cerate new `{FILENAME}Test.php` in `tests` folder. use `vendor/bin/phpunit tests` command 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 -- GitLab From 0102702fce7b0fd536dcb4acedd9517d4ecbd364 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Tue, 1 Mar 2022 20:25:42 +0530 Subject: [PATCH 9/9] updated readme file --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4321efd..beabd27 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ Feel free to use this [simple script](https://github.com/julianxhokaxhiu/Lineage ## LineageOTA Unit Testing -You can cerate new `{FILENAME}Test.php` in `tests` folder. use `vendor/bin/phpunit tests` command to run the tests. +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 @@ -113,6 +113,7 @@ $this->incrementalVersion = '{incr}'; ``` + ## 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 ). -- GitLab