Skip to content

Commit

Permalink
refactor: notification tests to and from
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmikita committed May 10, 2024
1 parent f28d22f commit 2c95fa7
Showing 1 changed file with 40 additions and 49 deletions.
89 changes: 40 additions & 49 deletions tests/unit/Core/TestNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use BracketSpace\Notification\Tests\Helpers\Registerer;
use BracketSpace\Notification\Core\Notification;
use Brain\Monkey\Filters;

/**
* Notification test case.
Expand Down Expand Up @@ -192,55 +193,6 @@ public function test_version() {

}

/**
* Test to_array
*
* @since 6.0.0
*/
public function test_to_array() {

$trigger = Registerer::register_trigger();
$carrier = Registerer::register_carrier();
$carrier->enable();

$version = time() - 100;
$extras = [
'extras1' => 'value1',
'extras2' => [ 'value2-1', 'value2-2' ],
'extras3' => 3,
];

$notification = new Notification( [
'hash' => 'test-hash',
'title' => 'Test Title',
'trigger' => $trigger,
'carriers' => [ $carrier ],
'enabled' => true,
'extras' => $extras,
'version' => $version,
] );

$data = $notification->to_array();

$this->assertArrayHasKey( 'hash', $data );
$this->assertArrayHasKey( 'title', $data );
$this->assertArrayHasKey( 'trigger', $data );
$this->assertArrayHasKey( 'carriers', $data );
$this->assertArrayHasKey( 'enabled', $data );
$this->assertArrayHasKey( 'extras', $data );
$this->assertArrayHasKey( 'version', $data );

$this->assertEquals( 'test-hash', $data['hash'] );
$this->assertEquals( 'Test Title', $data['title'] );
$this->assertEquals( $trigger->get_slug(), $data['trigger'] );
$this->assertEquals( $trigger->get_slug(), $data['trigger'] );
$this->assertArrayHasKey( $carrier->get_slug(), $data['carriers'] );
$this->assertEquals( true, $data['enabled'] );
$this->assertEquals( $extras, $data['extras'] );
$this->assertEquals( $version, $data['version'] );

}

/**
* Test create_hash
*
Expand Down Expand Up @@ -384,6 +336,45 @@ public function test_get_extra() {

}

/**
* Test from
*/
public function test_from() {

$type = uniqid();
$data = uniqid();
$filterName = sprintf('notification/from/%s', $type);

add_filter($filterName, function() {
return new Notification();
});

Notification::from($type, $data);

$this->assertEquals(1, did_filter($filterName));

}

/**
* Test to
*/
public function test_to() {

$notification = new Notification();
$type = uniqid();
$representation = uniqid();
$filterName = sprintf('notification/to/%s', $type);

add_filter($filterName, function() use ($representation) {
return $representation;
} );

$actual = $notification->to($type);

$this->assertSame($representation, $actual);

}

/**
* Clears after the test
*
Expand Down

0 comments on commit 2c95fa7

Please sign in to comment.