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

In a dockerized environment, FileWriterGeneratorStrategyTest#testGenerateWillFailIfTmpFileCannotBeWrittenToDisk() fails #653

Open
Ocramius opened this issue Dec 30, 2020 · 1 comment
Labels

Comments

@Ocramius
Copy link
Owner

To reproduce:

FROM php:7.4-cli

VOLUME /app

WORKDIR /app
docker build -t foo .
docker run -ti -v $(pwd):/app foo vendor/bin/phpunit

The error is:

1) ProxyManagerTest\GeneratorStrategy\FileWriterGeneratorStrategyTest::testGenerateWillFailIfTmpFileCannotBeWrittenToDisk
Failed asserting that exception of type "ProxyManager\Exception\FileNotWritableException" is thrown.

Perhaps the test is a bit weak: @michalbundyra do you know how to harden this test?

private string $tempDir;
private Closure $originalErrorHandler;
protected function setUp(): void
{
parent::setUp();
$this->tempDir = tempnam(sys_get_temp_dir(), 'FileWriterGeneratorStrategyTest');
$this->originalErrorHandler = static function (): bool {
throw new ErrorException();
};
unlink($this->tempDir);
mkdir($this->tempDir);
set_error_handler($this->originalErrorHandler);
}

public function testGenerateWillFailIfTmpFileCannotBeWrittenToDisk(): void
{
$tmpDirPath = $this->tempDir . '/' . uniqid('nonWritable', true);
mkdir($tmpDirPath, 0555, true);
$locator = $this->createMock(FileLocatorInterface::class);
$generator = new FileWriterGeneratorStrategy($locator);
$tmpFile = $tmpDirPath . '/' . uniqid('FileWriterGeneratorStrategyFailedFileWriteTest', true) . '.php';
$namespace = 'Foo';
$className = UniqueIdentifierGenerator::getIdentifier('Bar');
$fqcn = $namespace . '\\' . $className;
$locator
->method('getProxyFileName')
->with($fqcn)
->willReturn($tmpFile);
$this->expectException(FileNotWritableException::class);
$generator->generate(new ClassGenerator($fqcn));
}

The source being tested:

public function generate(ClassGenerator $classGenerator): string
{
$generatedCode = $classGenerator->generate();
assert(is_string($generatedCode));
$className = $classGenerator->getNamespaceName() . '\\' . $classGenerator->getName();
$fileName = $this->fileLocator->getProxyFileName($className);
set_error_handler($this->emptyErrorHandler);
try {
FileWriter::writeFile($fileName, "<?php\n\n" . $generatedCode);
return $generatedCode;
} catch (FileWriterException $e) {
throw FileNotWritableException::fromPrevious($e);
} finally {
restore_error_handler();
}
}

@Ocramius Ocramius added the bug label Dec 30, 2020
@Ocramius
Copy link
Owner Author

Note: I just noticed this because I no longer run PHP 7.x on my systems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant