From 687b46389df2950d791f799f7ed1122e39efb967 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 14 Sep 2015 08:47:51 +0100 Subject: [PATCH] Started work on Events tests --- app/Events/User/UserEventInterface.php | 17 +++++ app/Events/User/UserWasAddedEvent.php | 4 +- app/Events/User/UserWasRemovedEvent.php | 4 +- tests/AbstractAnemicTestCase.php | 67 +++++++++++++++++++ tests/Events/AbstractEventTestCase.php | 54 +++++++++++++++ .../Events/User/AbstractUserEventTestCase.php | 23 +++++++ tests/Events/User/UserWasAddedEventTest.php | 26 +++++++ 7 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 app/Events/User/UserEventInterface.php create mode 100644 tests/AbstractAnemicTestCase.php create mode 100644 tests/Events/AbstractEventTestCase.php create mode 100644 tests/Events/User/AbstractUserEventTestCase.php create mode 100644 tests/Events/User/UserWasAddedEventTest.php diff --git a/app/Events/User/UserEventInterface.php b/app/Events/User/UserEventInterface.php new file mode 100644 index 00000000..f9f88df3 --- /dev/null +++ b/app/Events/User/UserEventInterface.php @@ -0,0 +1,17 @@ + + */ +abstract class AbstractAnemicTestCase extends AbstractTestCase +{ + public function testClassIsFinal() + { + $rc = new ReflectionClass($this->getObjectAndParams()['object']); + + $this->assertTrue($rc->isFinal()); + } + + public function testPropertiesMatchTheConstructor() + { + $rc = new ReflectionClass($this->getObjectAndParams()['object']); + + $properties = array_map(function ($property) { + return $property->getName(); + }, $rc->getProperties()); + + $params = array_map(function ($param) { + return $param->getName(); + }, $rc->getMethod('__construct')->getParameters()); + + if ($this->objectHasRules()) { + $params[] = 'rules'; + } + + $this->assertSame($properties, $params); + } + + public function testPropertiesAreCorrectlyDefined() + { + $rc = new ReflectionClass($this->getObjectAndParams()['object']); + + foreach ($rc->getProperties() as $property) { + $this->assertTrue($property->isPublic()); + $this->assertFalse($property->isStatic()); + } + } + + public function testPropertyAccessBehavesCorrectly() + { + extract($this->getObjectAndParams()); + + foreach ($params as $key => $value) { + $this->assertSame($value, $object->{$key}); + } + } +} diff --git a/tests/Events/AbstractEventTestCase.php b/tests/Events/AbstractEventTestCase.php new file mode 100644 index 00000000..3dd75ce0 --- /dev/null +++ b/tests/Events/AbstractEventTestCase.php @@ -0,0 +1,54 @@ + + */ +abstract class AbstractEventTestCase extends AbstractAnemicTestCase +{ + protected function objectHasRules() + { + return false; + } + + public function testEventImplementsTheCorrectInterfaces() + { + $event = $this->getObjectAndParams()['object']; + + foreach ($this->getEventInterfaces() as $interface) { + $this->assertInstanceOf($interface, $event); + } + } + + /*public function testEventHasRegisteredHandlers() + { + $property = (new ReflectionClass(EventServiceProvider::class))->getProperty('listen'); + $property->setAccessible(true); + + $class = get_class($this->getObjectAndParams()['object']); + $mappings = $property->getValue(new EventServiceProvider($this->app)); + + $this->assertTrue(isset($mappings[$class])); + $this->assertGreaterThan(0, count($mappings[$class])); + + foreach ($mappings[$class] as $handler) { + $this->assertInstanceOf($handler, $this->app->make($handler)); + } + }*/ +} diff --git a/tests/Events/User/AbstractUserEventTestCase.php b/tests/Events/User/AbstractUserEventTestCase.php new file mode 100644 index 00000000..c257749f --- /dev/null +++ b/tests/Events/User/AbstractUserEventTestCase.php @@ -0,0 +1,23 @@ + new User()]; + $object = new UserWasAddedEvent($params['user']); + + return compact('params', 'object'); + } +}