Skip to content

Commit

Permalink
Adds unit tests for get_bookmark().
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya committed Nov 11, 2020
1 parent e4d197e commit e361d91
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions tests/phpunit/tests/bookmark/getBookmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

/**
* @group bookmark_me
* @covers ::get_bookmark
*/
class Tests_Bookmark_GetBookmark extends WP_UnitTestCase {
private $query_called = false;
private static $bookmark;

public function tearDown() {
parent::tearDown();

$this->query_called = false;
unset( $GLOBALS['link'] );
}

public function test_should_return_null_when_empty_given() {
$this->assertNull( get_bookmark( 0 ) );
$this->assertFalse( wp_cache_get( self::$bookmark->link_id, 'bookmark' ) );
}

/**
* @dataProvider data_bookmark
*
* @param array|object|null $expected Type returned depends on $output value.
* @param string $output The required return type.
* @param string $filter How to sanitize bookmark fields.
*/
public function test_should_return_globals_link_when_given( $expected, $output, $filter ) {
$GLOBALS['link'] = self::$bookmark;
$this->run_bookmark_test( 0, $expected, $output, $filter );
$this->run_bookmark_test( self::$bookmark->link_id, $expected, $output, $filter );
}

/**
* @dataProvider data_bookmark
*
* @param array|object|null $expected Type returned depends on $output value.
* @param string $output The required return type.
* @param string $filter How to sanitize bookmark fields.
*/
public function test_should_return_bookmark_when_object_given( $expected, $output, $filter ) {
$this->run_bookmark_test( self::$bookmark, $expected, $output, $filter );
}

/**
* @dataProvider data_bookmark
*
* @param array|object|null $expected Type returned depends on $output value.
* @param string $output The required return type.
* @param string $filter How to sanitize bookmark fields.
*/
public function test_should_return_bookmark_when_int_given( $expected, $output, $filter ) {
add_filter(
'query',
function ( $query ) {
$this->query_called = true;

return $query;
}
);

// Cache does not exist. Check pulls from db.
$this->run_bookmark_test( self::$bookmark->link_id, $expected, $output, $filter );
$this->assertTrue( $this->query_called );

// Cache now exists. Check pulls from cache.
$this->query_called = false;
$actual = get_bookmark( self::$bookmark->link_id, $output, $filter );
if ( ARRAY_A === $output || ARRAY_N === $output ) {
$this->assertIsArray( $actual );
$this->assertSame( $expected, $actual );
} else {
$this->assertInstanceOf( stdClass::class, $actual );
$this->assertEquals( $expected, $actual );
}
$this->assertFalse( $this->query_called );
}

/**
* @param int|stdClass $bookmark ID or instance of the bookmark.
* @param array|object|null $expected Type returned depends on $output value.
* @param string $output The required return type.
* @param string $filter How to sanitize bookmark fields.
*/
private function run_bookmark_test( $bookmark, $expected, $output, $filter ) {
$actual = get_bookmark( $bookmark, $output, $filter );
$this->assertEquals( $expected, $actual );

if ( ARRAY_A === $output || ARRAY_N === $output ) {
$this->assertIsArray( $actual );
$this->assertSame( $expected, $actual );
$key = ARRAY_N === $output ? $actual[0] : $actual['link_id'];
} else {
$this->assertInstanceOf( stdClass::class, $actual );
$this->assertEquals( $expected, $actual );
$key = $actual->link_id;
}

if ( ! isset( $GLOBALS['link'] ) ) {
$cached = wp_cache_get( $key, 'bookmark' );
$this->assertInstanceOf( stdClass::class, $cached );
$this->assertEquals( self::$bookmark, $cached );
}
}

public function data_bookmark() {
if ( null === self::$bookmark ) {
self::$bookmark = self::factory()->bookmark->create_and_get();
}
$array_a = get_object_vars( self::$bookmark );
$array_n = array_values( $array_a );

return array(
array( self::$bookmark, '', 'raw' ),
array( self::$bookmark, 'invalid', 'raw' ),
array( self::$bookmark, OBJECT, 'raw' ),
array( self::$bookmark, OBJECT, 'display' ),
array( $array_a, ARRAY_A, 'raw' ),
array( $array_a, ARRAY_A, 'display' ),
array( $array_n, ARRAY_N, 'raw' ),
array( $array_n, ARRAY_N, 'display' ),
);
}
}

0 comments on commit e361d91

Please sign in to comment.