Skip to content
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
17 changes: 9 additions & 8 deletions includes/framework/class-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
abstract class Factory {

/**
* Container for the main instance of the class.
* Container for the main instance of each factory subclass, keyed by class name.
*
* @property ?Model
* @var array<class-string<static>, static>
*/
protected static $instance = null;
protected static $instances = array();

/**
* Instantiates a model object.
Expand All @@ -33,15 +33,16 @@ abstract public function make( $args );
/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
* The instance will be created if it does not exist yet. Each subclass
* has its own instance.
*
* @return Model The main instance.
* @return static The main instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new static();
if ( ! isset( self::$instances[ static::class ] ) ) {
self::$instances[ static::class ] = new static();
}

return self::$instance;
return self::$instances[ static::class ];
}
}
42 changes: 42 additions & 0 deletions tests/phpunit/tests/framework/test-factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace WP\Notifications\Tests;

use WP\Notifications\Factory;

/**
* @covers \WP\Notifications\Framework\Factory
*/
class Test_Framework_Factory extends TestCase {

/**
* Should return an instance of the subclass it is called on.
*/
public function test_get_instance_returns_instance_of_called_class() {
$this->assertInstanceOf( Factory\Message::class, Factory\Message::get_instance() );
$this->assertInstanceOf( Factory\Notification::class, Factory\Notification::get_instance() );
$this->assertInstanceOf( Factory\Subscription::class, Factory\Subscription::get_instance() );
}

/**
* Should not share one instance across factory subclasses.
*/
public function test_get_instance_is_distinct_per_subclass() {
$message = Factory\Message::get_instance();
$notification = Factory\Notification::get_instance();
$subscription = Factory\Subscription::get_instance();

$this->assertNotSame( $message, $notification );
$this->assertNotSame( $message, $subscription );
$this->assertNotSame( $notification, $subscription );
}

/**
* Should return the same instance on repeated calls for the same subclass.
*/
public function test_get_instance_is_singleton_per_subclass() {
$this->assertSame( Factory\Message::get_instance(), Factory\Message::get_instance() );
$this->assertSame( Factory\Notification::get_instance(), Factory\Notification::get_instance() );
$this->assertSame( Factory\Subscription::get_instance(), Factory\Subscription::get_instance() );
}
Comment thread
jonathanbossenger marked this conversation as resolved.
Resolved
}
Loading