From 2fe59263f8534bb2926c5c5fff50368de148ca23 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 8 Aug 2014 12:33:34 +0100 Subject: [PATCH] Improved exception tests We should make sure the tests fail if an exception that should be thrown is not thrown. --- tests/DefinitionTest.php | 4 ++++ tests/FactoryMuffinTest.php | 8 ++++++++ tests/SaveAndDeleteTest.php | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/tests/DefinitionTest.php b/tests/DefinitionTest.php index bdf2660..ed38d7a 100644 --- a/tests/DefinitionTest.php +++ b/tests/DefinitionTest.php @@ -86,6 +86,9 @@ public function testFactoryLoading() $this->assertInstanceOf('League\FactoryMuffin\Factory', $return); } + /** + * @expectedException \League\FactoryMuffin\Exceptions\DirectoryNotFoundException + */ public function testShouldThrowExceptionWhenLoadingANonexistentDirectory() { try { @@ -93,6 +96,7 @@ public function testShouldThrowExceptionWhenLoadingANonexistentDirectory() } catch (DirectoryNotFoundException $e) { $this->assertEquals("The directory '$path' was not found.", $e->getMessage()); $this->assertEquals($path, $e->getPath()); + throw $e; } } } diff --git a/tests/FactoryMuffinTest.php b/tests/FactoryMuffinTest.php index 26cec37..f52a7fd 100644 --- a/tests/FactoryMuffinTest.php +++ b/tests/FactoryMuffinTest.php @@ -84,6 +84,9 @@ public function testFakerDefaultLongitude() $this->assertLessThanOrEqual(180, $obj->lon); } + /** + * @expectedException \League\FactoryMuffin\Exceptions\NoDefinedFactoryException + */ public function testShouldThrowExceptionWhenNoDefinedFactoryException() { try { @@ -91,6 +94,7 @@ public function testShouldThrowExceptionWhenNoDefinedFactoryException() } catch (NoDefinedFactoryException $e) { $this->assertEquals("No factory class was defined for the model of type: '$model'.", $e->getMessage()); $this->assertEquals($model, $e->getModel()); + throw $e; } } @@ -108,6 +112,9 @@ public function testCanCreateFromStaticMethod() $this->assertInstanceOf('ModelWithStaticMethodFactory', $obj->object); } + /** + * @expectedException \League\FactoryMuffin\Exceptions\MethodNotFoundException + */ public function testThrowExceptionWhenInvalidStaticMethod() { try { @@ -116,6 +123,7 @@ public function testThrowExceptionWhenInvalidStaticMethod() $this->assertEquals("The static method 'doesNotExist' was not found on the model of type: '$model'.", $e->getMessage()); $this->assertEquals($model, $e->getModel()); $this->assertEquals('doesNotExist', $e->getMethod()); + throw $e; } } } diff --git a/tests/SaveAndDeleteTest.php b/tests/SaveAndDeleteTest.php index a271546..29af8ab 100644 --- a/tests/SaveAndDeleteTest.php +++ b/tests/SaveAndDeleteTest.php @@ -32,6 +32,8 @@ public function testShouldNotSave() /** * @runInSeparateProcess * @preserveGlobalState disabled + * + * @expectedException \League\FactoryMuffin\Exceptions\SaveMethodNotFoundException */ public function testShouldThrowExceptionAfterSaveMethodRename() { @@ -44,12 +46,15 @@ public function testShouldThrowExceptionAfterSaveMethodRename() $this->assertEquals($model, $e->getModel()); $this->assertEquals('foo', $e->getMethod()); $this->assertInstanceOf($model, $e->getObject()); + throw $e; } } /** * @runInSeparateProcess * @preserveGlobalState disabled + * + * @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException */ public function testShouldThrowExceptionAfterDeleteMethodRename() { @@ -65,9 +70,13 @@ public function testShouldThrowExceptionAfterDeleteMethodRename() $this->assertEquals($model, $exceptions[0]->getModel()); $this->assertEquals('bar', $exceptions[0]->getMethod()); $this->assertInstanceOf($model, $exceptions[0]->getObject()); + throw $e; } } + /** + * @expectedException \League\FactoryMuffin\Exceptions\SaveFailedException + */ public function testShouldThrowExceptionOnModelSaveFailure() { try { @@ -76,9 +85,13 @@ public function testShouldThrowExceptionOnModelSaveFailure() $this->assertEquals("We could not save the model of type: '$model'.", $e->getMessage()); $this->assertEquals($model, $e->getModel()); $this->assertNull($e->getErrors()); + throw $e; } } + /** + * @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException + */ public function testShouldThrowExceptionOnModelDeleteFailure() { try { @@ -88,9 +101,13 @@ public function testShouldThrowExceptionOnModelDeleteFailure() $exceptions = $e->getExceptions(); $this->assertEquals("We encountered 1 problem(s) while trying to delete the saved models.", $e->getMessage()); $this->assertEquals("We could not delete the model of type: '$model'.", $exceptions[0]->getMessage()); + throw $e; } } + /** + * @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException + */ public function testShouldAlsoThrowExceptionOnModelDeleteFailure() { try { @@ -100,9 +117,13 @@ public function testShouldAlsoThrowExceptionOnModelDeleteFailure() $exceptions = $e->getExceptions(); $this->assertEquals("We encountered 1 problem(s) while trying to delete the saved models.", $e->getMessage()); $this->assertEquals("OH NOES!", $exceptions[0]->getMessage()); + throw $e; } } + /** + * @expectedException \League\FactoryMuffin\Exceptions\SaveMethodNotFoundException + */ public function testShouldThrowExceptionWithoutSaveMethod() { try { @@ -112,9 +133,13 @@ public function testShouldThrowExceptionWithoutSaveMethod() $this->assertEquals($model, $e->getModel()); $this->assertEquals('save', $e->getMethod()); $this->assertInstanceOf($model, $e->getObject()); + throw $e; } } + /** + * @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException + */ public function testShouldThrowExceptionWithoutDeleteMethod() { try { @@ -127,9 +152,13 @@ public function testShouldThrowExceptionWithoutDeleteMethod() $this->assertEquals($model, $exceptions[0]->getModel()); $this->assertEquals('delete', $exceptions[0]->getMethod()); $this->assertInstanceOf($model, $exceptions[0]->getObject()); + throw $e; } } + /** + * @expectedException \League\FactoryMuffin\Exceptions\SaveFailedException + */ public function testShouldThrowExceptionWithValidationErrors() { try { @@ -138,9 +167,13 @@ public function testShouldThrowExceptionWithValidationErrors() $this->assertEquals("Failed to save. We could not save the model of type: '$model'.", $e->getMessage()); $this->assertEquals($model, $e->getModel()); $this->assertEquals('Failed to save.', $e->getErrors()); + throw $e; } } + /** + * @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException + */ public function testShouldThrowMultipleDeletionExceptions() { try { @@ -157,6 +190,7 @@ public function testShouldThrowMultipleDeletionExceptions() $this->assertInstanceOf($model, $exceptions[1]->getObject()); $this->assertInternalType('array', $e->getExceptions()); $this->assertCount(2, $e->getExceptions()); + throw $e; } } }