1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
<?php
declare(strict_types=1);
namespace League\HTMLToMarkdown\Test;
use PHPUnit\Framework\TestCase;
use mikehaertl\shellcommand\Command;
class BinTest extends TestCase
{
/**
* Tests the behavior of not providing any HTML input
*/
public function testNoArgsOrStdin(): void
{
$cmd = new Command($this->getPathToCommonmark());
$cmd->execute();
$this->assertEquals(1, $cmd->getExitCode());
$this->assertEmpty($cmd->getOutput());
$this->assertStringContainsString('Usage:', $cmd->getError());
}
/**
* Tests the -h flag
*/
public function testHelpShortFlag(): void
{
$cmd = new Command($this->getPathToCommonmark());
$cmd->addArg('-h');
$cmd->execute();
$this->assertEquals(0, $cmd->getExitCode());
$this->assertStringContainsString('Usage:', $cmd->getOutput());
}
/**
* Tests the --help option
*/
public function testHelpOption(): void
{
$cmd = new Command($this->getPathToCommonmark());
$cmd->addArg('--help');
$cmd->execute();
$this->assertEquals(0, $cmd->getExitCode());
$this->assertStringContainsString('Usage:', $cmd->getOutput());
}
/**
* Tests the behavior of using unknown options
*/
public function testUnknownOption(): void
{
$cmd = new Command($this->getPathToCommonmark());
$cmd->addArg('--foo');
$cmd->execute();
$this->assertEquals(1, $cmd->getExitCode());
$this->assertStringContainsString('Unknown option', $cmd->getError());
}
/**
* Tests converting a file by filename
*/
public function testFileArgument(): void
{
$cmd = new Command($this->getPathToCommonmark());
$cmd->addArg($this->getPathToData('header.html'));
$cmd->execute();
$this->assertEquals(0, $cmd->getExitCode());
$expectedContents = \trim(\file_get_contents($this->getPathToData('header.md')));
$this->assertEquals($expectedContents, \trim($cmd->getOutput()));
}
/**
* Tests converting HTML from STDIN
*/
public function testStdin(): void
{
$cmd = new Command(\sprintf('cat %s | %s ', $this->getPathToData('header.html'), $this->getPathToCommonmark()));
$cmd->execute();
$this->assertEquals(0, $cmd->getExitCode());
$expectedContents = \trim(\file_get_contents($this->getPathToData('header.md')));
$this->assertEquals($expectedContents, \trim($cmd->getOutput()));
}
/**
* Returns the full path the html-to-markdown "binary"
*/
protected function getPathToCommonmark(): string
{
return \realpath(__DIR__ . '/../bin/html-to-markdown');
}
/**
* Returns the full path to the test data file
*/
protected function getPathToData(string $file): string
{
return \realpath(__DIR__ . '/data/' . $file);
}
}
|