Skip to content

Commit

Permalink
Added 'AbstractTestCase::getFixtureContents()'. In the process, got '…
Browse files Browse the repository at this point in the history
…AbstractTestCaseTest' working.
  • Loading branch information
Daniel Bettles committed Jan 21, 2023
1 parent 0bb9552 commit 956acb5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

No unreleased changes.

## [2.4.0] - 2023-01-21

### Added

- Added `AbstractTestCase::getFixtureContents()`, which returns the contents of a fixture file.

### Fixed

- Got `AbstractTestCaseTest` working 🤦‍♂️

## [2.3.3] - 2023-01-17

### Fixed
Expand Down Expand Up @@ -70,7 +80,8 @@ No unreleased changes.

First stable release.

[unreleased]: https://github.com/danbettles/marigold/compare/v2.3.3...HEAD
[unreleased]: https://github.com/danbettles/marigold/compare/v2.4.0...HEAD
[2.4.0]: https://github.com/danbettles/marigold/compare/v2.3.3...v2.4.0
[2.3.3]: https://github.com/danbettles/marigold/compare/v2.3.2...v2.3.3
[2.3.2]: https://github.com/danbettles/marigold/compare/v2.3.1...v2.3.2
[2.3.1]: https://github.com/danbettles/marigold/compare/v2.3.0...v2.3.1
Expand Down
19 changes: 19 additions & 0 deletions src/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace DanBettles\Marigold;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

use function array_pop;
use function explode;
use function file_get_contents;
use function get_class;
use function implode;
use function is_file;
use function preg_replace;
use function strlen;
use function substr;
Expand Down Expand Up @@ -46,6 +49,7 @@ public function __construct(
$fixturesDir = (
self::$testsDir .
DIRECTORY_SEPARATOR .
// (Forward slash or backslash.)
preg_replace('~[\x2F\x5C]~', DIRECTORY_SEPARATOR, $relativeClassName)
);

Expand Down Expand Up @@ -76,6 +80,21 @@ protected function createFixturePathname(string $basename): string
return $this->getFixturesDir() . DIRECTORY_SEPARATOR . $basename;
}

/**
* @throws InvalidArgumentException If the file does not exist.
*/
protected function getFixtureContents(string $basename): string
{
$fixturePathname = $this->createFixturePathname($basename);

if (!is_file($fixturePathname)) {
throw new InvalidArgumentException("File `{$fixturePathname}` does not exist.");
}

/** @var string */
return file_get_contents($fixturePathname);
}

/**
* @phpstan-param class-string $className
*/
Expand Down
9 changes: 2 additions & 7 deletions tests/src/.bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
<?php

use DanBettles\Marigold\AbstractTestCase;
use DanBettles\Marigold\Tests\AbstractTestCaseTest;

require __DIR__ . '/../../vendor/autoload.php';

(function () {
$classAtRootOfTests = new ReflectionClass(AbstractTestCaseTest::class);

AbstractTestCase::$testsNamespace = $classAtRootOfTests->getNamespaceName();
AbstractTestCase::$testsDir = dirname($classAtRootOfTests->getFileName());
})();
AbstractTestCase::$testsNamespace = 'DanBettles\Marigold\Tests';
AbstractTestCase::$testsDir = __DIR__;
14 changes: 14 additions & 0 deletions tests/src/AbstractTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DanBettles\Marigold\Tests;

use DanBettles\Marigold\AbstractTestCase;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

Expand Down Expand Up @@ -53,4 +54,17 @@ public function testGettestedclass(): void
$this->assertInstanceOf(ReflectionClass::class, $testedClass);
$this->assertSame(AbstractTestCase::class, $testedClass->getName());
}

public function testGetfixturecontentsReturnsTheContentsOfTheFixture(): void
{
$this->assertSame('Lorem ipsum dolor.', $this->getFixtureContents('fixture.txt'));
}

public function testGetfixturecontentsThrowsAnExceptionIfTheFixtureDoesNotExist(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('~^File `[^`]+` does not exist.$~');

$this->getFixtureContents('non_existent');
}
}
1 change: 1 addition & 0 deletions tests/src/AbstractTestCaseTest/fixture.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor.

0 comments on commit 956acb5

Please sign in to comment.