Skip to content

Commit

Permalink
Merge branch 'SenseException-coverage'
Browse files Browse the repository at this point in the history
  • Loading branch information
shochdoerfer committed Jun 12, 2017
2 parents 5baa3e1 + 66fa136 commit 2e41463
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 143 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"psr/container": "^1.0.0",
"doctrine/annotations": "^1.3.0",
"ocramius/proxy-manager": "^2.1.1",
"bitexpert/slf4psrlog": "^0.1.3"
"bitexpert/slf4psrlog": "^0.1.3",
"mikey179/vfsStream": "^1.6"
},
"require-dev": {
"phpunit/phpunit": "^6.1.3",
Expand Down
295 changes: 179 additions & 116 deletions composer.lock

Large diffs are not rendered by default.

19 changes: 6 additions & 13 deletions src/bitExpert/Disco/BeanFactoryConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use ProxyManager\FileLocator\FileLocator;
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
use ProxyManager\GeneratorStrategy\GeneratorStrategyInterface;
use RuntimeException;

/**
* BeanFactory configuration class.
Expand Down Expand Up @@ -97,7 +96,8 @@ public function setProxyTargetDir(string $proxyTargetDir)
sprintf(
'Proxy target directory "%s" does not exist!',
$proxyTargetDir
)
),
10
);
}

Expand All @@ -106,7 +106,8 @@ public function setProxyTargetDir(string $proxyTargetDir)
sprintf(
'Proxy target directory "%s" is not writable!',
$proxyTargetDir
)
),
20
);
}

Expand Down Expand Up @@ -134,20 +135,12 @@ public function setProxyWriterGenerator(GeneratorStrategyInterface $writergenera
public function setProxyAutoloader(AutoloaderInterface $autoloader)
{
if ($this->proxyAutoloader instanceof AutoloaderInterface) {
if (!spl_autoload_unregister($this->proxyAutoloader)) {
throw new RuntimeException(
sprintf('Cannot unregister autoloader "%s"', get_class($this->proxyAutoloader))
);
}
spl_autoload_unregister($this->proxyAutoloader);
}

$this->proxyAutoloader = $autoloader;

if (!spl_autoload_register($this->proxyAutoloader, false)) {
throw new RuntimeException(
sprintf('Cannot register autoloader "%s"', get_class($this->proxyAutoloader))
);
}
spl_autoload_register($this->proxyAutoloader);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe
$methodReflection,
$beanAnnotation,
$parametersAnnotation,
$beanType,
$method->getReturnType(),
$forceLazyInitProperty,
$sessionBeansProperty,
$postProcessorsProperty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\BeanPostProcessorsProperty;
use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\ForceLazyInitProperty;
use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\SessionBeansProperty;
use bitExpert\Disco\Proxy\Configuration\MethodGenerator\WrapBeanAsLazy;
use bitExpert\Disco\Proxy\LazyBean\LazyBeanFactory;
use ProxyManager\Exception\InvalidProxiedClassException;
use ProxyManager\Generator\MethodGenerator;
use ProxyManager\Proxy\LazyLoadingInterface;
use ReflectionType;
use Zend\Code\Generator\ParameterGenerator;
use Zend\Code\Reflection\MethodReflection;

Expand All @@ -42,7 +43,7 @@ class BeanMethod extends MethodGenerator
* @param MethodReflection $originalMethod
* @param Bean $beanMetadata
* @param Parameters $methodParameters
* @param $beanType
* @param ReflectionType|null $beanType
* @param ForceLazyInitProperty $forceLazyInitProperty
* @param SessionBeansProperty $sessionBeansProperty
* @param BeanPostProcessorsProperty $postProcessorsProperty
Expand All @@ -55,14 +56,25 @@ public static function generateMethod(
MethodReflection $originalMethod,
Bean $beanMetadata,
Parameters $methodParameters,
$beanType,
?ReflectionType $beanType,
ForceLazyInitProperty $forceLazyInitProperty,
SessionBeansProperty $sessionBeansProperty,
BeanPostProcessorsProperty $postProcessorsProperty,
BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty,
GetParameter $parameterValuesMethod,
WrapBeanAsLazy $wrapBeanAsLazy
) {
if (null === $beanType) {
throw new InvalidProxiedClassException(
sprintf(
'Method "%s" on "%s" is missing the return type hint!',
$originalMethod->getName(),
$originalMethod->getDeclaringClass()->getName()
)
);
}
$beanType = (string) $beanType;

$method = static::fromReflection($originalMethod);
$methodParams = static::convertMethodParamsToString($methodParameters, $parameterValuesMethod);
$beanId = $originalMethod->name;
Expand Down Expand Up @@ -99,12 +111,13 @@ public static function generateMethod(
}
} else {
// return type is unknown, throw an exception
$body .= '$message = sprintf(' . PHP_EOL;
$body .= ' \'Either return type declaration missing or unknown for bean with id "' . $beanId .
'": %s\',' . PHP_EOL;
$body .= ' $e->getMessage()' . PHP_EOL;
$body .= ');' . PHP_EOL;
$body .= 'throw new \\' . BeanException::class . '($message, 0, $e);' . PHP_EOL;
throw new InvalidProxiedClassException(
sprintf(
'Return type of method "%s" on "%s" cannot be found! Did you use the full qualified name?',
$originalMethod->getName(),
$originalMethod->getDeclaringClass()->getName()
)
);
}

$method->setBody($body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use PHPUnit\Framework\TestCase;

/**
* Unit tests for {@link \bitExpert\Disco\Annotations\AnnotationsAttributeParser}.
* Unit tests for {@link \bitExpert\Disco\Annotations\AnnotationAttributeParser}.
*/
class AnnotationAttributeParserUnitTest extends TestCase
{
Expand Down
35 changes: 35 additions & 0 deletions tests/bitExpert/Disco/BeanFactoryAwareTraitUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Disco package.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

namespace bitExpert\Disco;

use PHPUnit\Framework\TestCase;

/**
* Unit tests for {@link \bitExpert\Disco\BeanFactoryAwareTrait}.
*/
class BeanFactoryAwareTraitUnitTest extends TestCase
{
/**
* @test
*/
public function setBeanFactory()
{
$beanFactory = $this->createMock(BeanFactory::class);
$trait = $this->getMockForTrait(BeanFactoryAwareTrait::class);

$trait->setBeanFactory($beanFactory);

self::assertAttributeInstanceOf(BeanFactory::class, 'beanFactory', $trait);
self::assertAttributeSame($beanFactory, 'beanFactory', $trait);
}
}
50 changes: 48 additions & 2 deletions tests/bitExpert/Disco/BeanFactoryConfigurationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
namespace bitExpert\Disco;

use bitExpert\Disco\Store\SerializableBeanStore;
use Doctrine\Common\Cache\FilesystemCache;
use Doctrine\Common\Cache\VoidCache;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use ProxyManager\Autoloader\Autoloader;
use ProxyManager\Autoloader\AutoloaderInterface;
Expand Down Expand Up @@ -90,6 +89,30 @@ public function enablingProxyAutoloaderRegistersAdditionalAutoloader()
count($autoloaderFunctionsBeforeBeanFactoryInit) + 1,
$autoloaderFunctionsAfterBeanFactoryInit
);
self::assertNotContains($autoloader, $autoloaderFunctionsBeforeBeanFactoryInit);
self::assertContains($autoloader, $autoloaderFunctionsAfterBeanFactoryInit);
}

/**
* @test
*/
public function existingProxyAutoloaderCanBeUnregistered()
{
$autoloader1 = $this->createMock(AutoloaderInterface::class);
$autoloader2 = $this->createMock(AutoloaderInterface::class);

$config = new BeanFactoryConfiguration(sys_get_temp_dir());

// Set first proxy autoloader
$config->setProxyAutoloader($autoloader1);
// Set second proxy autoloader to unregister the first one
$config->setProxyAutoloader($autoloader2);

$proxyManagerConfig = $config->getProxyManagerConfiguration();

self::assertSame($autoloader2, $proxyManagerConfig->getProxyAutoloader());
self::assertContains($autoloader2, spl_autoload_functions());
self::assertNotContains($autoloader1, spl_autoload_functions());
}

/**
Expand All @@ -104,4 +127,27 @@ public function configuredBeanStoreInstanceCanBererieved()

self::assertSame($beanStore, $config->getSessionBeanStore());
}

/**
* @test
* @expectedException \InvalidArgumentException
* @expectedExceptionCode 10
*/
public function injectedInvalidProxyTargetDirThrowsException()
{
$config = new BeanFactoryConfiguration(sys_get_temp_dir());
$config->setProxyTargetDir('/abc');
}

/**
* @test
* @expectedException \InvalidArgumentException
* @expectedExceptionCode 20
*/
public function injectedNotWritableProxyTargetDirThrowsException()
{
$config = new BeanFactoryConfiguration(sys_get_temp_dir());
$path = vfsStream::setup('root', 0x111);
$config->setProxyTargetDir($path->url());
}
}
39 changes: 39 additions & 0 deletions tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,45 @@ public function sampleServiceWithParam($test = '') : SampleService
return $service;
}

/**
* @Bean({"singleton"=false})
* @Parameters({
* @Parameter({"name" = "test", "default" = null})
* })
*/
public function sampleServiceWithParamNull($test = '') : SampleService
{
$service = new SampleService();
$service->setTest($test);
return $service;
}

/**
* @Bean({"singleton"=false})
* @Parameters({
* @Parameter({"name" = "test", "default" = true})
* })
*/
public function sampleServiceWithParamBool($test = '') : SampleService
{
$service = new SampleService();
$service->setTest($test);
return $service;
}

/**
* @Bean({"singleton"=false})
* @Parameters({
* @Parameter({"name" = "test", "default" = 0})
* })
*/
public function sampleServiceWithParamEmpty($test = '') : SampleService
{
$service = new SampleService();
$service->setTest($test);
return $service;
}

/**
* @Bean({"singleton"=false})
* @Parameters({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ public function sameAliasUsedForMultipleBeansThrowsException()
$this->configGenerator->generate($reflClass, $this->classGenerator);
}

/**
* @test
* @expectedException \ProxyManager\Exception\InvalidProxiedClassException
* @expectedExceptionMessageRegExp /^\[Semantical Error\] The annotation "@foo"/
*/
public function unknownAnnotationThrowsException()
{
/**
* @foo
*/
$configObject = new class {
public function foo() : string
{
return 'foo';
}
};
$reflClass = new \ReflectionObject($configObject);
$this->configGenerator->generate($reflClass, $this->classGenerator);
}

/**
* @test
*/
Expand Down
42 changes: 42 additions & 0 deletions tests/bitExpert/Disco/Proxy/LazyBean/LazyBeanGeneratorUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Disco package.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

namespace bitExpert\Disco\Proxy\LazyBean;

use Iterator;
use ProxyManager\Proxy\VirtualProxyInterface;
use ReflectionClass;
use PHPUnit\Framework\TestCase;
use Zend\Code\Generator\ClassGenerator;

/**
* Unit tests for {@link \bitExpert\Disco\Proxy\LazyBean\LazyBeanGenerator}.
*/
class LazyBeanGeneratorUnitTest extends TestCase
{
/**
* @test
*/
public function generateWithInterfaceAsOriginalObject()
{
$classGenerator = $this->createMock(ClassGenerator::class);
$classGenerator->expects(self::once())
->method('setImplementedInterfaces')
->with(array(VirtualProxyInterface::class, Iterator::class));

$reflectionClass = new ReflectionClass(Iterator::class);
self::assertTrue($reflectionClass->isInterface());

$proxyGenerator = new LazyBeanGenerator();
$proxyGenerator->generate($reflectionClass, $classGenerator);
}
}

0 comments on commit 2e41463

Please sign in to comment.