From b3de59825f9d0b5cd01f92dd67d87505e3f413a7 Mon Sep 17 00:00:00 2001 From: Brandon Tassone Date: Thu, 18 Jan 2018 22:16:59 -0500 Subject: [PATCH 1/2] Critical Bug: Singleton was returning first and only class used by it. This was reaching across multiple plugins. Added tests for Singleton Fix. Assign first class to to string key of class name. If class exists, return it, if not create new class. --- src/Base/Singleton.php | 10 +++-- tests/Base/SingletonTest.php | 78 ++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 tests/Base/SingletonTest.php diff --git a/src/Base/Singleton.php b/src/Base/Singleton.php index b97834d..5322388 100644 --- a/src/Base/Singleton.php +++ b/src/Base/Singleton.php @@ -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 @@ -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]; } } \ No newline at end of file diff --git a/tests/Base/SingletonTest.php b/tests/Base/SingletonTest.php new file mode 100644 index 0000000..73e4148 --- /dev/null +++ b/tests/Base/SingletonTest.php @@ -0,0 +1,78 @@ +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); + } +} From 03ef04cab0881fdd7a650e90e741891aecdb63f1 Mon Sep 17 00:00:00 2001 From: Brandon Tassone Date: Thu, 18 Jan 2018 22:22:55 -0500 Subject: [PATCH 2/2] Changed a few things for next release --- README.md | 10 ++++++++++ composer.json | 1 + tests/Base/SingletonTest.php | 2 ++ tests/Managers/PathManagerTest.php | 2 ++ tests/Utilities/ActivatorTest.php | 2 ++ tests/Utilities/DeactivatorTest.php | 2 ++ 6 files changed, 19 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..be8baa1 --- /dev/null +++ b/README.md @@ -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 \ No newline at end of file diff --git a/composer.json b/composer.json index bdd848a..9ae82ad 100644 --- a/composer.json +++ b/composer.json @@ -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": [ { diff --git a/tests/Base/SingletonTest.php b/tests/Base/SingletonTest.php index 73e4148..2fbe14f 100644 --- a/tests/Base/SingletonTest.php +++ b/tests/Base/SingletonTest.php @@ -18,6 +18,8 @@ class Test3 extends Middle {} /** * Sample test case. + * + * @since 1.0.3 */ class SingletonTest extends WP_UnitTestCase { diff --git a/tests/Managers/PathManagerTest.php b/tests/Managers/PathManagerTest.php index 8eebce2..2fe28a7 100644 --- a/tests/Managers/PathManagerTest.php +++ b/tests/Managers/PathManagerTest.php @@ -9,6 +9,8 @@ /** * Sample test case. + * + * @since 1.0.0 */ class PathManagerTest extends WP_UnitTestCase { diff --git a/tests/Utilities/ActivatorTest.php b/tests/Utilities/ActivatorTest.php index 690a57f..ae8f27d 100644 --- a/tests/Utilities/ActivatorTest.php +++ b/tests/Utilities/ActivatorTest.php @@ -7,6 +7,8 @@ /** * Sample test case. + * + * @since 1.0.0 */ class ActivatorTest extends WP_UnitTestCase { diff --git a/tests/Utilities/DeactivatorTest.php b/tests/Utilities/DeactivatorTest.php index 4b1ccaf..127680e 100644 --- a/tests/Utilities/DeactivatorTest.php +++ b/tests/Utilities/DeactivatorTest.php @@ -7,6 +7,8 @@ /** * Sample test case. + * + * @since 1.0.0 */ class DeactivatorTest extends WP_UnitTestCase {