Skip to content

Commit

Permalink
Merge pull request #37 from SMillerDev/fix/unittests
Browse files Browse the repository at this point in the history
General: Improve test coverage
  • Loading branch information
SMillerDev authored Jul 14, 2017
2 parents ea1bdc9 + 9dfc714 commit dd29e16
Show file tree
Hide file tree
Showing 51 changed files with 6,438 additions and 352 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/build/coverage
/build/logs
/build/phar
/build/tmp
/build/*.phar
/tests/statics/index.*
src/Michelf/*
Expand Down
29 changes: 18 additions & 11 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@
$values = UI::main($argv);

$apib = new ApibFileParser($values['file']);
$apib = $apib->parse();

$json = new DrafterAPI($apib);
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1)) {
try {
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1))
{
try
{
$json = new Drafter($apib);
} catch (RuntimeException $exception) {
file_put_contents('php://stderr', $exception->getMessage()."\n");
}
catch (RuntimeException $exception)
{
file_put_contents('php://stderr', $exception->getMessage() . "\n");
$options = [
'y' => 'Yes',
'n' => 'No',
];
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
if (!$answer) {
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
if (!$answer)
{
file_put_contents('php://stderr', 'Could not find a suitable drafter version');
exit(1);
}
Expand All @@ -39,19 +46,19 @@

$html = new JsonToHTML($json->parseToJson());
$html->sorting = $values['sorting'];
$html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
$generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']);


function phpdraft_var_dump(...$vars)
{
if (defined('__PHPDRAFT_PHAR__')) {
if (defined('__PHPDRAFT_PHAR__'))
{
return;
}
echo '<pre>';
foreach ($vars as $var) {
foreach ($vars as $var)
{
var_dump($var);
}
echo '</pre>';
}

?>
28 changes: 24 additions & 4 deletions src/PHPDraft/Core/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ private function __runkit_mock_function($name, $return)
runkit_function_copy($name, $name . self::FUNCTION_ID);
}

runkit_function_redefine($name, '', 'return ' . (is_string($return) ? ('"' . $return . '"') : $return) . ';');
runkit_function_redefine($name, function () use ($return) {
return $return;
});
}

/**
Expand All @@ -94,9 +96,7 @@ private function __uopz_mock_function($name, $return)
{
if (PHP_MAJOR_VERSION < 7) {
\uopz_backup($name);
\uopz_function($name, function () {
global $return;

\uopz_function($name, function () use ($return){
return $return;
});

Expand Down Expand Up @@ -158,4 +158,24 @@ private function __uopz_unmock_function($name)

uopz_unset_return($name);
}

/**
* Redefine a constant
*
* @param string $name Constant to redefine
* @param mixed $value Value to define to
*/
public function redefine($name, $value)
{
if (extension_loaded('runkit') === true) {
\runkit_constant_redefine($name, $value);
return;
}

if (extension_loaded('uopz') === true) {
\uopz_redefine($name, $value);
return;
}

}
}
21 changes: 17 additions & 4 deletions src/PHPDraft/In/ApibFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,31 @@ class ApibFileParser
*/
protected $location;

/**
* Filename to parse
*
* @var
*/
private $filename;

/**
* FileParser constructor.
*
* @param string $filename File to parse
*/
public function __construct($filename = 'index.apib')
{
$this->location = pathinfo($filename, PATHINFO_DIRNAME) . '/';
$this->filename = $filename;
$this->location = pathinfo($this->filename, PATHINFO_DIRNAME) . '/';

set_include_path(get_include_path() . ':' . $this->location);
}

public function parse()
{
$this->full_apib = $this->get_apib($this->filename);

$this->full_apib = $this->get_apib($filename);
return $this;
}

/**
Expand All @@ -49,7 +62,7 @@ public function __construct($filename = 'index.apib')
*
* @return string The full API blueprint file
*/
function get_apib($filename)
private function get_apib($filename)
{
$this->file_check($filename);
$file = file_get_contents($filename);
Expand Down Expand Up @@ -90,7 +103,7 @@ private function file_check($filename)
*
* @return string The schema as a string
*/
function get_schema($url)
private function get_schema($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
Expand Down
44 changes: 41 additions & 3 deletions src/PHPDraft/In/Tests/ApibFileParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* Class ApibFileParserTest
* @covers PHPDraft\In\ApibFileParser
* @covers \PHPDraft\In\ApibFileParser
*/
class ApibFileParserTest extends BaseTest
{
Expand All @@ -33,11 +33,49 @@ public function setUp()
* Test if setup is successful
* @return void
*/
public function testSetup()
public function testLocationSetup()
{
$property = $this->reflection->getProperty('location');
$property->setAccessible(TRUE);
$property->setAccessible(true);
$this->assertSame(__DIR__ . '/', $property->getValue($this->class));
}

/**
* Test if setup is successful
* @return void
*/
public function testFilenameSetup()
{
$property = $this->reflection->getProperty('filename');
$property->setAccessible(true);
$this->assertSame(__DIR__ . '/ApibFileParserTest.php', $property->getValue($this->class));
}

/**
* Test if setup is successful
* @return void
*/
public function testParseBasic()
{
$property = $this->reflection->getProperty('filename');
$property->setAccessible(true);
$property->setValue($this->class, TEST_STATICS . '/drafter/including_apib');
$loc_property = $this->reflection->getProperty('location');
$loc_property->setAccessible(true);
$loc_property->setValue($this->class, TEST_STATICS . '/drafter/');

$this->mock_function('curl_exec', 'hello');
$this->class->parse();
$this->unmock_function('curl_exec');

$full_property = $this->reflection->getProperty('full_apib');
$full_property->setAccessible(true);

$text = "FORMAT: 1A\nHOST: https://owner-api.teslamotors.com\nEXTRA_HOSTS: https://test.owner-api.teslamotors.com\nSOMETHING: INFO\n\n";
$text .="# Tesla Model S JSON API\nThis is unofficial documentation of the Tesla Model S JSON API used by the iOS and Android apps. It features functionality to monitor and control the Model S remotely.\n\nTEST\nhello";

$this->assertSame($text, $full_property->getValue($this->class));
$this->assertSame($text, $this->class->__toString());
}

}
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function parse($object)
$struct->deps = $deps;
$struct->parse($item, $deps);

if (isset($item->content[0]->meta->id))
if (is_array($item->content) && isset($item->content[0]->meta->id))
{
$this->structures[$item->content[0]->meta->id] = $struct;
} else {
Expand Down
17 changes: 12 additions & 5 deletions src/PHPDraft/Model/Elements/ArrayStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@

namespace PHPDraft\Model\Elements;

use PHPDraft\Model\StructureElement;

/**
* Class ArrayStructureElement
*/
class ArrayStructureElement extends ObjectStructureElement implements StructureElement
class ArrayStructureElement extends BasicStructureElement
{

/**
Expand All @@ -22,7 +20,7 @@ class ArrayStructureElement extends ObjectStructureElement implements StructureE
* @param \stdClass $object APIb Item to parse
* @param array $dependencies List of dependencies build
*
* @return $this
* @return self Self reference
*/
public function parse($object, &$dependencies)
{
Expand Down Expand Up @@ -66,7 +64,7 @@ function __toString()
}

foreach ($this->value as $item) {
$type = (in_array($this->value, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(' ', '-',
$type = (in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(' ', '-',
strtolower($item)) . '">' . $item . '</a>';

$return .= '<li class="list-group-item">' . $type . '</li>';
Expand All @@ -77,4 +75,13 @@ function __toString()
return $return;
}

/**
* Get a new instance of a class
*
* @return self
*/
protected function new_instance()
{
return new ArrayStructureElement();
}
}
Loading

0 comments on commit dd29e16

Please sign in to comment.