From 0175361881601946dc2013e8a71e0a3aecdcbbb5 Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Fri, 5 Nov 2021 22:35:28 -0300 Subject: [PATCH 1/3] remove final of the prefix of class --- src/Creational/Singleton/Singleton.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Creational/Singleton/Singleton.php b/src/Creational/Singleton/Singleton.php index ff740dc..f6bddd9 100644 --- a/src/Creational/Singleton/Singleton.php +++ b/src/Creational/Singleton/Singleton.php @@ -6,7 +6,7 @@ use LogicException; -final class Singleton +class Singleton { private static ?Singleton $instance = null; From 5c100eb0ff9686f3d076931ac2d9ccd531a3e56c Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Sat, 6 Nov 2021 10:52:36 -0300 Subject: [PATCH 2/3] Adjust test for check of unserialize --- src/Creational/Singleton/Singleton.php | 6 -- tests/Creational/Singleton/SingletonTest.php | 66 ++++++++++---------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/Creational/Singleton/Singleton.php b/src/Creational/Singleton/Singleton.php index f6bddd9..4fa76f4 100644 --- a/src/Creational/Singleton/Singleton.php +++ b/src/Creational/Singleton/Singleton.php @@ -26,12 +26,6 @@ private function __construct() private function __clone(): void { } - - public function __sleep(): array - { - throw new LogicException('Cannot serialize a singleton.'); - return []; - } public function __wakeup(): void { diff --git a/tests/Creational/Singleton/SingletonTest.php b/tests/Creational/Singleton/SingletonTest.php index 4cd87c0..64567a8 100644 --- a/tests/Creational/Singleton/SingletonTest.php +++ b/tests/Creational/Singleton/SingletonTest.php @@ -11,35 +11,37 @@ final class SingletonTest extends TestCase { - public function testShouldBeTheSameInstanceForTwoObjets(): void - { - $firstInstance = Singleton::getInstance(); - $secondInstance = SingleTon::getInstance(); - - $this->assertSame($firstInstance, $secondInstance); - } - - public function testShouldThrowErrorWhenTryToCreateInstance(): void - { - $this->expectException(Error::class); - - $instance = new Singleton(); - } - - public function testShouldThrowErrorWhenTryToClone(): void - { - $this->expectException(Error::class); - - $instance = Singleton::getInstance(); - $clone = clone $instance; - } - - public function testShouldThrowExceptionWhenTryToSerialize(): void - { - $this->expectException(LogicException::class); - $this->expectExceptionMessage('Cannot serialize a singleton.'); - - $instance = Singleton::getInstance(); - $serialize = serialize($instance); - } -} \ No newline at end of file + public function testShouldBeTheSameInstanceForTwoObjets(): void + { + $firstInstance = Singleton::getInstance(); + $secondInstance = SingleTon::getInstance(); + + $this->assertSame($firstInstance, $secondInstance); + } + + public function testShouldThrowErrorWhenTryToCreateInstance(): void + { + $this->expectException(Error::class); + + $instance = new Singleton(); + } + + public function testShouldThrowErrorWhenTryToClone(): void + { + $this->expectException(Error::class); + + $instance = Singleton::getInstance(); + $clone = clone $instance; + } + + public function testShouldThrowExceptionWhenTryToUnserialize(): void + { + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Cannot unserialize a singleton.'); + + $instance = Singleton::getInstance(); + $serialize = serialize($instance); + + $unserialize = unserialize($serialize); + } +} From c8f2476ee8c828a5c1f3a350bded41ae160efb1b Mon Sep 17 00:00:00 2001 From: Walmir Silva Date: Sat, 6 Nov 2021 11:08:11 -0300 Subject: [PATCH 3/3] add comments for explain this concept of serialize and unserialise --- src/Creational/Singleton/Singleton.php | 6 ++++++ tests/Creational/Singleton/SingletonTest.php | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Creational/Singleton/Singleton.php b/src/Creational/Singleton/Singleton.php index 4fa76f4..3d81571 100644 --- a/src/Creational/Singleton/Singleton.php +++ b/src/Creational/Singleton/Singleton.php @@ -27,6 +27,12 @@ private function __clone(): void { } + public function __sleep(): array + { + throw new LogicException('Cannot serialize a singleton.'); + return []; + } + public function __wakeup(): void { throw new LogicException('Cannot unserialize a singleton.'); diff --git a/tests/Creational/Singleton/SingletonTest.php b/tests/Creational/Singleton/SingletonTest.php index 64567a8..7fa206f 100644 --- a/tests/Creational/Singleton/SingletonTest.php +++ b/tests/Creational/Singleton/SingletonTest.php @@ -34,14 +34,23 @@ public function testShouldThrowErrorWhenTryToClone(): void $clone = clone $instance; } - public function testShouldThrowExceptionWhenTryToUnserialize(): void + public function testShouldThrowExceptionWhenTryToSerialize(): void { $this->expectException(LogicException::class); - $this->expectExceptionMessage('Cannot unserialize a singleton.'); + $this->expectExceptionMessage('Cannot serialize a singleton.'); $instance = Singleton::getInstance(); $serialize = serialize($instance); + } + + public function testShouldThrowExceptionWhenTryToUnserialize(): void + { + $this->expectException(LogicException::class); + //$this->expectExceptionMessage('Cannot unserialize a singleton.'); + + $instance = Singleton::getInstance(); + $serialize = serialize($instance); // vai cair na exceção do serialize - $unserialize = unserialize($serialize); + $unserialize = unserialize($serialize); // não vai chegar aqui } }