From b28c9eebdcccc682f9ff00358a5a78a7faf253ed Mon Sep 17 00:00:00 2001
From: jrfnl <jrfnl@users.noreply.github.com>
Date: Wed, 21 Jul 2021 16:33:30 +0200
Subject: [PATCH] Build/Tests: custom assertions should always have a $message
parameter
All assertions in PHPUnit have a `$message` parameter.
Setting this parameter allows to distinguish which assertion is failing when a test runs multiple assertion, making debugging of the tests easier.
The WP custom assertions which are included in the `WP_UnitTestCase_Base` class should also allow for this `$message` parameter.
This patch adds this - optional - parameter for those assertions which were missing it.
---
tests/phpunit/includes/abstract-testcase.php | 64 +++++++++++---------
1 file changed, 36 insertions(+), 28 deletions(-)
diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php
index ab984160e6..3d65410a67 100644
a
|
b
|
abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { |
671 | 671 | * |
672 | 672 | * @param string $expected The expected value. |
673 | 673 | * @param string $actual The actual value. |
| 674 | * @param string $message Optional. Message to display when the assertion fails. |
674 | 675 | */ |
675 | | public function assertDiscardWhitespace( $expected, $actual ) { |
676 | | $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ) ); |
| 676 | public function assertDiscardWhitespace( $expected, $actual, $message = '' ) { |
| 677 | $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual, $message ) ); |
677 | 678 | } |
678 | 679 | |
679 | 680 | /** |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { |
684 | 685 | * |
685 | 686 | * @param string|array $expected The expected value. |
686 | 687 | * @param string|array $actual The actual value. |
| 688 | * @param string $message Optional. Message to display when the assertion fails. |
687 | 689 | */ |
688 | | public function assertSameIgnoreEOL( $expected, $actual ) { |
| 690 | public function assertSameIgnoreEOL( $expected, $actual, $message = '' ) { |
689 | 691 | $expected = map_deep( |
690 | 692 | $expected, |
691 | 693 | function ( $value ) { |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { |
700 | 702 | } |
701 | 703 | ); |
702 | 704 | |
703 | | $this->assertSame( $expected, $actual ); |
| 705 | $this->assertSame( $expected, $actual, $message ); |
704 | 706 | } |
705 | 707 | |
706 | 708 | /** |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { |
711 | 713 | * |
712 | 714 | * @param string $expected The expected value. |
713 | 715 | * @param string $actual The actual value. |
| 716 | * @param string $message Optional. Message to display when the assertion fails. |
714 | 717 | */ |
715 | | public function assertEqualsIgnoreEOL( $expected, $actual ) { |
716 | | $this->assertSameIgnoreEOL( $expected, $actual ); |
| 718 | public function assertEqualsIgnoreEOL( $expected, $actual, $message = '' ) { |
| 719 | $this->assertSameIgnoreEOL( $expected, $actual, $message ); |
717 | 720 | } |
718 | 721 | |
719 | 722 | /** |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { |
721 | 724 | * |
722 | 725 | * @since 5.6.0 |
723 | 726 | * |
724 | | * @param array $expected Expected array. |
725 | | * @param array $actual Array to check. |
| 727 | * @param array $expected Expected array. |
| 728 | * @param array $actual Array to check. |
| 729 | * @param string $message Optional. Message to display when the assertion fails. |
726 | 730 | */ |
727 | | public function assertSameSets( $expected, $actual ) { |
| 731 | public function assertSameSets( $expected, $actual, $message = '' ) { |
728 | 732 | sort( $expected ); |
729 | 733 | sort( $actual ); |
730 | | $this->assertSame( $expected, $actual ); |
| 734 | $this->assertSame( $expected, $actual, $message ); |
731 | 735 | } |
732 | 736 | |
733 | 737 | /** |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { |
735 | 739 | * |
736 | 740 | * @since 3.5.0 |
737 | 741 | * |
738 | | * @param array $expected Expected array. |
739 | | * @param array $actual Array to check. |
| 742 | * @param array $expected Expected array. |
| 743 | * @param array $actual Array to check. |
| 744 | * @param string $message Optional. Message to display when the assertion fails. |
740 | 745 | */ |
741 | | public function assertEqualSets( $expected, $actual ) { |
| 746 | public function assertEqualSets( $expected, $actual, $message = '' ) { |
742 | 747 | sort( $expected ); |
743 | 748 | sort( $actual ); |
744 | | $this->assertEquals( $expected, $actual ); |
| 749 | $this->assertEquals( $expected, $actual, $message ); |
745 | 750 | } |
746 | 751 | |
747 | 752 | /** |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { |
749 | 754 | * |
750 | 755 | * @since 5.6.0 |
751 | 756 | * |
752 | | * @param array $expected Expected array. |
753 | | * @param array $actual Array to check. |
| 757 | * @param array $expected Expected array. |
| 758 | * @param array $actual Array to check. |
| 759 | * @param string $message Optional. Message to display when the assertion fails. |
754 | 760 | */ |
755 | | public function assertSameSetsWithIndex( $expected, $actual ) { |
| 761 | public function assertSameSetsWithIndex( $expected, $actual, $message = '' ) { |
756 | 762 | ksort( $expected ); |
757 | 763 | ksort( $actual ); |
758 | | $this->assertSame( $expected, $actual ); |
| 764 | $this->assertSame( $expected, $actual, $message ); |
759 | 765 | } |
760 | 766 | |
761 | 767 | /** |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { |
763 | 769 | * |
764 | 770 | * @since 4.1.0 |
765 | 771 | * |
766 | | * @param array $expected Expected array. |
767 | | * @param array $actual Array to check. |
| 772 | * @param array $expected Expected array. |
| 773 | * @param array $actual Array to check. |
| 774 | * @param string $message Optional. Message to display when the assertion fails. |
768 | 775 | */ |
769 | | public function assertEqualSetsWithIndex( $expected, $actual ) { |
| 776 | public function assertEqualSetsWithIndex( $expected, $actual, $message = '' ) { |
770 | 777 | ksort( $expected ); |
771 | 778 | ksort( $actual ); |
772 | | $this->assertEquals( $expected, $actual ); |
| 779 | $this->assertEquals( $expected, $actual, $message ); |
773 | 780 | } |
774 | 781 | |
775 | 782 | /** |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase { |
777 | 784 | * |
778 | 785 | * @since 4.8.0 |
779 | 786 | * |
780 | | * @param array $array Array to check. |
| 787 | * @param array $array Array to check. |
| 788 | * @param string $message Optional. Message to display when the assertion fails. |
781 | 789 | */ |
782 | | public function assertNonEmptyMultidimensionalArray( $array ) { |
783 | | $this->assertIsArray( $array ); |
784 | | $this->assertNotEmpty( $array ); |
| 790 | public function assertNonEmptyMultidimensionalArray( $array, $message = '' ) { |
| 791 | $this->assertIsArray( $array, $message . ' Value under test is not an array.' ); |
| 792 | $this->assertNotEmpty( $array, $message . ' Array is empty' ); |
785 | 793 | |
786 | 794 | foreach ( $array as $sub_array ) { |
787 | | $this->assertIsArray( $sub_array ); |
788 | | $this->assertNotEmpty( $sub_array ); |
| 795 | $this->assertIsArray( $sub_array, $message . ' Subitem of the array is not an array' ); |
| 796 | $this->assertNotEmpty( $sub_array, $message . ' Subitem of the array is empty' ); |
789 | 797 | } |
790 | 798 | } |
791 | 799 | |