@@ -196,7 +196,9 @@ private static function encryptFileInternal($inputFilename, $outputFilename, Key
196196 }
197197
198198 /* Open the input file. */
199+ self ::removePHPUnitErrorHandler ();
199200 $ if = @\fopen ($ inputFilename , 'rb ' );
201+ self ::restorePHPUnitErrorHandler ();
200202 if ($ if === false ) {
201203 throw new Ex \IOException (
202204 'Cannot open input file for encrypting: ' .
@@ -209,7 +211,9 @@ private static function encryptFileInternal($inputFilename, $outputFilename, Key
209211 }
210212
211213 /* Open the output file. */
214+ self ::removePHPUnitErrorHandler ();
212215 $ of = @\fopen ($ outputFilename , 'wb ' );
216+ self ::restorePHPUnitErrorHandler ();
213217 if ($ of === false ) {
214218 \fclose ($ if );
215219 throw new Ex \IOException (
@@ -265,7 +269,9 @@ private static function decryptFileInternal($inputFilename, $outputFilename, Key
265269 }
266270
267271 /* Open the input file. */
272+ self ::removePHPUnitErrorHandler ();
268273 $ if = @\fopen ($ inputFilename , 'rb ' );
274+ self ::restorePHPUnitErrorHandler ();
269275 if ($ if === false ) {
270276 throw new Ex \IOException (
271277 'Cannot open input file for decrypting: ' .
@@ -279,7 +285,9 @@ private static function decryptFileInternal($inputFilename, $outputFilename, Key
279285 }
280286
281287 /* Open the output file. */
288+ self ::removePHPUnitErrorHandler ();
282289 $ of = @\fopen ($ outputFilename , 'wb ' );
290+ self ::restorePHPUnitErrorHandler ();
283291 if ($ of === false ) {
284292 \fclose ($ if );
285293 throw new Ex \IOException (
@@ -770,9 +778,38 @@ private static function getLastErrorMessage()
770778 {
771779 $ error = error_get_last ();
772780 if ($ error === null ) {
773- return '[no PHP error] ' ;
781+ return '[no PHP error, or you have a custom error handler set ] ' ;
774782 } else {
775783 return $ error ['message ' ];
776784 }
777785 }
786+
787+ /**
788+ * PHPUnit sets an error handler, which prevents getLastErrorMessage() from working,
789+ * because error_get_last does not work when custom handlers are set.
790+ *
791+ * This is a workaround, which should be a no-op in production deployments, to make
792+ * getLastErrorMessage() return the error messages that the PHPUnit tests expect.
793+ *
794+ * If, in a production deployment, a custom error handler is set, the exception
795+ * handling will still work as usual, but the error messages will be confusing.
796+ *
797+ * @return void
798+ */
799+ private static function removePHPUnitErrorHandler () {
800+ if (defined ('PHPUNIT_COMPOSER_INSTALL ' ) || defined ('__PHPUNIT_PHAR__ ' )) {
801+ set_error_handler (null );
802+ }
803+ }
804+
805+ /**
806+ * Undoes what removePHPUnitErrorHandler did.
807+ *
808+ * @return void
809+ */
810+ private static function restorePHPUnitErrorHandler () {
811+ if (defined ('PHPUNIT_COMPOSER_INSTALL ' ) || defined ('__PHPUNIT_PHAR__ ' )) {
812+ restore_error_handler ();
813+ }
814+ }
778815}
0 commit comments