Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#40 Sender instance passed to the Factory #50

Merged
merged 3 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions includes/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class WPNotify_Factory {

const MESSAGE = 'message';
const MESSAGE = 'message';
const RECIPIENTS = 'recipients';

/**
Expand All @@ -19,28 +19,45 @@ class WPNotify_Factory {
*/
private $recipient_factory;

/**
* Sender factory implementation to use
*
* @var WPNotify_SenderFactory
*/
private $sender_factory;

public function __construct(
WPNotify_MessageFactory $message_factory,
WPNotify_RecipientFactory $recipient_factory
WPNotify_RecipientFactory $recipient_factory,
WPNotify_SenderFactory $sender_factory
) {

$this->message_factory = $message_factory;
$this->recipient_factory = $recipient_factory;
$this->sender_factory = $sender_factory;
}

/**
* Create a notification instance
*
* @param $args
*
* @return WPNotify_BaseNotification
*/
public function create( $args ) {
list( $message_args, $recipients_args ) = $this->validate( $args );

$message = $this->message_factory->create( $message_args );
list( $message_args, $recipients_args, $sender_args ) = $this->validate( $args );

$sender = $this->sender_factory->create( $sender_args );
$recipients = new WPNotify_RecipientCollection();
$message = $this->message_factory->create( $message_args );

foreach ( $recipients_args as $type => $value ) {
$recipients->add(
$this->recipient_factory->create( $value, $type )
);
}

return new WPNotify_BaseNotification( new BaseSender(), $message, $recipients );
return new WPNotify_BaseNotification( $sender, $recipients, $message );
}

private function validate( $args ) {
Expand All @@ -49,11 +66,12 @@ private function validate( $args ) {
$args = json_decode( $args );
}

list( $message_args, $recipients_args ) = $args;
list( $message_args, $recipients_args, $sender_args ) = $args;

return array(
$this->get_message_args( $message_args ),
$this->get_recipients_args( $recipients_args ),
$this->get_sender_args( $sender_args ),
);
}

Expand All @@ -66,4 +84,9 @@ private function get_recipients_args( $args ) {
// TODO: Parse recipients args.
return $args;
}

public function get_sender_args( $args ) {
// TODO: Parse sender args.
return $args;
}
}
26 changes: 25 additions & 1 deletion includes/senders/BaseSender.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class WPNotify_BaseSender implements WPNotify_Sender {
class WPNotify_BaseSender implements WPNotify_Sender, WPNotify_JsonUnserializable {

/**
* @var string
Expand All @@ -24,4 +24,28 @@ public function jsonSerialize() {
'name' => $this->name,
);
}

/**
* Creates a new instance from JSON-encoded data.
*
* @param string $json JSON-encoded data to create the instance from.
*
* @return self
*/
public static function json_unserialize( $json ) {

$data = json_decode( $json, true );

$name = ! empty( $data['name'] ) ? $data['name'] : '';

return new self( $name );
}

/**
* @return string
*/
public function get_name() {

return $this->name;
}
}
13 changes: 13 additions & 0 deletions includes/senders/SenderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

interface WPNotify_SenderFactory {

/**
* Create a new instance of notification sender
*
* @param string $name
*
* @return WPNotify_Sender
*/
public function create( $name );
}
1 change: 0 additions & 1 deletion tests/phpunit/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@

require dirname( __FILE__ ) . '/stubs.php';
require dirname( __FILE__ ) . '/TestCase.php';

24 changes: 19 additions & 5 deletions tests/phpunit/tests/base-sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
class Test_WPNotify_BaseSender extends WPNotify_TestCase {

/**
* @param string $senderName
* @param string $expectedJson
* @param string $sender_name
* @param string $expected_json
*
* @dataProvider data_provider_senders
*/
public function test_it_can_be_json_encoded( $senderName, $expectedJson ) {
public function test_it_can_be_json_encoded( $sender_name, $expected_json ) {

$senderInstance = new WPNotify_BaseSender( $senderName );
$senderInstance = new WPNotify_BaseSender( $sender_name );
$encoded = json_encode( $senderInstance );
$this->assertEquals( $expectedJson, $encoded );
$this->assertEquals( $expected_json, $encoded );
}

/**
* @param string $sender_name
* @param string $json
*
* @dataProvider data_provider_senders
*/
public function test_it_can_be_instantiated_from_json( $sender_name, $json ) {

$sender_instance = WPNotify_BaseSender::json_unserialize( $json );

$this->assertInstanceOf( 'WPNotify_BaseSender', $sender_instance );
$this->assertEquals( $sender_name, $sender_instance->get_name() );
}

public function data_provider_senders() {
Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/tests/test-factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

class Test_WPNotify_Factory extends WPNotify_TestCase {


public function test_create_with_vendor_sender() {

$vendor_sender = $this->createMock( 'WPNotify_Sender' );

$message_factory = $this->getMockBuilder( 'WPNotify_MessageFactory' )
->setMethods( array( 'create', 'accepts' ) )
->getMock();

$message_factory->method( 'create' )->willReturn( $this->createMock( 'WPNotify_Message' ) );
$message_factory->method( 'accepts' )->willReturn( true );

$sender_factory = $this->getMockBuilder( 'WPNotify_SenderFactory' )
->setMethods( array( 'create' ) )
->getMock();

$sender_factory->method( 'create' )->willReturn( $this->createMock( 'WPNotify_Sender' ) );
Comment on lines +8 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. With all these mocks, you won't have to rewrite your test each time we change the code of any of these factories.


$factory = new WPNotify_Factory(
$message_factory,
$this->createMock( 'WPNotify_RecipientFactory' ),
$sender_factory
);

$args = array(
array(),
array(),
array(),
);

$notification = $factory->create( $args );
$this->assertEquals( $vendor_sender, $notification->get_sender() );
}
}