Skip to content

Commit

Permalink
Use methods instead of annotations for expected exceptions in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SenseException committed Sep 6, 2017
1 parent f0a8fd1 commit 628bbc5
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 27 deletions.
26 changes: 18 additions & 8 deletions tests/bitExpert/Disco/AnnotationBeanFactoryUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace bitExpert\Disco;

use bitExpert\Disco\BeanException;
use bitExpert\Disco\BeanNotFoundException;
use bitExpert\Disco\Config\BeanConfiguration;
use bitExpert\Disco\Config\BeanConfigurationSubclass;
use bitExpert\Disco\Config\BeanConfigurationTrait;
Expand Down Expand Up @@ -54,28 +56,31 @@ public function setUp()

/**
* @test
* @expectedException \bitExpert\Disco\BeanNotFoundException
*/
public function retrievingNonExistentBeanThrowsException()
{
self::expectException(BeanNotFoundException::class);

$this->beanFactory->get('serviceWhichDoesNotExist');
}

/**
* @test
* @expectedException \bitExpert\Disco\BeanException
*/
public function retrievingBeanWithEmptyStringThrowsException()
{
self::expectException(BeanException::class);

$this->beanFactory->get('');
}

/**
* @test
* @expectedException \bitExpert\Disco\BeanException
*/
public function retrievingBeanWithNonStringThrowsException()
{
self::expectException(BeanException::class);

$this->beanFactory->get(3);
}

Expand Down Expand Up @@ -322,10 +327,11 @@ public function nestedParameterKeyPassedToBeanFactoryGetsInjectedInBean()

/**
* @test
* @expectedException \bitExpert\Disco\BeanException
*/
public function missingRequiredParameterWillThrowException()
{
self::expectException(BeanException::class);

$this->beanFactory = new AnnotationBeanFactory(BeanConfigurationWithParameters::class);
BeanFactoryRegistry::register($this->beanFactory);

Expand Down Expand Up @@ -421,10 +427,11 @@ public function protectedDependencyNotVisibleToTheCaller()

/**
* @test
* @expectedException \bitExpert\Disco\BeanException
*/
public function throwsExceptionIfTypeOfReturnedObjectIsNotExpectedOfNonLazyBean()
{
self::expectException(BeanException::class);

$this->beanFactory = new AnnotationBeanFactory(WrongReturnTypeConfiguration::class);
BeanFactoryRegistry::register($this->beanFactory);

Expand All @@ -433,10 +440,11 @@ public function throwsExceptionIfTypeOfReturnedObjectIsNotExpectedOfNonLazyBean(

/**
* @test
* @expectedException \bitExpert\Disco\BeanException
*/
public function throwsExceptionIfNonLazyBeanMethodDoesNotReturnAnything()
{
self::expectException(BeanException::class);

$this->beanFactory = new AnnotationBeanFactory(WrongReturnTypeConfiguration::class);
BeanFactoryRegistry::register($this->beanFactory);

Expand All @@ -445,10 +453,11 @@ public function throwsExceptionIfNonLazyBeanMethodDoesNotReturnAnything()

/**
* @test
* @expectedException \bitExpert\Disco\BeanException
*/
public function throwsExceptionIfTypeOfReturnedObjectIsNotExpectedOfLazyBean()
{
self::expectException(BeanException::class);

$this->beanFactory = new AnnotationBeanFactory(WrongReturnTypeConfiguration::class);
BeanFactoryRegistry::register($this->beanFactory);

Expand All @@ -458,10 +467,11 @@ public function throwsExceptionIfTypeOfReturnedObjectIsNotExpectedOfLazyBean()

/**
* @test
* @expectedException \bitExpert\Disco\BeanException
*/
public function throwsExceptionIfLazyBeanMethodDoesNotReturnAnything()
{
self::expectException(BeanException::class);

$this->beanFactory = new AnnotationBeanFactory(WrongReturnTypeConfiguration::class);
BeanFactoryRegistry::register($this->beanFactory);

Expand Down
3 changes: 3 additions & 0 deletions tests/bitExpert/Disco/Annotations/AliasUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function aliasCannotBeNamedAliasAndTypeAlias()
{
self::expectException(AnnotationException::class);
self::expectExceptionMessage('Type alias should not have a name!');

new Alias(['value' => ['name' => 'someAliasName', 'type' => true]]);
}

Expand All @@ -59,6 +60,7 @@ public function aliasShouldBeNamedOrTypeAlias()
{
self::expectException(AnnotationException::class);
self::expectExceptionMessage('Alias should either be a named alias or a type alias!');

new Alias();
}

Expand All @@ -70,6 +72,7 @@ public function aliasNameCannotBeEmpty($name)
{
self::expectException(AnnotationException::class);
self::expectExceptionMessage('Alias should either be a named alias or a type alias!');

new Alias(['value' => ['name' => $name, 'type' => false]]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ function (Parameter $parameter) {

/**
* @test
* @expectedException TypeError
*/
public function throwsExceptionIfParameterTypeDoesNotMatch()
{
self::expectException(TypeError::class);

$bean = new Bean([
'value' => [
'parameters' => [
Expand Down
6 changes: 4 additions & 2 deletions tests/bitExpert/Disco/Annotations/BeanUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,11 @@ function (Alias $alias) {

/**
* @test
* @expectedException TypeError
*/
public function throwsExceptionIfAliasTypeDoesNotMatch()
{
self::expectException(TypeError::class);

$bean = new Bean([
'value' => [
'aliases' => [
Expand Down Expand Up @@ -245,10 +246,11 @@ function (Parameter $parameter) {

/**
* @test
* @expectedException TypeError
*/
public function throwsExceptionIfParameterTypeDoesNotMatch()
{
self::expectException(TypeError::class);

$bean = new Bean([
'value' => [
'parameters' => [
Expand Down
4 changes: 3 additions & 1 deletion tests/bitExpert/Disco/Annotations/ParameterUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace bitExpert\Disco\Annotations;

use Doctrine\Common\Annotations\AnnotationException;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -21,10 +22,11 @@ class ParameterUnitTest extends TestCase
{
/**
* @test
* @expectedException \Doctrine\Common\Annotations\AnnotationException
*/
public function missingNameWillThrowAnnotationException()
{
self::expectException(AnnotationException::class);

new Parameter();
}

Expand Down
14 changes: 9 additions & 5 deletions tests/bitExpert/Disco/BeanFactoryConfigurationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace bitExpert\Disco;

use bitExpert\Disco\Store\SerializableBeanStore;
use InvalidArgumentException;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use ProxyManager\Autoloader\Autoloader;
Expand All @@ -28,10 +29,11 @@ class BeanFactoryConfigurationUnitTest extends TestCase
{
/**
* @test
* @expectedException \InvalidArgumentException
*/
public function invalidProxyTargetDirThrowsException()
{
self::expectException(InvalidArgumentException::class);

new BeanFactoryConfiguration('/abc');
}

Expand Down Expand Up @@ -130,22 +132,24 @@ public function configuredBeanStoreInstanceCanBererieved()

/**
* @test
* @expectedException \InvalidArgumentException
* @expectedExceptionCode 10
*/
public function injectedInvalidProxyTargetDirThrowsException()
{
self::expectException(InvalidArgumentException::class);
self::expectExceptionCode(10);

$config = new BeanFactoryConfiguration(sys_get_temp_dir());
$config->setProxyTargetDir('/abc');
}

/**
* @test
* @expectedException \InvalidArgumentException
* @expectedExceptionCode 20
*/
public function injectedNotWritableProxyTargetDirThrowsException()
{
self::expectException(InvalidArgumentException::class);
self::expectExceptionCode(20);

$config = new BeanFactoryConfiguration(sys_get_temp_dir());
$path = vfsStream::setup('root', 0x111);
$config->setProxyTargetDir($path->url());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use bitExpert\Disco\Config\NonExistentReturnTypeConfiguration;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
use ProxyManager\Exception\InvalidProxiedClassException;
use Zend\Code\Generator\ClassGenerator;

/**
Expand Down Expand Up @@ -52,71 +53,78 @@ public function setUp()

/**
* @test
* @expectedException \ProxyManager\Exception\InvalidProxiedClassException
*/
public function configClassWithoutAnAnnotationThrowsException()
{
self::expectException(InvalidProxiedClassException::class);

$reflClass = new \ReflectionClass(InvalidConfiguration::class);
$this->configGenerator->generate($reflClass, $this->classGenerator);
}

/**
* @test
* @expectedException \ProxyManager\Exception\InvalidProxiedClassException
*/
public function passingInterfaceAsConfigClassThrowsException()
{
self::expectException(InvalidProxiedClassException::class);

$reflClass = new \ReflectionClass(InterfaceConfiguration::class);
$this->configGenerator->generate($reflClass, $this->classGenerator);
}

/**
* @test
* @expectedException \ProxyManager\Exception\InvalidProxiedClassException
*/
public function missingBeanAnnotationThrowsException()
{
self::expectException(InvalidProxiedClassException::class);

$reflClass = new \ReflectionClass(MissingBeanAnnotationConfiguration::class);
$this->configGenerator->generate($reflClass, $this->classGenerator);
}

/**
* @test
* @expectedException \ProxyManager\Exception\InvalidProxiedClassException
*/
public function missingReturnTypeOfBeanDeclarationThrowsException()
{
self::expectException(InvalidProxiedClassException::class);

$reflClass = new \ReflectionClass(MissingReturnTypeConfiguration::class);
$this->configGenerator->generate($reflClass, $this->classGenerator);
}

/**
* @test
* @expectedException \ProxyManager\Exception\InvalidProxiedClassException
*/
public function nonExistentClassInReturnTypeThrowsException()
{
self::expectException(InvalidProxiedClassException::class);

$reflClass = new \ReflectionClass(NonExistentReturnTypeConfiguration::class);
$this->configGenerator->generate($reflClass, $this->classGenerator);
}

/**
* @test
* @expectedException \ProxyManager\Exception\InvalidProxiedClassException
*/
public function sameAliasUsedForMultipleBeansThrowsException()
{
self::expectException(InvalidProxiedClassException::class);

$reflClass = new \ReflectionClass(BeanConfigurationWithConflictingAliases::class);
$this->configGenerator->generate($reflClass, $this->classGenerator);
}

/**
* @test
* @expectedException \ProxyManager\Exception\InvalidProxiedClassException
* @expectedExceptionMessageRegExp /^\[Semantical Error\] The annotation "@foo"/
*/
public function unknownAnnotationThrowsException()
{
self::expectException(InvalidProxiedClassException::class);
self::expectExceptionMessageRegExp('/^\[Semantical Error\] The annotation "@foo"/');

/**
* @foo
*/
Expand Down
5 changes: 3 additions & 2 deletions tests/bitExpert/Disco/Store/SerializableBeanStoreUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use bitExpert\Disco\Helper\MasterService;
use bitExpert\Disco\Helper\SampleService;
use Doctrine\Instantiator\Exception\InvalidArgumentException;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -66,10 +66,11 @@ public function addingBeanWithSameBeanIdMultipleTimeWillNotTriggerError()

/**
* @test
* @expectedException InvalidArgumentException
*/
public function gettingNonExistentBeanWillThrowException()
{
self::expectException(InvalidArgumentException::class);

$this->beanStore->get('some-random-bean-instance');
}

Expand Down

0 comments on commit 628bbc5

Please sign in to comment.