Skip to content

Commit

Permalink
Add test assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Saggre committed Apr 1, 2024
1 parent 7a019e7 commit 50851d2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
24 changes: 7 additions & 17 deletions tests/integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@

namespace PhpDocumentorMarkdown\Test\Integration;

use phpDocumentor\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

class IntegrationTest extends IntegrationTestCase
{
public function testTest()
{
$input = new ArrayInput([
'--directory' => [$this->sourceDir],
'--target' => $this->targetDir,
'--template' => $this->templateDir,
'--no-interaction' => true,
'--cache-folder' => $this->cacheDir,
'-vvv' => true,
]);

$output = new BufferedOutput();

$this->application->run($input, $output);
$this->runApplication();

echo $output->fetch();
$this->assertFileExists("{$this->targetDir}/Home.md");
$this->assertFileContainsString(
'[`AbstractProduct`]',
"{$this->targetDir}/Home.md",
'File contains class documentation'
);
}
}
55 changes: 55 additions & 0 deletions tests/integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use phpDocumentor\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Filesystem\Filesystem;

class IntegrationTestCase extends KernelTestCase
Expand Down Expand Up @@ -31,6 +34,8 @@ class IntegrationTestCase extends KernelTestCase
*/
protected string $targetDir;

protected array $cachedFiles;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -46,6 +51,8 @@ protected function setUp(): void
$this->application = $this->createApplication();
$this->filesystem = new Filesystem();

$this->cachedFiles = [];

$this->setUpTestsTmpDir();
}

Expand All @@ -62,4 +69,52 @@ protected function setUpTestsTmpDir(): void

$this->filesystem->mkdir($this->tmpDir);
}

/**
* Assert that a file contains a string.
*
* @param string $needle
* @param string $path
* @param string $message
* @return void
*/
protected function assertFileContainsString(string $needle, string $path, string $message = ''): void
{
if (!$this->filesystem->exists($path)) {
$this->fail("$path does not exist");
}

$haystack = file_get_contents($path);
$this->assertStringContainsString($needle, $haystack, $message);
}

/**
* Run PhpDocumentor.
*
* @param array $input
* @param BufferedOutput|null $output
* @return void
*/
protected function runApplication(array $input = [], ?BufferedOutput $output = null): void
{
if ($output === null) {
$output = new BufferedOutput();
}

$_input = new ArrayInput(array_merge($input, [
'--directory' => [$this->sourceDir],
'--target' => $this->targetDir,
'--template' => $this->templateDir,
'--no-interaction' => true,
'--cache-folder' => $this->cacheDir,
'-vvv' => true,
]));

try {
$command = $this->application->find('project:run');
$command->run($_input, $output);
} catch (ExceptionInterface $e) {
$this->fail($e->getMessage());
}
}
}

0 comments on commit 50851d2

Please sign in to comment.