From 33f0b3917c4eef9179f45535019c86e40d744102 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 8 May 2019 01:12:04 +0200 Subject: [PATCH] QA: use the most specific PHPUnit assertion possible This is a long established best practice. PHPUnit contains a variety of assertions and the ones available have been extended hugely over the years. To have the most reliable tests, the most specific assertion should be used. This implements this for the Yoast ACF Analysis plugin. Refs: * https://phpunit.de/manual/4.8/en/appendixes.assertions.html * https://phpunit.readthedocs.io/en/7.5/assertions.html# Most notably, this changes calls to `assertEquals()` to `assertSame()`, where `assertEquals()` does a loose type comparison and `assertSame()` does a strict type comparison. Refs: * https://phpunit.de/manual/6.5/en/appendixes.assertions.html#appendixes.assertions.assertEquals * https://phpunit.de/manual/6.5/en/appendixes.assertions.html#appendixes.assertions.assertSame It also includes a stricter check on arrays which are supposed to be empty and removes a duplicate function call. --- .../unit/Configuration/configuration-test.php | 4 ++-- .../unit/Configuration/string-store-test.php | 18 ++++++++++++++---- tests/php/unit/main-test.php | 6 ++++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/php/unit/Configuration/configuration-test.php b/tests/php/unit/Configuration/configuration-test.php index d88ddd8a..e530dc6c 100644 --- a/tests/php/unit/Configuration/configuration-test.php +++ b/tests/php/unit/Configuration/configuration-test.php @@ -59,7 +59,7 @@ public function testEmpty() { $configuration->to_array() ); - $this->assertEquals( Filters\applied( 'acf/get_info' ), 1 ); + $this->assertSame( Filters\applied( 'acf/get_info' ), 1 ); } /** @@ -78,7 +78,7 @@ public function testACF5VersionFunction() { ); $config = $configuration->to_array(); - $this->assertEquals( $acf_version, $config['acfVersion'] ); + $this->assertSame( $acf_version, $config['acfVersion'] ); } /** diff --git a/tests/php/unit/Configuration/string-store-test.php b/tests/php/unit/Configuration/string-store-test.php index a825ce01..51010d42 100644 --- a/tests/php/unit/Configuration/string-store-test.php +++ b/tests/php/unit/Configuration/string-store-test.php @@ -22,8 +22,11 @@ protected function getStore() { * @return void */ public function testEmpty() { - $store = $this->getStore(); - $this->assertEmpty( $store->to_array() ); + $store = $this->getStore(); + $result = $store->to_array(); + + $this->assertInternalType( 'array', $result ); + $this->assertEmpty( $result ); } /** @@ -101,7 +104,11 @@ public function testAddNonString() { $store = $this->getStore(); $this->assertFalse( $store->add( 999 ) ); - $this->assertEmpty( $store->to_array() ); + + $result = $store->to_array(); + + $this->assertInternalType( 'array', $result ); + $this->assertEmpty( $result ); } /** @@ -127,7 +134,10 @@ public function testRemove() { $store->remove( $type_b ); - $this->assertEmpty( $store->to_array() ); + $result = $store->to_array(); + + $this->assertInternalType( 'array', $result ); + $this->assertEmpty( $result ); } /** diff --git a/tests/php/unit/main-test.php b/tests/php/unit/main-test.php index 9cee2e8f..e6b19e04 100644 --- a/tests/php/unit/main-test.php +++ b/tests/php/unit/main-test.php @@ -42,7 +42,9 @@ public function testInvalidConfig() { $testee = new \AC_Yoast_SEO_ACF_Content_Analysis(); $testee->boot(); - $this->assertNotSame( 'Invalid Config', $registry->get( 'config' ) ); - $this->assertInstanceOf( \Yoast_ACF_Analysis_Configuration::class, $registry->get( 'config' ) ); + $result = $registry->get( 'config' ); + + $this->assertNotSame( 'Invalid Config', $result ); + $this->assertInstanceOf( \Yoast_ACF_Analysis_Configuration::class, $result ); } }