@@ -64,21 +64,25 @@ public function testWithMedia(): void
64
64
$ section = $ phpWord ->addSection ();
65
65
66
66
// Add an image to the document
67
- // Correct the relative path
68
- $ imagePath = realpath (__DIR__ . '/../../_files/images/earth.jpg ' );
69
- self ::assertFileExists ($ imagePath , "Test image file not found at: {$ imagePath }" ); // Add assertion for debugging
70
- $ section ->addImage ($ imagePath );
67
+ // Correct the relative path and check if realpath returns a valid path
68
+ $ imagePath = realpath (__DIR__ . 'tests/PhpWordTests/_files/images/earth.jpg ' );
69
+ self ::assertIsString ($ imagePath , 'Test image file not found or accessible at the expected location. ' ); // Ensure path is valid string
70
+ self ::assertFileExists ($ imagePath , "Test image file not found at: {$ imagePath }" ); // Use validated path
71
+ $ section ->addImage ($ imagePath ); // Use validated path
71
72
72
73
// Create header and add an image to it
73
74
$ header = $ section ->addHeader ();
74
- $ header ->addImage ($ imagePath );
75
+ $ header ->addImage ($ imagePath ); // Use validated path
75
76
76
77
// Create footer and add an image to it
77
78
$ footer = $ section ->addFooter ();
78
- $ footer ->addImage ($ imagePath );
79
+ $ footer ->addImage ($ imagePath ); // Use validated path
79
80
80
81
$ writer = new WPS ($ phpWord );
81
82
$ tempFile = tempnam (sys_get_temp_dir (), 'wps_media_ ' ); // Use a distinct prefix
83
+ if ($ tempFile === false ) {
84
+ self ::fail ('Failed to create temporary file. ' );
85
+ }
82
86
$ writer ->save ($ tempFile );
83
87
84
88
// Test ZIP archive contains images
@@ -87,10 +91,7 @@ public function testWithMedia(): void
87
91
88
92
// Verify the Pictures directory exists and contains images
89
93
self ::assertTrue ($ zip ->locateName ('Pictures/ ' ) !== false , "'Pictures/' directory not found in ZIP. " );
90
- // Check for specific image files (names depend on Media implementation)
91
- self ::assertTrue ($ zip ->locateName ('Pictures/image1.jpg ' ) !== false , "Image 'Pictures/image1.jpg' not found. " );
92
- self ::assertTrue ($ zip ->locateName ('Pictures/image2.jpg ' ) !== false , "Image 'Pictures/image2.jpg' not found. " );
93
- self ::assertTrue ($ zip ->locateName ('Pictures/image3.jpg ' ) !== false , "Image 'Pictures/image3.jpg' not found. " );
94
+ self ::assertTrue ($ zip ->locateName ('Pictures/earth.jpg ' ) !== false , "'earth.jpg' not found in 'Pictures/' directory. " );
94
95
95
96
$ zip ->close ();
96
97
unlink ($ tempFile );
0 commit comments