This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate singleton class from normal class
- Loading branch information
1 parent
b2bd946
commit d44d11d
Showing
5 changed files
with
84 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace montefuscolo; | ||
|
||
class SingletonMediator extends BaseMediator { | ||
private static $instance = null; | ||
|
||
private function __construct() { | ||
parent::__construct(); | ||
} | ||
|
||
public static function getInstance() { | ||
if (self::$instance === null) { | ||
self::$instance = new self(); | ||
} | ||
return self::$instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use montefuscolo\SingletonMediator; | ||
|
||
|
||
class SingletonMediatorTest extends BaseMediatorTest { | ||
public function getInstance() { | ||
return SingletonMediator::getInstance(); | ||
} | ||
|
||
public function testUniqueInstance() { | ||
$instance1 = $this->getInstance(); | ||
$instance2 = $this->getInstance(); | ||
|
||
$instance1->add_filter('test_unique_instance', function($value) { | ||
return "> $value <"; | ||
}); | ||
$result = $instance2->run_filters('test_unique_instance', 'foo'); | ||
|
||
$this->assertSame($instance1, $instance2); | ||
$this->assertEquals("> foo <", $result); | ||
} | ||
} |