diff --git a/README.md b/README.md index 4f69b1374742c105e6d2f9e8b7c94adcae99dd9d..beabd272cb4aa1cc6429d8cff1599fe4d450768f 100644 --- a/README.md +++ b/README.md @@ -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 ). diff --git a/composer.json b/composer.json index 6d66bcec1e42a6aa3bb6493a2ea855f7a0cc45c5..61e635892f2c94d9dce89832019f0de3082ffa75 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 b85773bd3d2904f5472a9b7fbce0a64914ce465e..fb2ce2bbeeff8f5a92207eb647904d7bbfdeb089 100644 --- a/src/Helpers/Builds.php +++ b/src/Helpers/Builds.php @@ -216,4 +216,5 @@ } $this->logger->debug('Total execution time of getBuilds in seconds'); } - } + +} \ No newline at end of file diff --git a/tests/ApiTest.php b/tests/ApiTest.php new file mode 100755 index 0000000000000000000000000000000000000000..07990aad22f0abbe3f396d37cf0f2fb74542696e --- /dev/null +++ b/tests/ApiTest.php @@ -0,0 +1,81 @@ +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 diff --git a/tests/BuildTest.php b/tests/BuildTest.php new file mode 100755 index 0000000000000000000000000000000000000000..71a9b61765867bfd59595ad98a7fda438b65fd9a --- /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, '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