Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Helper/SampleCoverageTest.php
oleibman 7e071e8abc 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.
2021-03-14 20:04:07 +01:00

40 lines
1.2 KiB
PHP

<?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'));
}
}