Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve usage of phpunit #442

Merged
merged 1 commit into from Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions tests/ProxyManagerTest/Autoloader/AutoloaderTest.php
Expand Up @@ -56,7 +56,7 @@ public function testWillNotAutoloadUserClasses() : void
->expects(self::once())
->method('isProxyClassName')
->with($className)
->will(self::returnValue(false));
->willReturn(false);

self::assertFalse($this->autoloadWithoutFurtherAutoloaders($className));
}
Expand All @@ -72,12 +72,12 @@ public function testWillNotAutoloadNonExistingClass() : void
->expects(self::once())
->method('isProxyClassName')
->with($className)
->will(self::returnValue(true));
->willReturn(true);
$this
->fileLocator
->expects(self::once())
->method('getProxyFileName')
->will(self::returnValue(__DIR__ . '/non-existing'));
->willReturn(__DIR__ . '/non-existing');

self::assertFalse($this->autoloadWithoutFurtherAutoloaders($className));
}
Expand All @@ -98,7 +98,7 @@ public function testWillAutoloadExistingFile() : void
$namespace = 'Foo';
$className = UniqueIdentifierGenerator::getIdentifier('Bar');
$fqcn = $namespace . '\\' . $className;
$fileName = sys_get_temp_dir() . '/foo_' . uniqid() . '.php';
$fileName = sys_get_temp_dir() . '/foo_' . uniqid('file', true) . '.php';

file_put_contents($fileName, '<?php namespace ' . $namespace . '; class ' . $className . '{}');

Expand All @@ -107,12 +107,12 @@ public function testWillAutoloadExistingFile() : void
->expects(self::once())
->method('isProxyClassName')
->with($fqcn)
->will(self::returnValue(true));
->willReturn(true);
$this
->fileLocator
->expects(self::once())
->method('getProxyFileName')
->will(self::returnValue($fileName));
->willReturn($fileName);

self::assertTrue($this->autoloadWithoutFurtherAutoloaders($fqcn));
self::assertTrue(class_exists($fqcn, false));
Expand Down
12 changes: 8 additions & 4 deletions tests/ProxyManagerTest/ConfigurationTest.php
Expand Up @@ -13,7 +13,6 @@
use ProxyManager\Signature\ClassSignatureGeneratorInterface;
use ProxyManager\Signature\SignatureCheckerInterface;
use ProxyManager\Signature\SignatureGeneratorInterface;
use function is_dir;

/**
* Tests for {@see \ProxyManager\Configuration}
Expand Down Expand Up @@ -55,6 +54,7 @@ public function testGetSetProxiesNamespace() : void
*/
public function testSetGetClassNameInflector() : void
{
/** @noinspection UnnecessaryAssertionInspection */
self::assertInstanceOf(ClassNameInflectorInterface::class, $this->configuration->getClassNameInflector());

/** @var ClassNameInflectorInterface $inflector */
Expand All @@ -78,6 +78,7 @@ public function testDefaultGeneratorStrategyNeedToBeAInstanceOfEvaluatingGenerat
*/
public function testSetGetGeneratorStrategy() : void
{
/** @noinspection UnnecessaryAssertionInspection */
self::assertInstanceOf(GeneratorStrategyInterface::class, $this->configuration->getGeneratorStrategy());

/** @var GeneratorStrategyInterface $strategy */
Expand All @@ -93,7 +94,7 @@ public function testSetGetGeneratorStrategy() : void
*/
public function testSetGetProxiesTargetDir() : void
{
self::assertTrue(is_dir($this->configuration->getProxiesTargetDir()));
self::assertDirectoryExists($this->configuration->getProxiesTargetDir());

$this->configuration->setProxiesTargetDir(__DIR__);
self::assertSame(__DIR__, $this->configuration->getProxiesTargetDir());
Expand All @@ -105,6 +106,7 @@ public function testSetGetProxiesTargetDir() : void
*/
public function testSetGetProxyAutoloader() : void
{
/** @noinspection UnnecessaryAssertionInspection */
self::assertInstanceOf(AutoloaderInterface::class, $this->configuration->getProxyAutoloader());

/** @var AutoloaderInterface $autoloader */
Expand All @@ -120,7 +122,8 @@ public function testSetGetProxyAutoloader() : void
*/
public function testSetGetSignatureGenerator() : void
{
self::assertInstanceOf(SignatureGeneratorInterface::class, $this->configuration->getSignatureGenerator());
malukenho marked this conversation as resolved.
Show resolved Hide resolved
/** @noinspection UnnecessaryAssertionInspection */
self::assertInstanceOf(SignatureCheckerInterface::class, $this->configuration->getSignatureChecker());

/** @var SignatureGeneratorInterface $signatureGenerator */
$signatureGenerator = $this->createMock(SignatureGeneratorInterface::class);
Expand All @@ -135,6 +138,7 @@ public function testSetGetSignatureGenerator() : void
*/
public function testSetGetSignatureChecker() : void
{
/** @noinspection UnnecessaryAssertionInspection */
self::assertInstanceOf(SignatureCheckerInterface::class, $this->configuration->getSignatureChecker());

/** @var SignatureCheckerInterface $signatureChecker */
Expand All @@ -150,11 +154,11 @@ public function testSetGetSignatureChecker() : void
*/
public function testSetGetClassSignatureGenerator() : void
{
/** @noinspection UnnecessaryAssertionInspection */
self::assertInstanceOf(
ClassSignatureGeneratorInterface::class,
$this->configuration->getClassSignatureGenerator()
);

/** @var ClassSignatureGeneratorInterface $classSignatureGenerator */
$classSignatureGenerator = $this->createMock(ClassSignatureGeneratorInterface::class);

Expand Down
Expand Up @@ -19,7 +19,6 @@ public function testFromInvalidMoveOperation() : void
{
$exception = FileNotWritableException::fromInvalidMoveOperation('/tmp/a', '/tmp/b');

self::assertInstanceOf(FileNotWritableException::class, $exception);
self::assertSame(
'Could not move file "/tmp/a" to location "/tmp/b": either the source file is not readable,'
. ' or the destination is not writable',
Expand All @@ -31,7 +30,6 @@ public function testFromNotWritableDirectory() : void
{
$exception = FileNotWritableException::fromNotWritableDirectory('/tmp/a');

self::assertInstanceOf(FileNotWritableException::class, $exception);
self::assertSame(
'Could not create temp file in directory "/tmp/a" '
. 'either the directory does not exist, or it is not writable',
Expand Down
23 changes: 8 additions & 15 deletions tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php
Expand Up @@ -68,39 +68,33 @@ protected function setUp() : void
$this->classSignatureGenerator = $this->createMock(ClassSignatureGeneratorInterface::class);

$configuration
->expects(self::any())
->method('getClassNameInflector')
->will(self::returnValue($this->classNameInflector));
->willReturn($this->classNameInflector);

$configuration
->expects(self::any())
->method('getGeneratorStrategy')
->will(self::returnValue($this->generatorStrategy));
->willReturn($this->generatorStrategy);

$configuration
->expects(self::any())
->method('getProxyAutoloader')
->will(self::returnValue($this->proxyAutoloader));
->willReturn($this->proxyAutoloader);

$configuration
->expects(self::any())
->method('getSignatureChecker')
->will(self::returnValue($this->signatureChecker));
->willReturn($this->signatureChecker);

$configuration
->expects(self::any())
->method('getClassSignatureGenerator')
->will(self::returnValue($this->classSignatureGenerator));
->willReturn($this->classSignatureGenerator);

$this
->classNameInflector
->expects(self::any())
->method('getUserClassName')
->will(self::returnValue('stdClass'));
->willReturn('stdClass');

$this->factory = $this->getMockForAbstractClass(AbstractBaseFactory::class, [$configuration]);

$this->factory->expects(self::any())->method('getGenerator')->will(self::returnValue($this->generator));
$this->factory->method('getGenerator')->willReturn($this->generator);
}

public function testGeneratesClass() : void
Expand All @@ -112,10 +106,9 @@ public function testGeneratesClass() : void

$this
->classNameInflector
->expects(self::any())
->method('getProxyClassName')
->with('stdClass')
->will(self::returnValue($generatedClass));
->willReturn($generatedClass);

$this
->generatorStrategy
Expand Down
Expand Up @@ -51,21 +51,18 @@ protected function setUp() : void

$this
->config
->expects(self::any())
->method('getClassNameInflector')
->will(self::returnValue($this->inflector));
->willReturn($this->inflector);

$this
->config
->expects(self::any())
->method('getSignatureChecker')
->will(self::returnValue($this->signatureChecker));
->willReturn($this->signatureChecker);

$this
->config
->expects(self::any())
->method('getClassSignatureGenerator')
->will(self::returnValue($this->classSignatureGenerator));
->willReturn($this->classSignatureGenerator);
}

/**
Expand Down Expand Up @@ -96,7 +93,7 @@ public function testWillSkipAutoGeneration() : void
->expects(self::once())
->method('getProxyClassName')
->with('stdClass')
->will(self::returnValue(AccessInterceptorValueHolderMock::class));
->willReturn(AccessInterceptorValueHolderMock::class);

$factory = new AccessInterceptorScopeLocalizerFactory($this->config);
$prefixInterceptors = [static function () : void {
Expand Down Expand Up @@ -132,8 +129,8 @@ public function testWillTryAutoGeneration() : void
$generator = $this->createMock(GeneratorStrategyInterface::class);
$autoloader = $this->createMock(AutoloaderInterface::class);

$this->config->expects(self::any())->method('getGeneratorStrategy')->will(self::returnValue($generator));
$this->config->expects(self::any())->method('getProxyAutoloader')->will(self::returnValue($autoloader));
$this->config->method('getGeneratorStrategy')->willReturn($generator);
$this->config->method('getProxyAutoloader')->willReturn($autoloader);

$generator
->expects(self::once())
Expand Down Expand Up @@ -165,14 +162,14 @@ static function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
->expects(self::once())
->method('getProxyClassName')
->with('stdClass')
->will(self::returnValue($proxyClassName));
->willReturn($proxyClassName);

$this
->inflector
->expects(self::once())
->method('getUserClassName')
->with('stdClass')
->will(self::returnValue(LazyLoadingMock::class));
->willReturn(LazyLoadingMock::class);

$this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
$this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
Expand All @@ -189,7 +186,9 @@ static function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
/** @var AccessInterceptorValueHolderMock $proxy */
$proxy = $factory->createProxy($instance, $prefixInterceptors, $suffixInterceptors);

/** @noinspection UnnecessaryAssertionInspection */
self::assertInstanceOf($proxyClassName, $proxy);

self::assertSame($instance, $proxy->instance);
self::assertSame($prefixInterceptors, $proxy->prefixInterceptors);
self::assertSame($suffixInterceptors, $proxy->suffixInterceptors);
Expand Down
Expand Up @@ -50,21 +50,18 @@ protected function setUp() : void

$this
->config
->expects(self::any())
->method('getClassNameInflector')
->will(self::returnValue($this->inflector));
->willReturn($this->inflector);

$this
->config
->expects(self::any())
->method('getSignatureChecker')
->will(self::returnValue($this->signatureChecker));
->willReturn($this->signatureChecker);

$this
->config
->expects(self::any())
->method('getClassSignatureGenerator')
->will(self::returnValue($this->classSignatureGenerator));
->willReturn($this->classSignatureGenerator);
}

/**
Expand Down Expand Up @@ -95,7 +92,7 @@ public function testWillSkipAutoGeneration() : void
->expects(self::once())
->method('getProxyClassName')
->with('stdClass')
->will(self::returnValue(AccessInterceptorValueHolderMock::class));
->willReturn(AccessInterceptorValueHolderMock::class);

$factory = new AccessInterceptorValueHolderFactory($this->config);
$prefixInterceptors = [static function () : void {
Expand Down Expand Up @@ -131,8 +128,8 @@ public function testWillTryAutoGeneration() : void
$generator = $this->createMock(GeneratorStrategyInterface::class);
$autoloader = $this->createMock(AutoloaderInterface::class);

$this->config->expects(self::any())->method('getGeneratorStrategy')->will(self::returnValue($generator));
$this->config->expects(self::any())->method('getProxyAutoloader')->will(self::returnValue($autoloader));
$this->config->method('getGeneratorStrategy')->will(self::returnValue($generator));
$this->config->method('getProxyAutoloader')->will(self::returnValue($autoloader));

$generator
->expects(self::once())
Expand Down Expand Up @@ -164,14 +161,14 @@ static function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
->expects(self::once())
->method('getProxyClassName')
->with('stdClass')
->will(self::returnValue($proxyClassName));
->willReturn($proxyClassName);

$this
->inflector
->expects(self::once())
->method('getUserClassName')
->with('stdClass')
->will(self::returnValue(EmptyClass::class));
->willReturn(EmptyClass::class);

$this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
$this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
Expand Down
20 changes: 8 additions & 12 deletions tests/ProxyManagerTest/Factory/LazyLoadingGhostFactoryTest.php
Expand Up @@ -48,21 +48,18 @@ protected function setUp() : void

$this
->config
->expects(self::any())
->method('getClassNameInflector')
->will(self::returnValue($this->inflector));
->willReturn($this->inflector);

$this
->config
->expects(self::any())
->method('getSignatureChecker')
->will(self::returnValue($this->signatureChecker));
->willReturn($this->signatureChecker);

$this
->config
->expects(self::any())
->method('getClassSignatureGenerator')
->will(self::returnValue($this->classSignatureGenerator));
->willReturn($this->classSignatureGenerator);
}

/**
Expand Down Expand Up @@ -92,7 +89,7 @@ public function testWillSkipAutoGeneration() : void
->expects(self::once())
->method('getProxyClassName')
->with($className)
->will(self::returnValue(LazyLoadingMock::class));
->willReturn(LazyLoadingMock::class);

$factory = new LazyLoadingGhostFactory($this->config);
$initializer = static function () : void {
Expand Down Expand Up @@ -120,8 +117,8 @@ public function testWillTryAutoGeneration() : void
$generator = $this->createMock(GeneratorStrategyInterface::class);
$autoloader = $this->createMock(AutoloaderInterface::class);

$this->config->expects(self::any())->method('getGeneratorStrategy')->will(self::returnValue($generator));
$this->config->expects(self::any())->method('getProxyAutoloader')->will(self::returnValue($autoloader));
$this->config->method('getGeneratorStrategy')->willReturn($generator);
$this->config->method('getProxyAutoloader')->willReturn($autoloader);

$generator
->expects(self::once())
Expand Down Expand Up @@ -150,14 +147,14 @@ static function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
->expects(self::once())
->method('getProxyClassName')
->with($className)
->will(self::returnValue($proxyClassName));
->willReturn($proxyClassName);

$this
->inflector
->expects(self::once())
->method('getUserClassName')
->with($className)
->will(self::returnValue(LazyLoadingMock::class));
->willReturn(LazyLoadingMock::class);

$this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
$this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
Expand All @@ -168,7 +165,6 @@ static function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
/** @var LazyLoadingMock $proxy */
$proxy = $factory->createProxy($className, $initializer);

self::assertInstanceOf($proxyClassName, $proxy);
self::assertSame($initializer, $proxy->initializer);
}
}