Coverage for Helper/Samples (#1920)
* Coverage for Helper/Samples I was perplexed by the fact that Helper/Samples seemed to be entirely uncovered when running the test suite, since I know all the samples are run as part of the test. I think that what must be happening is that the Helper code is invoked mostly as part of a Data Provider (and therefore not counted), not as part of the test proper (which would count). So, this change adds a small number of tests which result in Samples being 100% covered. Covering one statement was tricky - simulating the inability to create a test directory. Mocking, a technique I have not used before, solves this problem admirably. * Suggestions From Mark Baker Tests changed from assertEquals to assertSame. Added @covers annotation to test class. Validate parameter for method being mocked.
This commit is contained in:
parent
ed62526aca
commit
7e071e8abc
|
|
@ -71,7 +71,7 @@ class Sample
|
||||||
/**
|
/**
|
||||||
* Returns an array of all known samples.
|
* Returns an array of all known samples.
|
||||||
*
|
*
|
||||||
* @return string[] [$name => $path]
|
* @return string[][] [$name => $path]
|
||||||
*/
|
*/
|
||||||
public function getSamples()
|
public function getSamples()
|
||||||
{
|
{
|
||||||
|
|
@ -132,6 +132,11 @@ class Sample
|
||||||
$this->logEndingNotes();
|
$this->logEndingNotes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function isDirOrMkdir(string $folder): bool
|
||||||
|
{
|
||||||
|
return \is_dir($folder) || \mkdir($folder);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the temporary directory and make sure it exists.
|
* Returns the temporary directory and make sure it exists.
|
||||||
*
|
*
|
||||||
|
|
@ -140,11 +145,9 @@ class Sample
|
||||||
private function getTemporaryFolder()
|
private function getTemporaryFolder()
|
||||||
{
|
{
|
||||||
$tempFolder = sys_get_temp_dir() . '/phpspreadsheet';
|
$tempFolder = sys_get_temp_dir() . '/phpspreadsheet';
|
||||||
if (!is_dir($tempFolder)) {
|
if (!$this->isDirOrMkdir($tempFolder)) {
|
||||||
if (!mkdir($tempFolder) && !is_dir($tempFolder)) {
|
|
||||||
throw new RuntimeException(sprintf('Directory "%s" was not created', $tempFolder));
|
throw new RuntimeException(sprintf('Directory "%s" was not created', $tempFolder));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $tempFolder;
|
return $tempFolder;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpSpreadsheetTests\Helper;
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\Helper\Sample;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \PhpOffice\PhpSpreadsheet\Helper\Sample
|
||||||
|
*/
|
||||||
|
class SampleCoverageTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testSample(): void
|
||||||
|
{
|
||||||
|
$helper = new Sample();
|
||||||
|
$samples = $helper->getSamples();
|
||||||
|
self::assertArrayHasKey('Basic', $samples);
|
||||||
|
$basic = $samples['Basic'];
|
||||||
|
self::assertArrayHasKey('02 Types', $basic);
|
||||||
|
self::assertSame('Basic/02_Types.php', $basic['02 Types']);
|
||||||
|
self::assertSame('phpunit', $helper->getPageTitle());
|
||||||
|
self::assertSame('<h1>phpunit</h1>', $helper->getPageHeading());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDirectoryFail(): void
|
||||||
|
{
|
||||||
|
$this->expectException(RuntimeException::class);
|
||||||
|
|
||||||
|
$helper = $this->getMockBuilder(Sample::class)
|
||||||
|
->onlyMethods(['isDirOrMkdir'])
|
||||||
|
->getMock();
|
||||||
|
$helper->expects(self::once())
|
||||||
|
->method('isDirOrMkdir')
|
||||||
|
->with(self::isType('string'))
|
||||||
|
->willReturn(false);
|
||||||
|
self::assertSame('', $helper->getFilename('a.xlsx'));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue