| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Email; |
| 6 |
|
| 7 |
use Packetery\Module\Email\BugReportAttachment; |
| 8 |
use Packetery\Module\Framework\WcAdapter; |
| 9 |
use Packetery\Module\Framework\WpAdapter; |
| 10 |
use Packetery\Module\Log\LogSizeLimiter; |
| 11 |
use Packetery\Module\Options\Exporter; |
| 12 |
use PHPUnit\Framework\MockObject\MockObject; |
| 13 |
use PHPUnit\Framework\TestCase; |
| 14 |
use ReflectionClass; |
| 15 |
use ZipArchive; |
| 16 |
|
| 17 |
class BugReportAttachmentTest extends TestCase { |
| 18 |
private WpAdapter&MockObject $wpAdapter; |
| 19 |
private WcAdapter&MockObject $wcAdapter; |
| 20 |
private Exporter&MockObject $exporter; |
| 21 |
private LogSizeLimiter&MockObject $logSizeLimiter; |
| 22 |
|
| 23 |
private const TEST_EMAIL = 'test@example.com'; |
| 24 |
private const TEST_SITE_NAME = 'Test Site'; |
| 25 |
private const TEST_MESSAGE = 'Test bug report message'; |
| 26 |
private const TEST_TIMESTAMP = '2024-01-01_12-00-00'; |
| 27 |
private const TEST_EXPORT_CONTENT = 'exported settings content'; |
| 28 |
|
| 29 |
private function createBugReportAttachment(): BugReportAttachment { |
| 30 |
$this->wpAdapter = $this->createMock( WpAdapter::class ); |
| 31 |
$this->wcAdapter = $this->createMock( WcAdapter::class ); |
| 32 |
$this->wcAdapter->method( 'loggingUtilGetLogDirectory' ) |
| 33 |
->willReturn( '' ); |
| 34 |
$this->exporter = $this->createMock( Exporter::class ); |
| 35 |
$this->logSizeLimiter = $this->createMock( LogSizeLimiter::class ); |
| 36 |
|
| 37 |
return new BugReportAttachment( |
| 38 |
$this->wpAdapter, |
| 39 |
$this->wcAdapter, |
| 40 |
$this->exporter, |
| 41 |
$this->logSizeLimiter, |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function testCreateAttachmentsMethod(): void { |
| 46 |
$bugReportAttachment = $this->createBugReportAttachment(); |
| 47 |
$result = $bugReportAttachment->createAttachments(); |
| 48 |
$this->assertIsArray( $result ); |
| 49 |
} |
| 50 |
|
| 51 |
public function testCreateAttachmentsWithZipArchiveAndWooCommerce(): void { |
| 52 |
$bugReportAttachment = $this->createBugReportAttachment(); |
| 53 |
|
| 54 |
$this->wpAdapter->method( 'currentTime' ) |
| 55 |
->with( 'Y-m-d_H-i-s', false ) |
| 56 |
->willReturn( self::TEST_TIMESTAMP ); |
| 57 |
|
| 58 |
$this->exporter->method( 'getExportContent' ) |
| 59 |
->willReturn( self::TEST_EXPORT_CONTENT ); |
| 60 |
|
| 61 |
$this->wcAdapter->method( 'adminStatusStatusReport' ) |
| 62 |
->willReturnCallback( |
| 63 |
function () { |
| 64 |
echo 'WooCommerce System Status Content'; |
| 65 |
} |
| 66 |
); |
| 67 |
|
| 68 |
$result = $bugReportAttachment->createAttachments(); |
| 69 |
$this->assertIsArray( $result ); |
| 70 |
if ( class_exists( 'ZipArchive' ) ) { |
| 71 |
$this->assertNotEmpty( $result ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public function testAddWooCommerceSystemStatusToZip(): void { |
| 76 |
$bugReportAttachment = $this->createBugReportAttachment(); |
| 77 |
|
| 78 |
$this->wcAdapter->method( 'adminStatusStatusReport' ) |
| 79 |
->willReturnCallback( |
| 80 |
function () { |
| 81 |
echo 'WooCommerce System Status Content'; |
| 82 |
} |
| 83 |
); |
| 84 |
|
| 85 |
$reflection = new ReflectionClass( $bugReportAttachment ); |
| 86 |
$method = $reflection->getMethod( 'addWooCommerceSystemStatusToZip' ); |
| 87 |
$method->setAccessible( true ); |
| 88 |
|
| 89 |
$zipMock = $this->createMock( ZipArchive::class ); |
| 90 |
$zipMock->expects( $this->once() ) |
| 91 |
->method( 'addFromString' ) |
| 92 |
->with( 'woocommerce-system-status.html', 'WooCommerce System Status Content' ); |
| 93 |
|
| 94 |
$method->invoke( $bugReportAttachment, $zipMock ); |
| 95 |
} |
| 96 |
|
| 97 |
public function testCreateAttachmentsWithRealZipArchive(): void { |
| 98 |
$bugReportAttachment = $this->createBugReportAttachment(); |
| 99 |
|
| 100 |
$this->wpAdapter->method( 'currentTime' ) |
| 101 |
->with( 'Y-m-d_H-i-s', false ) |
| 102 |
->willReturn( self::TEST_TIMESTAMP ); |
| 103 |
|
| 104 |
$this->exporter->method( 'getExportContent' ) |
| 105 |
->willReturn( self::TEST_EXPORT_CONTENT ); |
| 106 |
|
| 107 |
$this->wcAdapter->method( 'adminStatusStatusReport' ) |
| 108 |
->willReturnCallback( |
| 109 |
function () { |
| 110 |
echo 'WooCommerce System Status Content'; |
| 111 |
} |
| 112 |
); |
| 113 |
|
| 114 |
$result = $bugReportAttachment->createAttachments(); |
| 115 |
$this->assertIsArray( $result ); |
| 116 |
|
| 117 |
if ( class_exists( 'ZipArchive' ) ) { |
| 118 |
$this->assertNotEmpty( $result ); |
| 119 |
$this->assertCount( 1, $result ); |
| 120 |
$this->assertStringContainsString( 'logs-', $result[0] ); |
| 121 |
$this->assertStringContainsString( '.zip', $result[0] ); |
| 122 |
|
| 123 |
if ( file_exists( $result[0] ) ) { |
| 124 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink |
| 125 |
unlink( $result[0] ); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|