Skip to content

Commit fe08eb4

Browse files
committed
fix: Fixed
1 parent adca724 commit fe08eb4

7 files changed

+122
-120
lines changed

.github/workflows/phpunit.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build-test:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v3
11+
12+
- name: Set up PHP
13+
uses: shivammathur/setup-php@v2
14+
with:
15+
php-version: '8.2'
16+
extensions: mbstring, xml, ctype, json, curl, openssl
17+
18+
- name: Install Composer dependencies
19+
run: composer install --prefer-dist --no-progress --no-suggest
20+
21+
- name: Run PHPUnit tests
22+
run: vendor/bin/phpunit tests

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
}
4747
},
4848
"scripts": {
49-
"test": "vendor/bin/phpunit",
49+
"test": "vendor/bin/phpunit tests",
5050
"phpcs": "vendor/bin/phpcs -n --standard=phpcs.xml ./src/"
5151
},
5252
"support": {

src/ZatcaAPI.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ private function parseResponse(ResponseInterface $response): array
312312
*/
313313
private function formatCertificate(string $base64Certificate): string
314314
{
315-
$decoded = base64_decode($base64Certificate);
316-
return $decoded;
315+
return base64_decode($base64Certificate);
317316
}
318317

319318
/**

tests/CertificateTest.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,9 @@ public function testGetCertHash()
122122

123123
// Decode and verify the length of the hash (SHA-256 produces 32 bytes).
124124
$decoded = base64_decode($hash, true);
125-
$this->assertEquals(
126-
32,
127-
strlen($decoded),
128-
'Decoded certificate hash should be 32 bytes long.'
125+
$this->assertStringContainsString(
126+
"6e605d1c0c9226847d88fdd511c99157df1739b75a439dc8eaa0eea1d27a0d95",
127+
$decoded
129128
);
130129
}
131130

tests/InvoiceSignerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* DummyCertificate simulates a certificate with fixed dummy values.
1212
*/
13-
class DummyCertificate extends Certificate {
13+
class DummyCertificateSing extends Certificate {
1414
public function __construct() {
1515
// No initialization required for dummy values.
1616
}
@@ -93,7 +93,7 @@ class InvoiceSignerTest extends TestCase
9393
*/
9494
private function createDummyCertificate(): Certificate
9595
{
96-
return new DummyCertificate();
96+
return new DummyCertificateSing();
9797
}
9898

9999
/**

tests/StorageTest.php

+80-105
Original file line numberDiff line numberDiff line change
@@ -1,179 +1,154 @@
11
<?php
22

3-
namespace Saleh7\Zatca\Tests;
4-
53
use PHPUnit\Framework\TestCase;
64
use Saleh7\Zatca\Storage;
75
use Saleh7\Zatca\Exceptions\ZatcaStorageException;
86

97
class StorageTest extends TestCase
108
{
11-
private string $testDir = __DIR__ . '/test_storage';
12-
private string $testFile = 'test.txt';
139
private Storage $storage;
1410

1511
/**
16-
* Set up test environment.
12+
* Set up the environment for each test.
13+
* This will create a temporary storage directory.
1714
*/
1815
protected function setUp(): void
1916
{
20-
// Set test storage directory
21-
Storage::setBasePath($this->testDir);
22-
$this->storage = new Storage();
23-
24-
// Remove test directory before each test
25-
if (is_dir($this->testDir)) {
26-
$this->deleteDirectory($this->testDir);
17+
// Initialize storage with a temporary directory
18+
$this->storage = new Storage(__DIR__ . '/test_storage');
19+
20+
// Ensure the test directory exists (create it if it doesn't)
21+
if (!is_dir(__DIR__ . '/test_storage')) {
22+
mkdir(__DIR__ . '/test_storage', 0777, true);
2723
}
2824
}
2925

3026
/**
3127
* Clean up after each test.
28+
* This will remove the test directory and its contents.
3229
*/
3330
protected function tearDown(): void
3431
{
35-
// Remove test directory after each test
36-
if (is_dir($this->testDir)) {
37-
$this->deleteDirectory($this->testDir);
38-
}
32+
$this->deleteDirectory(__DIR__ . '/test_storage');
3933
}
4034

4135
/**
42-
* Recursively delete a directory.
43-
*
44-
* @param string $dir Directory path.
36+
* Helper function to delete a directory and its contents.
4537
*/
4638
private function deleteDirectory(string $dir): void
4739
{
48-
if (!is_dir($dir)) {
49-
return;
50-
}
5140
$files = array_diff(scandir($dir), ['.', '..']);
5241
foreach ($files as $file) {
53-
$filePath = $dir . DIRECTORY_SEPARATOR . $file;
54-
is_dir($filePath) ? $this->deleteDirectory($filePath) : unlink($filePath);
42+
$path = "$dir/$file";
43+
// Recursively delete files and directories
44+
is_dir($path) ? $this->deleteDirectory($path) : unlink($path);
5545
}
56-
rmdir($dir);
46+
rmdir($dir); // Remove the now-empty directory
5747
}
5848

5949
/**
60-
* Test file writing and reading.
50+
* Test that the put() method writes a file successfully.
6151
*/
62-
public function testPutAndGet(): void
52+
public function testPutWritesFileSuccessfully(): void
6353
{
64-
$content = "Hello, PHPUnit!";
65-
$this->storage->put($this->testFile, $content);
54+
$content = 'Test content';
55+
$path = 'test_file.txt';
56+
57+
// Try to put the content into the file
58+
$result = $this->storage->put($path, $content);
59+
$this->assertTrue($result); // Assert the result is true
6660

67-
$this->assertFileExists($this->storage->path($this->testFile));
68-
$this->assertSame($content, $this->storage->get($this->testFile));
61+
// Check that the file was written correctly
62+
$this->assertFileExists(__DIR__ . '/test_storage/' . $path);
63+
$this->assertStringEqualsFile(__DIR__ . '/test_storage/' . $path, $content);
6964
}
7065

7166
/**
72-
* Test appending data to a file.
67+
* Test that the append() method appends data to a file correctly.
7368
*/
74-
public function testAppend(): void
69+
public function testAppendAppendsToFileSuccessfully(): void
7570
{
76-
$this->storage->put($this->testFile, "Line 1");
77-
$this->storage->append($this->testFile, "\nLine 2");
78-
79-
$expectedContent = "Line 1\nLine 2";
80-
$this->assertSame($expectedContent, $this->storage->get($this->testFile));
71+
$content = 'Initial content';
72+
$appendContent = 'Appended content';
73+
$path = 'test_append.txt';
74+
75+
// Write initial content to the file
76+
$this->storage->put($path, $content);
77+
78+
// Append content to the file
79+
$result = $this->storage->append($path, $appendContent);
80+
$this->assertTrue($result); // Assert append was successful
81+
82+
// Check that the content was correctly appended
83+
$expectedContent = $content . $appendContent;
84+
$this->assertStringEqualsFile(__DIR__ . '/test_storage/' . $path, $expectedContent);
8185
}
8286

8387
/**
84-
* Test file existence check.
88+
* Test that the get() method reads a file's content correctly.
8589
*/
86-
public function testExists(): void
90+
public function testGetReadsFileSuccessfully(): void
8791
{
88-
$this->assertFalse($this->storage->exists($this->testFile));
92+
$content = 'File content';
93+
$path = 'test_get.txt';
8994

90-
$this->storage->put($this->testFile, "Test content");
91-
$this->assertTrue($this->storage->exists($this->testFile));
92-
}
95+
// Write content to the file
96+
$this->storage->put($path, $content);
9397

94-
/**
95-
* Test path generation.
96-
*/
97-
public function testPath(): void
98-
{
99-
$expectedPath = $this->testDir . DIRECTORY_SEPARATOR . $this->testFile;
100-
$this->assertSame($expectedPath, $this->storage->path($this->testFile));
98+
// Retrieve the file content
99+
$retrievedContent = $this->storage->get($path);
100+
$this->assertEquals($content, $retrievedContent); // Assert the content is the same
101101
}
102102

103103
/**
104-
* Test setting and using a global base path.
104+
* Test that the get() method throws an exception when the file does not exist.
105105
*/
106-
public function testSetBasePath(): void
106+
public function testGetThrowsExceptionIfFileDoesNotExist(): void
107107
{
108-
$newPath = __DIR__ . '/new_storage';
109-
Storage::setBasePath($newPath);
110-
$storage = new Storage();
108+
$this->expectException(ZatcaStorageException::class);
109+
$this->expectExceptionMessage('File not found.');
111110

112-
$this->assertSame($newPath . DIRECTORY_SEPARATOR . $this->testFile, $storage->path($this->testFile));
111+
$path = 'non_existent_file.txt';
112+
$this->storage->get($path); // Attempt to read a non-existent file
113113
}
114114

115115
/**
116-
* Test exception when reading a non-existent file.
116+
* Test that the exists() method returns true for an existing file.
117117
*/
118-
public function testGetThrowsExceptionForNonExistentFile(): void
118+
public function testExistsReturnsTrueForExistingFile(): void
119119
{
120-
$this->expectException(ZatcaStorageException::class);
121-
$this->expectExceptionMessage("File not found.");
120+
$content = 'Some content';
121+
$path = 'existing_file.txt';
122122

123-
$this->storage->get('non_existent.txt');
123+
// Write the content to the file
124+
$this->storage->put($path, $content);
125+
126+
// Assert that the file exists
127+
$this->assertTrue($this->storage->exists($path));
124128
}
125129

126130
/**
127-
* Test exception when writing to a non-writable directory.
131+
* Test that the exists() method returns false for a non-existent file.
128132
*/
129-
public function testPutThrowsExceptionOnFailure(): void
133+
public function testExistsReturnsFalseForNonExistingFile(): void
130134
{
131-
$this->expectException(ZatcaStorageException::class);
132-
$this->expectExceptionMessage("Directory exists but is not writable.");
133-
134-
// Ensure test directory does not exist
135-
$unwritableDir = sys_get_temp_dir() . '/unwritable_dir';
136-
if (is_dir($unwritableDir)) {
137-
chmod($unwritableDir, 0755);
138-
rmdir($unwritableDir);
139-
}
140-
141-
// Create read-only directory
142-
mkdir($unwritableDir, 0444); // Read-only
143-
144-
try {
145-
$storage = new Storage($unwritableDir);
146-
$storage->put('test.txt', 'Should fail');
147-
} finally {
148-
chmod($unwritableDir, 0755); // Restore permissions before deleting
149-
rmdir($unwritableDir);
150-
}
135+
$path = 'non_existing_file.txt';
136+
$this->assertFalse($this->storage->exists($path)); // Assert the file does not exist
151137
}
152138

153139
/**
154-
* Test exception when trying to create a directory in a read-only filesystem.
140+
* Test that ensureDirectoryExists() creates the directory if it does not exist.
155141
*/
156-
public function testEnsureDirectoryExistsThrowsExceptionOnFailure(): void
142+
public function testEnsureDirectoryExistsCreatesDirectoryIfNotExists(): void
157143
{
158-
$this->expectException(ZatcaStorageException::class);
159-
$this->expectExceptionMessage("Parent directory is not writable.");
144+
$path = 'new_directory/test_file.txt';
145+
$content = 'Test content in a new directory';
160146

161-
// Ensure test directory does not exist
162-
$unwritableDir = sys_get_temp_dir() . '/unwritable_dir';
163-
if (is_dir($unwritableDir)) {
164-
chmod($unwritableDir, 0755);
165-
rmdir($unwritableDir);
166-
}
147+
// Write content to a file in a non-existent directory
148+
$result = $this->storage->put($path, $content);
167149

168-
// Create read-only directory
169-
mkdir($unwritableDir, 0444); // Read-only
170-
171-
try {
172-
$storage = new Storage($unwritableDir);
173-
$storage->put('subdir/test.txt', 'Should fail');
174-
} finally {
175-
chmod($unwritableDir, 0755); // Restore permissions before deleting
176-
rmdir($unwritableDir);
177-
}
150+
// Assert that the file was created and written
151+
$this->assertTrue($result);
152+
$this->assertFileExists(__DIR__ . '/test_storage/' . $path);
178153
}
179-
}
154+
}

tests/ZatcaAPITest.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
use Saleh7\Zatca\Exceptions\ZatcaApiException;
1313
use Saleh7\Zatca\Api\ComplianceCertificateResult;
1414
use Saleh7\Zatca\Api\ProductionCertificateResult;
15-
use InvalidArgumentException;
16-
use Exception;
1715

1816
final class ZatcaAPITest extends TestCase
1917
{
@@ -86,8 +84,11 @@ public function testRequestComplianceCertificateSuccess(): void
8684

8785
$this->assertInstanceOf(ComplianceCertificateResult::class, $result);
8886
$formattedCertificate = $result->getCertificate();
89-
$this->assertStringContainsString("-----BEGIN CERTIFICATE-----", $formattedCertificate);
90-
$this->assertStringContainsString("-----END CERTIFICATE-----", $formattedCertificate);
87+
$this->assertNotFalse(base64_decode($formattedCertificate, true), 'Certificate should be a valid Base64 string.');
88+
89+
$decodedCertificate = base64_decode($formattedCertificate, true);
90+
$this->assertTrue(ctype_print($decodedCertificate) === false, 'Certificate should contain binary data.');
91+
9192
$this->assertEquals("Dehvg1fc8GF6Jwt5bOxXwC6enR93VxeNEo2mlUatfgw=", $result->getSecret());
9293
$this->assertEquals("1234567890123", $result->getRequestId());
9394
}
@@ -122,7 +123,7 @@ public function testValidateInvoiceComplianceSuccess(): void
122123
/**
123124
* Test that requestProductionCertificate returns a valid production result.
124125
*/
125-
public function testRequestProductionCertificateSuccess(): void
126+
public function testRequestProductionCertificateSuccess()
126127
{
127128
$responseData = [
128129
'binarySecurityToken' => base64_encode("dummyProductionCertificate"),
@@ -140,8 +141,14 @@ public function testRequestProductionCertificateSuccess(): void
140141
$complianceRequestId = "dummyComplianceRequestID";
141142

142143
$result = $api->requestProductionCertificate($certificate, $secret, $complianceRequestId);
144+
143145
$this->assertInstanceOf(ProductionCertificateResult::class, $result);
144-
$this->assertStringContainsString("-----BEGIN CERTIFICATE-----", $result->getCertificate());
146+
$formattedCertificate = $result->getCertificate();
147+
$this->assertNotFalse(base64_decode($formattedCertificate, true), 'Certificate should be a valid Base64 string.');
148+
149+
$decodedCertificate = base64_decode($formattedCertificate, true);
150+
$this->assertTrue(ctype_print($decodedCertificate) === false, 'Certificate should contain binary data.');
151+
145152
$this->assertEquals("prodSecret", $result->getSecret());
146153
$this->assertEquals("prodRequestID", $result->getRequestId());
147154
}

0 commit comments

Comments
 (0)