Skip to content

Commit

Permalink
Improve usage of phpunit
Browse files Browse the repository at this point in the history
- Remove unnecessary assertions
- Avoid using deprecated assertions
(sebastianbergmann/phpunit#3338)

Signed-off-by: Jefersson Nathan <malukenho.dev@gmail.com>
  • Loading branch information
malukenho committed Feb 27, 2019
1 parent 6a6c808 commit 70ba51d
Show file tree
Hide file tree
Showing 86 changed files with 313 additions and 378 deletions.
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
18 changes: 1 addition & 17 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,8 +54,6 @@ public function testGetSetProxiesNamespace() : void
*/
public function testSetGetClassNameInflector() : void
{
self::assertInstanceOf(ClassNameInflectorInterface::class, $this->configuration->getClassNameInflector());

/** @var ClassNameInflectorInterface $inflector */
$inflector = $this->createMock(ClassNameInflectorInterface::class);

Expand All @@ -78,8 +75,6 @@ public function testDefaultGeneratorStrategyNeedToBeAInstanceOfEvaluatingGenerat
*/
public function testSetGetGeneratorStrategy() : void
{
self::assertInstanceOf(GeneratorStrategyInterface::class, $this->configuration->getGeneratorStrategy());

/** @var GeneratorStrategyInterface $strategy */
$strategy = $this->createMock(GeneratorStrategyInterface::class);

Expand All @@ -93,7 +88,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,8 +100,6 @@ public function testSetGetProxiesTargetDir() : void
*/
public function testSetGetProxyAutoloader() : void
{
self::assertInstanceOf(AutoloaderInterface::class, $this->configuration->getProxyAutoloader());

/** @var AutoloaderInterface $autoloader */
$autoloader = $this->createMock(AutoloaderInterface::class);

Expand All @@ -120,8 +113,6 @@ public function testSetGetProxyAutoloader() : void
*/
public function testSetGetSignatureGenerator() : void
{
self::assertInstanceOf(SignatureGeneratorInterface::class, $this->configuration->getSignatureGenerator());

/** @var SignatureGeneratorInterface $signatureGenerator */
$signatureGenerator = $this->createMock(SignatureGeneratorInterface::class);

Expand All @@ -135,8 +126,6 @@ public function testSetGetSignatureGenerator() : void
*/
public function testSetGetSignatureChecker() : void
{
self::assertInstanceOf(SignatureCheckerInterface::class, $this->configuration->getSignatureChecker());

/** @var SignatureCheckerInterface $signatureChecker */
$signatureChecker = $this->createMock(SignatureCheckerInterface::class);

Expand All @@ -150,11 +139,6 @@ public function testSetGetSignatureChecker() : void
*/
public function testSetGetClassSignatureGenerator() : void
{
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->expects(self::any())->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->expects(self::any())->method('getGeneratorStrategy')->willReturn($generator);
$this->config->expects(self::any())->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,6 @@ static function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
/** @var AccessInterceptorValueHolderMock $proxy */
$proxy = $factory->createProxy($instance, $prefixInterceptors, $suffixInterceptors);

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 @@ -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->expects(self::any())->method('getGeneratorStrategy')->willReturn($generator);
$this->config->expects(self::any())->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);
}
}

0 comments on commit 70ba51d

Please sign in to comment.