Skip to content

Commit

Permalink
QA: use the most specific PHPUnit assertion possible
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed May 7, 2019
1 parent a2d14b5 commit 33f0b39
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions tests/php/unit/Configuration/configuration-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand All @@ -78,7 +78,7 @@ public function testACF5VersionFunction() {
);
$config = $configuration->to_array();

$this->assertEquals( $acf_version, $config['acfVersion'] );
$this->assertSame( $acf_version, $config['acfVersion'] );
}

/**
Expand Down
18 changes: 14 additions & 4 deletions tests/php/unit/Configuration/string-store-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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 );
}

/**
Expand All @@ -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 );
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/php/unit/main-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

0 comments on commit 33f0b39

Please sign in to comment.