PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / tests / Module / Email / BugReportAttachmentTest.php

BugReportAttachmentTest.php in Packeta 2.3.2, at tests/Module/Email/BugReportAttachmentTest.php

130 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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