| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Tests\Module; |
| 6 |
|
| 7 |
use DateTimeImmutable; |
| 8 |
use Packetery\Module\Framework\WpAdapter; |
| 9 |
use Packetery\Module\ModuleHelper; |
| 10 |
use Packetery\Nette\IOException; |
| 11 |
use PHPUnit\Framework\TestCase; |
| 12 |
|
| 13 |
class ModuleHelperTest extends TestCase { |
| 14 |
public static function convertToCentimetersProvider(): array { |
| 15 |
return [ |
| 16 |
[ |
| 17 |
'input' => 0, |
| 18 |
'expected' => null, |
| 19 |
], |
| 20 |
[ |
| 21 |
'input' => 1, |
| 22 |
'expected' => 0.1, |
| 23 |
], |
| 24 |
[ |
| 25 |
'input' => 10, |
| 26 |
'expected' => 1.0, |
| 27 |
], |
| 28 |
[ |
| 29 |
'input' => 100, |
| 30 |
'expected' => 10.0, |
| 31 |
], |
| 32 |
[ |
| 33 |
'input' => - 1, |
| 34 |
'expected' => null, |
| 35 |
], |
| 36 |
[ |
| 37 |
'input' => 1000, |
| 38 |
'expected' => 100.0, |
| 39 |
], |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @dataProvider convertToCentimetersProvider |
| 45 |
*/ |
| 46 |
public function testConvertToCentimeters( int $input, ?float $expected ): void { |
| 47 |
$result = ModuleHelper::convertToCentimeters( $input ); |
| 48 |
$this->assertSame( $expected, $result ); |
| 49 |
} |
| 50 |
|
| 51 |
public static function convertToMillimetersProvider(): array { |
| 52 |
return [ |
| 53 |
[ |
| 54 |
'input' => 0.0, |
| 55 |
'expected' => null, |
| 56 |
], |
| 57 |
[ |
| 58 |
'input' => 0.1, |
| 59 |
'expected' => 1.0, |
| 60 |
], |
| 61 |
[ |
| 62 |
'input' => 1.0, |
| 63 |
'expected' => 10.0, |
| 64 |
], |
| 65 |
[ |
| 66 |
'input' => 10.0, |
| 67 |
'expected' => 100.0, |
| 68 |
], |
| 69 |
[ |
| 70 |
'input' => - 0.1, |
| 71 |
'expected' => null, |
| 72 |
], |
| 73 |
[ |
| 74 |
'input' => 100.0, |
| 75 |
'expected' => 1000.0, |
| 76 |
], |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @dataProvider convertToMillimetersProvider |
| 82 |
*/ |
| 83 |
public function testConvertToMillimeters( float $input, ?float $expected ): void { |
| 84 |
$result = ModuleHelper::convertToMillimeters( $input ); |
| 85 |
$this->assertSame( $expected, $result ); |
| 86 |
} |
| 87 |
|
| 88 |
public function testGetTranslatedStringFromDateTimeReturnsFormattedDate(): void { |
| 89 |
$moduleHelper = new ModuleHelper( |
| 90 |
$this->createMock( WpAdapter::class ) |
| 91 |
); |
| 92 |
|
| 93 |
$dummyDateTimeImmutable = new DateTimeImmutable( '2024-03-10 15:30:00' ); |
| 94 |
|
| 95 |
add_filter( |
| 96 |
'woocommerce_admin_order_date_format', |
| 97 |
function () { |
| 98 |
return 'M j, Y'; |
| 99 |
} |
| 100 |
); |
| 101 |
|
| 102 |
$expectedTranslatedString = date_i18n( 'M j, Y', $dummyDateTimeImmutable->getTimestamp() ); |
| 103 |
|
| 104 |
$this->assertEquals( |
| 105 |
$expectedTranslatedString, |
| 106 |
$moduleHelper->getTranslatedStringFromDateTime( $dummyDateTimeImmutable ) |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
public function testGetTranslatedStringFromDateTimeReturnsNullForNullInput(): void { |
| 111 |
$moduleHelper = new ModuleHelper( |
| 112 |
$this->createMock( WpAdapter::class ) |
| 113 |
); |
| 114 |
|
| 115 |
$this->assertNull( $moduleHelper->getTranslatedStringFromDateTime( null ) ); |
| 116 |
} |
| 117 |
|
| 118 |
public function testInstantDeleteRemovesDirectorySafely(): void { |
| 119 |
$tempDir = sys_get_temp_dir() . '/packeta-test-' . uniqid(); |
| 120 |
$testFile = $tempDir . '/test-file.txt'; |
| 121 |
|
| 122 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir |
| 123 |
mkdir( $tempDir, 0755, true ); |
| 124 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 125 |
file_put_contents( $testFile, 'test content' ); |
| 126 |
|
| 127 |
$this->assertDirectoryExists( $tempDir ); |
| 128 |
$this->assertFileExists( $testFile ); |
| 129 |
|
| 130 |
ModuleHelper::instantDelete( $tempDir ); |
| 131 |
|
| 132 |
$this->assertDirectoryDoesNotExist( $tempDir ); |
| 133 |
$this->assertDirectoryDoesNotExist( $tempDir . '-old' ); |
| 134 |
} |
| 135 |
|
| 136 |
public function testInstantDeleteHandlesNonExistentDirectory(): void { |
| 137 |
$nonExistentDir = sys_get_temp_dir() . '/packeta-test-nonexistent-' . uniqid(); |
| 138 |
|
| 139 |
$this->expectException( IOException::class ); |
| 140 |
ModuleHelper::instantDelete( $nonExistentDir ); |
| 141 |
} |
| 142 |
|
| 143 |
public function testInstantDeleteWithComplexDirectoryStructure(): void { |
| 144 |
$tempDir = sys_get_temp_dir() . '/packeta-test-complex-' . uniqid(); |
| 145 |
$subDir1 = $tempDir . '/subdir1'; |
| 146 |
$subDir2 = $tempDir . '/subdir2'; |
| 147 |
$file1 = $tempDir . '/file1.txt'; |
| 148 |
$file2 = $subDir1 . '/file2.txt'; |
| 149 |
$file3 = $subDir2 . '/file3.txt'; |
| 150 |
|
| 151 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir |
| 152 |
mkdir( $subDir1, 0755, true ); |
| 153 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir |
| 154 |
mkdir( $subDir2, 0755, true ); |
| 155 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 156 |
file_put_contents( $file1, 'content1' ); |
| 157 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 158 |
file_put_contents( $file2, 'content2' ); |
| 159 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 160 |
file_put_contents( $file3, 'content3' ); |
| 161 |
|
| 162 |
$this->assertDirectoryExists( $tempDir ); |
| 163 |
$this->assertDirectoryExists( $subDir1 ); |
| 164 |
$this->assertDirectoryExists( $subDir2 ); |
| 165 |
$this->assertFileExists( $file1 ); |
| 166 |
$this->assertFileExists( $file2 ); |
| 167 |
$this->assertFileExists( $file3 ); |
| 168 |
|
| 169 |
ModuleHelper::instantDelete( $tempDir ); |
| 170 |
|
| 171 |
$this->assertDirectoryDoesNotExist( $tempDir ); |
| 172 |
$this->assertDirectoryDoesNotExist( $tempDir . '-old' ); |
| 173 |
} |
| 174 |
} |
| 175 |
|