Skip to content

Commit

Permalink
Rename get/set introductions
Browse files Browse the repository at this point in the history
to include `all` for a clearer distinction between the multiple and single methods
  • Loading branch information
igorschoester committed Oct 25, 2023
1 parent 1b2bfe3 commit 36fe817
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct( User_Helper $user_helper ) {
*
* @return array The introductions.
*/
public function get_introductions( $user_id ): array {
public function get_all_introductions( $user_id ): array {
$seen_introductions = $this->user_helper->get_meta( $user_id, self::USER_META_KEY, true );
if ( $seen_introductions === false ) {
throw new Invalid_User_Id_Exception();
Expand All @@ -66,7 +66,7 @@ public function get_introductions( $user_id ): array {
* @return bool True on successful update, false on failure or if the value passed to the function is the same as
* the one that is already in the database.
*/
public function set_introductions( $user_id, array $introductions ): bool {
public function set_all_introductions( $user_id, array $introductions ): bool {
return $this->user_helper->update_meta( $user_id, self::USER_META_KEY, $introductions ) === true;
}

Expand All @@ -81,7 +81,7 @@ public function set_introductions( $user_id, array $introductions ): bool {
* @return bool Whether the introduction is seen.
*/
public function is_introduction_seen( $user_id, string $introduction_id ): bool {
$introductions = $this->get_introductions( $user_id );
$introductions = $this->get_all_introductions( $user_id );

if ( \array_key_exists( $introduction_id, $introductions ) ) {
return (bool) $introductions[ $introduction_id ];
Expand All @@ -102,14 +102,14 @@ public function is_introduction_seen( $user_id, string $introduction_id ): bool
* @return bool False on failure. Not having to update is a success.
*/
public function set_introduction( $user_id, string $introduction_id, bool $is_seen = true ): bool {
$introductions = $this->get_introductions( $user_id );
$introductions = $this->get_all_introductions( $user_id );

// Check if the wanted value is already set.
if ( \array_key_exists( $introduction_id, $introductions ) && $introductions[ $introduction_id ] === $is_seen ) {
return true;
}
$introductions[ $introduction_id ] = $is_seen;

return $this->set_introductions( $user_id, $introductions );
return $this->set_all_introductions( $user_id, $introductions );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,31 @@ public function test_constructor() {
/**
* Tests the introductions retrieval.
*
* @covers ::get_introductions
* @covers ::get_all_introductions
*
* @dataProvider provide_get_introductions_test_data
* @dataProvider provide_get_all_introductions_test_data
*
* @param mixed $meta Value `get_meta` returns.
* @param boolean $expected The expected value.
*
* @throws Invalid_User_Id_Exception Invalid User ID.
*/
public function test_get_introductions( $meta, $expected ) {
public function test_get_all_introductions( $meta, $expected ) {
$user_id = 1;
$this->user_helper->expects( 'get_meta' )
->once()
->with( $user_id, Introductions_Seen_Repository::USER_META_KEY, true )
->andReturn( $meta );

$this->assertEquals( $expected, $this->instance->get_introductions( $user_id ) );
$this->assertEquals( $expected, $this->instance->get_all_introductions( $user_id ) );
}

/**
* Provides data for `test_get_introductions()`.
* Provides data for `test_get_all_introductions()`.
*
* @return array
*/
public function provide_get_introductions_test_data() {
public function provide_get_all_introductions_test_data() {
return [
'nothing stored' => [
'meta' => '',
Expand All @@ -101,11 +101,11 @@ public function provide_get_introductions_test_data() {
/**
* Tests the introductions retrieval with an invalid user ID.
*
* @covers ::get_introductions
* @covers ::get_all_introductions
*
* @throws Invalid_User_Id_Exception Invalid User ID.
*/
public function test_get_introductions_with_invalid_user_id() {
public function test_get_all_introductions_with_invalid_user_id() {
$user_id = 1;
$this->user_helper->expects( 'get_meta' )
->once()
Expand All @@ -114,23 +114,23 @@ public function test_get_introductions_with_invalid_user_id() {

$this->expectException( Invalid_User_Id_Exception::class );

$this->instance->get_introductions( $user_id );
$this->instance->get_all_introductions( $user_id );
}

/**
* Tests setting the introductions calls update_meta().
*
* @covers ::set_introductions
* @covers ::set_all_introductions
*/
public function test_set_introductions() {
public function test_set_all_introductions() {
$user_id = 1;
$introductions = [ 'foo' => true ];
$this->user_helper->expects( 'update_meta' )
->once()
->with( $user_id, Introductions_Seen_Repository::USER_META_KEY, $introductions )
->andReturnTrue();

$this->instance->set_introductions( $user_id, $introductions );
$this->instance->set_all_introductions( $user_id, $introductions );
}

/**
Expand Down

0 comments on commit 36fe817

Please sign in to comment.