Skip to content

Commit

Permalink
Merge branch 'release/1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
btassone committed Jan 19, 2018
2 parents 89db74c + 03ef04c commit ea81dce
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Installation
============
TODO

CHANGELOG
=========
* 1.0.3
* Fixed issue with Singleton class.
* Description: When using the library in multiple plugins the Singleton class was only returning the class firstly ever
used by it. Obviously this was not intended behavior. It now returns the only the first used instance of each class type
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "objectivco/booster-seat",
"description": "Classes for the faster creation of wordpress plugins",
"keywords": ["template", "composer", "package"],
"version": "1.0.3",
"license": "GPL-3.0-only",
"authors": [
{
Expand Down
10 changes: 6 additions & 4 deletions src/Base/Singleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class Singleton
* @access private
* @var null
*/
private static $instance = null;
protected static $instance = array();

/**
* Singleton constructor. Just a stub. Do not fill with logic
Expand Down Expand Up @@ -54,10 +54,12 @@ private function __wakeup() {}
*/
final public static function instance()
{
if (self::$instance === null) {
self::$instance = new static;
$class = (string)get_called_class();

if (!array_key_exists($class, self::$instance)) {
self::$instance[$class] = new static;
}

return self::$instance;
return self::$instance[$class];
}
}
80 changes: 80 additions & 0 deletions tests/Base/SingletonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

use Objectiv\BoosterSeat\Base\Singleton;

// Middle class so we can test more inheritance
class Middle extends Singleton {
public $name = "";
}

// Test class 1
class Test1 extends Middle {}

// Test class 2
class Test2 extends Middle {}

// Test class 3
class Test3 extends Middle {}

/**
* Sample test case.
*
* @since 1.0.3
*/
class SingletonTest extends WP_UnitTestCase {

/**
* @var Test1
*/
public $test1;

/**
* @var Test2
*/
public $test2;

/**
* @var Test3
*/
public $test3;

/**
* Test setup function where we grab each classes initial instance
*/
function setUp() {
parent::setUp();

$this->test1 = Test1::instance();
$this->test1->name = "Murphy";

$this->test2 = Test2::instance();
$this->test2->name = "John";

$this->test3 = Test3::instance();
$this->test3->name = "Brandon";
}

/**
* Test that when multiple classes extend from this base class it correctly retrieves the right class
*/
function test_class_extended_can_allow_for_other_classes() {

$this->assertTrue($this->test1 instanceof Test1);
$this->assertTrue($this->test2 instanceof Test2);
$this->assertTrue($this->test3 instanceof Test3);
}

/**
* T
*/
function test_class_extended_can_be_the_only_class_created() {

$previous_test1 = $this->test1;
$test1 = Test1::instance();

$this->assertEquals($previous_test1, $test1);
$this->assertEquals("Murphy", $test1->name);
$this->assertEquals("John", Test2::instance()->name);
$this->assertEquals("Brandon", Test3::instance()->name);
}
}
2 changes: 2 additions & 0 deletions tests/Managers/PathManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/**
* Sample test case.
*
* @since 1.0.0
*/
class PathManagerTest extends WP_UnitTestCase {

Expand Down
2 changes: 2 additions & 0 deletions tests/Utilities/ActivatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* Sample test case.
*
* @since 1.0.0
*/
class ActivatorTest extends WP_UnitTestCase {

Expand Down
2 changes: 2 additions & 0 deletions tests/Utilities/DeactivatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* Sample test case.
*
* @since 1.0.0
*/
class DeactivatorTest extends WP_UnitTestCase {

Expand Down

0 comments on commit ea81dce

Please sign in to comment.