From b0509adb9604190bfd7cfb3efeb49917d6af18c2 Mon Sep 17 00:00:00 2001 From: Marcin Michalski Date: Fri, 20 Aug 2021 10:49:03 +0200 Subject: [PATCH 1/4] Bump required php version to 7.2 & phpunit to 8.5 --- .gitignore | 3 +++ README.md | 2 +- composer.json | 6 +++--- test/ParserTest.php | 10 +++++----- test/XmlParserTest.php | 5 ++--- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 8ba392c..dc23338 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ vendor/ # Mac OS .DS_Store examples/extract_folder + +# PHPUnit +.phpunit.result.cache diff --git a/README.md b/README.md index 71b8ad2..b823eef 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ The class can also extract the whole files contained in the APK file to a given ### Requirements -PHP 7.0+ +PHP 7.2+ ### Installation diff --git a/composer.json b/composer.json index a3e5b20..6563a6c 100644 --- a/composer.json +++ b/composer.json @@ -18,14 +18,14 @@ } ], "require": { - "php": ">=7.0", + "php": ">=7.2", "ext-simplexml": "*", "ext-json": "*", "ext-libxml": "*", "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^7.4" + "phpunit/phpunit": "^8.5" }, "repositories": [ { @@ -39,4 +39,4 @@ } }, "minimum-stability": "dev" -} \ No newline at end of file +} diff --git a/test/ParserTest.php b/test/ParserTest.php index 6cfc040..3c65873 100644 --- a/test/ParserTest.php +++ b/test/ParserTest.php @@ -20,7 +20,7 @@ class ParserTest extends \PHPUnit\Framework\TestCase /** * @throws Exception */ - public function setUp() + protected function setUp(): void { $file = __DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'EBHS.apk'; $this->subject = new Parser($file); @@ -44,7 +44,7 @@ public function testPermissions() { $permissions = $this->subject->getManifest()->getPermissions(); - $this->assertEquals(count($permissions), 4); + $this->assertCount(4, $permissions); $this->assertArrayHasKey('INTERNET', $permissions, "INTERNET permission not found!"); $this->assertArrayHasKey('CAMERA', $permissions, "CAMERA permission not found!"); $this->assertArrayHasKey('BLUETOOTH', $permissions, "BLUETOOTH permission not found!"); @@ -75,7 +75,7 @@ public function testIconResources() $application = $this->subject->getManifest()->getApplication(); $resources = $this->subject->getResources($application->getIcon()); - $expected = array('res/drawable-ldpi/ebhs.png', 'res/drawable-mdpi/ebhs.png', 'res/drawable-hdpi/ebhs.png'); + $expected = ['res/drawable-ldpi/ebhs.png', 'res/drawable-mdpi/ebhs.png', 'res/drawable-hdpi/ebhs.png']; $this->assertEquals($resources, $expected); } @@ -90,7 +90,7 @@ public function testIconStream() $file = __DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'ebhs.png'; $expected = file_get_contents($file); - $this->assertTrue(is_resource($stream)); + $this->assertIsResource($stream); $this->assertEquals(base64_encode($icon), base64_encode($expected)); } @@ -104,7 +104,7 @@ public function testLabelResources() $application = $this->subject->getManifest()->getApplication(); $resources = $this->subject->getResources($application->getLabel()); - $expected = array('EBHS'); + $expected = ['EBHS']; $this->assertEquals($resources, $expected); } } diff --git a/test/XmlParserTest.php b/test/XmlParserTest.php index a35b453..6f41e38 100644 --- a/test/XmlParserTest.php +++ b/test/XmlParserTest.php @@ -5,9 +5,6 @@ */ class XmlParserTest extends \PHPUnit\Framework\TestCase { - /** - * @expectedException ApkParser\Exceptions\XmlParserException - */ public function testXmlObject() { $mock = $this->getMockBuilder('ApkParser\XmlParser') @@ -18,6 +15,8 @@ public function testXmlObject() $file = __DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'invalid.xml'; $mock->expects($this->once())->method('getXmlString')->will($this->returnValue(file_get_contents($file))); + $this->expectException(\ApkParser\Exceptions\XmlParserException::class); + $mock->getXmlObject(); } } From f9b7edfbc202c7e95a91ab659eeec33585953012 Mon Sep 17 00:00:00 2001 From: Marcin Michalski Date: Fri, 20 Aug 2021 10:52:40 +0200 Subject: [PATCH 2/4] Fix parser tests --- lib/ApkParser/Config.php | 6 +++--- lib/ApkParser/Parser.php | 4 ++-- test/ParserTest.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ApkParser/Config.php b/lib/ApkParser/Config.php index 20eb225..a23cacd 100644 --- a/lib/ApkParser/Config.php +++ b/lib/ApkParser/Config.php @@ -27,13 +27,13 @@ class Config /** * @param array $config */ - public function __construct(array $config = array()) + public function __construct(array $config = []) { - $this->config = array_merge(array( + $this->config = array_merge([ 'tmp_path' => sys_get_temp_dir(), 'jar_path' => __DIR__ . '/Dex/dedexer.jar', 'manifest_only' => true - ), $config); + ], $config); } /** diff --git a/lib/ApkParser/Parser.php b/lib/ApkParser/Parser.php index 10c2f16..be00ecf 100644 --- a/lib/ApkParser/Parser.php +++ b/lib/ApkParser/Parser.php @@ -25,7 +25,7 @@ class Parser * @param array $config * @throws \Exception */ - public function __construct($apkFile, array $config = array()) + public function __construct($apkFile, array $config = []) { $this->config = new Config($config); $this->apk = new Archive($apkFile); @@ -70,7 +70,7 @@ public function getResources($key) { return is_null($this->resources) ? false : $this->resources->getResources($key); } - + /** Get all resources as an array */ diff --git a/test/ParserTest.php b/test/ParserTest.php index 3c65873..4130380 100644 --- a/test/ParserTest.php +++ b/test/ParserTest.php @@ -23,7 +23,7 @@ class ParserTest extends \PHPUnit\Framework\TestCase protected function setUp(): void { $file = __DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'EBHS.apk'; - $this->subject = new Parser($file); + $this->subject = new Parser($file, ['manifest_only' => false]); } /** From 2f0417ccd9dd1d37cedb6cd11b533511324c7d84 Mon Sep 17 00:00:00 2001 From: Marcin Michalski Date: Fri, 20 Aug 2021 10:57:02 +0200 Subject: [PATCH 3/4] Replace travis with github actions --- .github/workflows/tests.yml | 40 +++++++++++++++++++++++++++++++++++++ .travis.yml | 17 ---------------- composer.json | 3 +++ 3 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/tests.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..e6948ac --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,40 @@ +name: tests + +on: [push, pull_request] + +jobs: + build: + strategy: + matrix: + php-version: + - '7.2' + - '7.3' + - '7.4' + - '8.0' + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: "Setup PHP" + uses: "shivammathur/setup-php@v2" + with: + php-version: "${{ matrix.php-version }}" + + - name: Get composer cache directory + id: composer-cache + run: | + echo "::set-output name=dir::$(composer config cache-files-dir)" + - uses: actions/cache@v2 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.php-version }}-composer- + + - name: Install dependencies + run: composer install + + - name: Run tests + run: composer tests diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 15695fa..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -# see http://about.travis-ci.org/docs/user/languages/php/ for more hints -language: php - -php: - - 7.0 - - 7.1 - - 7.2 - - 7.3 - -before_script: - - curl -s http://getcomposer.org/installer | php - - php composer.phar install --dev - -script: vendor/bin/phpunit --configuration phpunit.xml --coverage-text - -notifications: - email: false diff --git a/composer.json b/composer.json index 6563a6c..6dc0116 100644 --- a/composer.json +++ b/composer.json @@ -38,5 +38,8 @@ "ApkParser": "lib" } }, + "scripts": { + "tests": "@php phpunit --configuration=phpunit.xml" + }, "minimum-stability": "dev" } From 3ac5662885f32ae4e703e188e878adfd371984e3 Mon Sep 17 00:00:00 2001 From: Marcin Michalski Date: Fri, 20 Aug 2021 11:03:56 +0200 Subject: [PATCH 4/4] Ensure resources key exists --- lib/ApkParser/ResourcesParser.php | 9 +++++++-- test/ParserTest.php | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/ApkParser/ResourcesParser.php b/lib/ApkParser/ResourcesParser.php index db61f5c..ba4229c 100644 --- a/lib/ApkParser/ResourcesParser.php +++ b/lib/ApkParser/ResourcesParser.php @@ -51,9 +51,14 @@ public function __construct(SeekableStream $stream) */ public function getResources($key) { - return $this->resources[strtolower($key)]; + $key = strtolower($key); + if (array_key_exists($key, $this->resources)) { + return $this->resources[$key]; + } + + return false; } - + /** All resources */ diff --git a/test/ParserTest.php b/test/ParserTest.php index 4130380..58a2c1b 100644 --- a/test/ParserTest.php +++ b/test/ParserTest.php @@ -79,6 +79,11 @@ public function testIconResources() $this->assertEquals($resources, $expected); } + public function testGetMissingResources() + { + $this->assertFalse($this->subject->getResources('missing')); + } + /** * @throws \PHPUnit\Framework\ExpectationFailedException * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException