Skip to content

Commit

Permalink
Add very basic PHPUnit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
tschoffelen committed Apr 21, 2023
1 parent dddfca2 commit d98e52e
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 15 deletions.
9 changes: 0 additions & 9 deletions .github/lock.yml

This file was deleted.

27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: PHPUnit Tests on Multiple PHP Versions

on: [ push ]

jobs:
tests:
runs-on: ubuntu-latest

strategy:
matrix:
php: [ "7.1", "7.4", "8.0", "8.2" ]

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: zip, openssl

- name: Install Composer dependencies
run: composer install --prefer-dist --no-progress --no-interaction

- name: Run PHPUnit
run: vendor/bin/phpunit
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
composer.lock
/Certificates.p12
/Certificates.p12
.phpunit.result.cache
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
"email": "thomas@includable.com"
}
],
"autoload": {
"psr-4": {
"PKPass\\": "src"
}
},
"require": {
"php": ">=5.6",
"php": ">=7.0",
"ext-zip": "*",
"ext-json": "*",
"ext-openssl": "*"
},
"autoload": {
"psr-4": {
"PKPass\\": "src"
}
"require-dev": {
"phpunit/phpunit": "^9.6"
}
}
65 changes: 65 additions & 0 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use PKPass\PKPass;

final class BasicTest extends TestCase
{
private function validatePass($pass, $expected_files = [])
{
// basic string validation
$this->assertIsString($pass);
$this->assertGreaterThan(100, strlen($pass));
$this->assertStringContainsString('icon.png', $pass);
$this->assertStringContainsString('manifest.json', $pass);

// try to read the ZIP file
$temp_name = tempnam(sys_get_temp_dir(), 'pkpass');
file_put_contents($temp_name, $pass);
$zip = new ZipArchive();
$res = $zip->open($temp_name);
$this->assertTrue($res, 'Invalid ZIP file.');
$this->assertEquals(count($expected_files), $zip->numFiles);

// extract zip to temp dir
$temp_dir = $temp_name . '_dir';
mkdir($temp_dir);
$zip->extractTo($temp_dir);
$zip->close();
echo $temp_dir;
foreach ($expected_files as $file) {
$this->assertFileExists($temp_dir . DIRECTORY_SEPARATOR . $file);
}
}

public function testBasicGeneration()
{
$pass = new PKPass(__DIR__ . '/fixtures/example-certificate.p12', 'password');
$data = [
'description' => 'Demo pass',
'formatVersion' => 1,
'organizationName' => 'Flight Express',
'passTypeIdentifier' => 'pass.com.scholica.flights', // Change this!
'serialNumber' => '12345678',
'teamIdentifier' => 'KN44X8ZLNC', // Change this!
'barcode' => [
'format' => 'PKBarcodeFormatQR',
'message' => 'Flight-GateF12-ID6643679AH7B',
'messageEncoding' => 'iso-8859-1',
],
'backgroundColor' => 'rgb(32,110,247)',
'logoText' => 'Flight info',
'relevantDate' => date('Y-m-d\TH:i:sP')
];
$pass->setData($data);
$pass->addFile(__DIR__ . '/fixtures/icon.png');
$value = $pass->create();

$this->validatePass($value, [
'icon.png',
'manifest.json',
'pass.json',
'signature',
]);
}
}
Binary file added tests/fixtures/example-certificate.p12
Binary file not shown.
Binary file added tests/fixtures/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d98e52e

Please sign in to comment.