Skip to content

Commit

Permalink
Improved exception tests
Browse files Browse the repository at this point in the history
We should make sure the tests fail if an exception that should be thrown
is not thrown.
  • Loading branch information
GrahamCampbell committed Aug 8, 2014
1 parent e4ddb5f commit 2fe5926
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,17 @@ public function testFactoryLoading()
$this->assertInstanceOf('League\FactoryMuffin\Factory', $return);
}

/**
* @expectedException \League\FactoryMuffin\Exceptions\DirectoryNotFoundException
*/
public function testShouldThrowExceptionWhenLoadingANonexistentDirectory()
{
try {
FactoryMuffin::loadFactories($path = __DIR__ . '/thisdirectorydoesntexist');
} catch (DirectoryNotFoundException $e) {
$this->assertEquals("The directory '$path' was not found.", $e->getMessage());
$this->assertEquals($path, $e->getPath());
throw $e;
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/FactoryMuffinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,17 @@ public function testFakerDefaultLongitude()
$this->assertLessThanOrEqual(180, $obj->lon);
}

/**
* @expectedException \League\FactoryMuffin\Exceptions\NoDefinedFactoryException
*/
public function testShouldThrowExceptionWhenNoDefinedFactoryException()
{
try {
FactoryMuffin::instance($model = 'ModelWithNoFactoryClassStub');
} 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;
}
}

Expand All @@ -108,6 +112,9 @@ public function testCanCreateFromStaticMethod()
$this->assertInstanceOf('ModelWithStaticMethodFactory', $obj->object);
}

/**
* @expectedException \League\FactoryMuffin\Exceptions\MethodNotFoundException
*/
public function testThrowExceptionWhenInvalidStaticMethod()
{
try {
Expand All @@ -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;
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/SaveAndDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function testShouldNotSave()
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @expectedException \League\FactoryMuffin\Exceptions\SaveMethodNotFoundException
*/
public function testShouldThrowExceptionAfterSaveMethodRename()
{
Expand All @@ -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()
{
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit 2fe5926

Please sign in to comment.