Restore working directory if test fails (#1490)

This test changes directory then performs an assertion.
No problem if the assertion succeeds. I was a little concerned about
what would happen if the assertion fails, leaving us in the
new directory. So I have changed test to use setUp/tearDown
to ensure that we end up where we started.
This commit is contained in:
oleibman 2020-05-24 03:54:59 -07:00 committed by GitHub
parent 84e03da5c7
commit 9947de3b89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 2 deletions

View File

@ -9,6 +9,18 @@ use PhpOffice\PhpSpreadsheetTests\Functional;
class ImagesRootTest extends Functional\AbstractFunctional class ImagesRootTest extends Functional\AbstractFunctional
{ {
private $curdir;
protected function setUp(): void
{
$this->curdir = getcwd();
}
protected function tearDown(): void
{
chdir($this->curdir);
}
public function testImagesRoot(): void public function testImagesRoot(): void
{ {
$spreadsheet = new Spreadsheet(); $spreadsheet = new Spreadsheet();
@ -20,7 +32,6 @@ class ImagesRootTest extends Functional\AbstractFunctional
$newdir = __DIR__ . '/../../../data/Reader/HTML'; $newdir = __DIR__ . '/../../../data/Reader/HTML';
$stub = 'image.jpg'; $stub = 'image.jpg';
$imagePath = "./$stub"; $imagePath = "./$stub";
$curdir = getcwd();
chdir($newdir); chdir($newdir);
self::assertFileExists($imagePath); self::assertFileExists($imagePath);
$drawing->setPath($imagePath); $drawing->setPath($imagePath);
@ -34,7 +45,6 @@ class ImagesRootTest extends Functional\AbstractFunctional
$writer = new Html($spreadsheet); $writer = new Html($spreadsheet);
$writer->setImagesRoot($root); $writer->setImagesRoot($root);
$html = $writer->generateHTMLAll(); $html = $writer->generateHTMLAll();
chdir($curdir);
$dom = new DOMDocument(); $dom = new DOMDocument();
$dom->loadHTML($html); $dom->loadHTML($html);
$body = $dom->getElementsByTagName('body')[0]; $body = $dom->getElementsByTagName('body')[0];