From 39be46b5aeee9fa9feeb6e1ba3c234c0b170432c Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Tue, 1 May 2018 20:34:55 +0200 Subject: [PATCH] Model: Add tests for HTTP classes --- src/PHPDraft/Model/HTTPRequest.php | 2 +- src/PHPDraft/Model/HTTPResponse.php | 2 +- src/PHPDraft/Model/Tests/HTTPRequestTest.php | 20 ++++++++++++++++--- src/PHPDraft/Model/Tests/HTTPResponseTest.php | 14 +++++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/PHPDraft/Model/HTTPRequest.php b/src/PHPDraft/Model/HTTPRequest.php index bbd964dd..d0a1f8ab 100644 --- a/src/PHPDraft/Model/HTTPRequest.php +++ b/src/PHPDraft/Model/HTTPRequest.php @@ -79,6 +79,7 @@ class HTTPRequest implements Comparable public function __construct(&$parent) { $this->parent = &$parent; + $this->id = md5(microtime()); } /** @@ -90,7 +91,6 @@ public function __construct(&$parent) */ public function parse($object) { - $this->id = md5(microtime()); $this->method = $object->attributes->method; $this->title = isset($object->meta->title) ? $object->meta->title : NULL; diff --git a/src/PHPDraft/Model/HTTPResponse.php b/src/PHPDraft/Model/HTTPResponse.php index 7b1dd3bd..420d8f4e 100644 --- a/src/PHPDraft/Model/HTTPResponse.php +++ b/src/PHPDraft/Model/HTTPResponse.php @@ -65,8 +65,8 @@ class HTTPResponse implements Comparable public function __construct($parent) { - $this->id = md5(microtime()); $this->parent = &$parent; + $this->id = md5(microtime()); } /** diff --git a/src/PHPDraft/Model/Tests/HTTPRequestTest.php b/src/PHPDraft/Model/Tests/HTTPRequestTest.php index f8029303..fac116cd 100644 --- a/src/PHPDraft/Model/Tests/HTTPRequestTest.php +++ b/src/PHPDraft/Model/Tests/HTTPRequestTest.php @@ -36,7 +36,9 @@ public function setUp() $this->parent = $this->getMockBuilder('\PHPDraft\Model\Transition') ->disableOriginalConstructor() ->getMock(); + $this->mock_function('microtime', 'test'); $this->class = new HTTPRequest($parent); + $this->unmock_function('microtime'); $this->reflection = new ReflectionClass('PHPDraft\Model\HTTPRequest'); } @@ -54,9 +56,21 @@ public function tearDown() */ public function testSetupCorrectly() { - $property = $this->reflection->getProperty('parent'); - $property->setAccessible(TRUE); - $this->assertNull($property->getValue($this->class)); + $parent_property = $this->reflection->getProperty('parent'); + $parent_property->setAccessible(TRUE); + $this->assertNull($parent_property->getValue($this->class)); + + $id_property = $this->reflection->getProperty('id'); + $id_property->setAccessible(TRUE); + $this->assertSame('098f6bcd4621d373cade4e832627b4f6', $id_property->getValue($this->class)); + } + + /** + * Tests if get_id returns the correct ID. + */ + public function testGetId() + { + $this->assertSame('098f6bcd4621d373cade4e832627b4f6', $this->class->get_id()); } /** diff --git a/src/PHPDraft/Model/Tests/HTTPResponseTest.php b/src/PHPDraft/Model/Tests/HTTPResponseTest.php index a2f44631..964e9ce0 100644 --- a/src/PHPDraft/Model/Tests/HTTPResponseTest.php +++ b/src/PHPDraft/Model/Tests/HTTPResponseTest.php @@ -33,7 +33,9 @@ public function setUp() $parent = NULL; $this->parent = $this->getMockBuilder('\PHPDraft\Model\HierarchyElement') ->getMock(); + $this->mock_function('microtime', 'test'); $this->class = new HTTPResponse($parent); + $this->unmock_function('microtime'); $this->reflection = new ReflectionClass('PHPDraft\Model\HTTPResponse'); } @@ -54,6 +56,18 @@ public function testSetupCorrectly() $property = $this->reflection->getProperty('parent'); $property->setAccessible(TRUE); $this->assertNull($property->getValue($this->class)); + + $id_property = $this->reflection->getProperty('id'); + $id_property->setAccessible(TRUE); + $this->assertSame('098f6bcd4621d373cade4e832627b4f6', $id_property->getValue($this->class)); + } + + /** + * Tests if get_id returns the correct ID. + */ + public function testGetId() + { + $this->assertSame('098f6bcd4621d373cade4e832627b4f6', $this->class->get_id()); } /**