Skip to content

Commit

Permalink
Merge pull request #38 from SMillerDev/dev/run
Browse files Browse the repository at this point in the history
Add material theme and a lot of unittests
  • Loading branch information
SMillerDev authored Jul 20, 2017
2 parents dd29e16 + fdca4d4 commit 9ed179c
Show file tree
Hide file tree
Showing 28 changed files with 3,095 additions and 68 deletions.
56 changes: 30 additions & 26 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,42 @@
use PHPDraft\Parse\JsonToHTML;

define('VERSION', '0');
$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
{
try
{
$json = new Drafter($apib);
}
catch (RuntimeException $exception)
$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))
{
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)
try
{
file_put_contents('php://stderr', 'Could not find a suitable drafter version');
exit(1);
$json = new Drafter($apib);
}
catch (\PHPDraft\Parse\ResourceException $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)
{
throw new \RuntimeException('Could not find a suitable drafter version', 1);
}
}
}
}

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

$html = new JsonToHTML($json->parseToJson());
$html->sorting = $values['sorting'];
$generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
} catch (RuntimeException $exception)
{
file_put_contents('php://stderr', $exception->getMessage().PHP_EOL);
exit($exception->getCode());
}

function phpdraft_var_dump(...$vars)
{
Expand Down
3 changes: 1 addition & 2 deletions src/PHPDraft/In/ApibFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ private function get_apib($filename)
private function file_check($filename)
{
if (!file_exists($filename)) {
file_put_contents('php://stderr', "API File not found: $filename\n");
exit(1);
throw new \RuntimeException("API File not found: $filename", 1);
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/PHPDraft/In/Tests/ApibFileParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public function testFilenameSetup()
$this->assertSame(__DIR__ . '/ApibFileParserTest.php', $property->getValue($this->class));
}

/**
* Test if exception when the file doesn't exist
* @expectedException \RuntimeException
* @expectedExceptionCode 1
* @expectedExceptionMessageRegExp "API File not found: [a-zA-Z0-9\/]*\/drafter\/non_existing_including_apib"
*
* @return void
*/
public function testFilenameSetupWrong()
{
$property = $this->reflection->getProperty('filename');
$property->setAccessible(true);
$property->setValue($this->class, TEST_STATICS . '/drafter/non_existing_including_apib');
$this->class->parse();
}

/**
* Test if setup is successful
* @return void
Expand Down
4 changes: 2 additions & 2 deletions src/PHPDraft/Model/Elements/ArrayStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function parse($object, &$dependencies)
*/
function __toString()
{
$return = '<ul class="list-group">';
$return = '<ul class="list-group mdl-list">';

if (!is_array($this->value))
{
Expand All @@ -67,7 +67,7 @@ function __toString()
$type = (in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(' ', '-',
strtolower($item)) . '">' . $item . '</a>';

$return .= '<li class="list-group-item">' . $type . '</li>';
$return .= '<li class="list-group-item mdl-list__item">' . $type . '</li>';
}

$return .= '</ul>';
Expand Down
4 changes: 2 additions & 2 deletions src/PHPDraft/Model/Elements/BasicStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ abstract protected function new_instance();
*/
protected function parse_common($object, &$dependencies)
{
$this->key = $object->content->key->content;
$this->type = $object->content->value->element;
$this->key = (isset($object->content->key->content)) ? $object->content->key->content : NULL;
$this->type = (isset($object->content->value->element)) ? $object->content->value->element : NULL;
$this->description = isset($object->meta->description) ? htmlentities($object->meta->description) : NULL;
$this->status =
isset($object->attributes->typeAttributes) ? join(', ', $object->attributes->typeAttributes) : NULL;
Expand Down
18 changes: 14 additions & 4 deletions src/PHPDraft/Model/Elements/EnumStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ class EnumStructureElement extends BasicStructureElement
*/
public function parse($object, &$dependencies)
{
$this->element = (isset($object->element)) ? $object->element : 'array';
$this->element = (isset($object->element)) ? $object->element : 'enum';

$this->parse_common($object, $dependencies);

$this->key = is_null($this->key) ? $object->content : $this->key;
$this->type = is_null($this->type) ? $object->element : $this->type;

if(!isset($object->content->value->content))
{
$this->value = [];
$this->value = $this->key;
return $this;
}

Expand All @@ -52,7 +55,14 @@ public function parse($object, &$dependencies)
*/
function __toString()
{
$return = '<ul class="list-group">';
$return = '<ul class="list-group mdl-list">';

if (is_string($this->value))
{
$type = (in_array($this->element, self::DEFAULTS)) ? $this->element : '<a href="#object-' . str_replace(' ', '-',
strtolower($this->element)) . '">' . $this->element . '</a>';
return '<tr><td>' . $this->key . '</td><td><code>' . $type . '</code></td><td>' . $this->description . '</td></tr>';
}

if (!is_array($this->value))
{
Expand All @@ -63,7 +73,7 @@ function __toString()
$type = (in_array($item, self::DEFAULTS)) ? $key : '<a href="#object-' . str_replace(' ', '-',
strtolower($item)) . '">' . $key . '</a>';

$return .= '<li class="list-group-item">' . $type . '</li>';
$return .= '<li class="list-group-item mdl-list__item">' . $type . '</li>';
}

$return .= '</ul>';
Expand Down
14 changes: 12 additions & 2 deletions src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,17 @@ public function testToStringWithArray()
{
$this->class->value = ['string', 'int'];
$return = $this->class->__toString();
$this->assertSame('<ul class="list-group"><li class="list-group-item">string</li><li class="list-group-item"><a href="#object-int">int</a></li></ul>', $return);
$this->assertSame('<ul class="list-group mdl-list"><li class="list-group-item mdl-list__item">string</li><li class="list-group-item mdl-list__item"><a href="#object-int">int</a></li></ul>', $return);
}

/**
* Test setup of new instances
*/
public function testToStringWithString()
{
$this->class->value = 'hello';
$return = $this->class->__toString();
$this->assertSame('<span class="example-value pull-right">[ ]</span>', $return);
}

/**
Expand All @@ -197,6 +207,6 @@ public function testToStringWithComplexArray()
{
$this->class->value = ['Bike', 'car'];
$return = $this->class->__toString();
$this->assertSame('<ul class="list-group"><li class="list-group-item"><a href="#object-bike">Bike</a></li><li class="list-group-item"><a href="#object-car">car</a></li></ul>', $return);
$this->assertSame('<ul class="list-group mdl-list"><li class="list-group-item mdl-list__item"><a href="#object-bike">Bike</a></li><li class="list-group-item mdl-list__item"><a href="#object-car">car</a></li></ul>', $return);
}
}
30 changes: 27 additions & 3 deletions src/PHPDraft/Model/Elements/Tests/EnumStructureElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,31 @@ public function testToStringWithArray()
{
$this->class->value = ['hello'=>'string', 'test'=>'int'];
$return = $this->class->__toString();
$this->assertSame('<ul class="list-group"><li class="list-group-item">hello</li><li class="list-group-item"><a href="#object-int">test</a></li></ul>', $return);
$this->assertSame('<ul class="list-group mdl-list"><li class="list-group-item mdl-list__item">hello</li><li class="list-group-item mdl-list__item"><a href="#object-int">test</a></li></ul>', $return);
}

/**
* Test setup of new instances
*/
public function testToStringWithString()
{
$this->class->value = 'hello';
$this->class->key = 'key';
$this->class->element = 'string';
$return = $this->class->__toString();
$this->assertSame('<tr><td>key</td><td><code>string</code></td><td></td></tr>', $return);
}

/**
* Test setup of new instances
*/
public function testToStringWithStringComplex()
{
$this->class->value = 'hello';
$this->class->key = 'key';
$this->class->element = 'Car';
$return = $this->class->__toString();
$this->assertSame('<tr><td>key</td><td><code><a href="#object-car">Car</a></code></td><td></td></tr>', $return);
}

/**
Expand All @@ -74,7 +98,7 @@ public function testToStringWithComplexArray()
{
$this->class->value = ['hello'=>'bike', 'test'=>'Car'];
$return = $this->class->__toString();
$this->assertSame('<ul class="list-group"><li class="list-group-item"><a href="#object-bike">hello</a></li><li class="list-group-item"><a href="#object-car">test</a></li></ul>', $return);
$this->assertSame('<ul class="list-group mdl-list"><li class="list-group-item mdl-list__item"><a href="#object-bike">hello</a></li><li class="list-group-item mdl-list__item"><a href="#object-car">test</a></li></ul>', $return);
}

/**
Expand Down Expand Up @@ -122,7 +146,7 @@ public function parseObjectProvider()

$base3 = new EnumStructureElement();
$base3->key = 'car_id_list';
$base3->value = [];
$base3->value = 'car_id_list';
$base3->status = 'optional';
$base3->element = 'member';
$base3->type = 'array';
Expand Down
1 change: 0 additions & 1 deletion src/PHPDraft/Model/HTTPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public function get_curl_command($base_url, $additional = [])
return htmlspecialchars('curl ' . join(' ', $options) . ' ' . escapeshellarg($this->parent->build_url($base_url, true)));
}


/**
* Check if item is the same as other item
*
Expand Down
4 changes: 3 additions & 1 deletion src/PHPDraft/Model/HTTPResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ public function __construct($parent)
*/
public function parse($object)
{
if (isset($object->attributes)) {
if (isset($object->attributes->statusCode)) {
$this->statuscode = intval($object->attributes->statusCode);
}
if (isset($object->attributes->headers)) {
$this->parse_headers($object->attributes->headers);
}

Expand Down
20 changes: 20 additions & 0 deletions src/PHPDraft/Model/Tests/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ public function testParseIsCalledObject()
$this->assertNotEmpty($s_property->getValue($this->class));
}

/**
* Test basic parse functions where 'element=dataStructure'
*/
public function testParseIsCalledObjectMetaID()
{
$property = $this->reflection->getProperty('parent');
$property->setAccessible(TRUE);
$property->setValue($this->class, $this->parent);

$json = '{"content":[{"element":"dataStructure", "content":[{"meta":{"id":4}, "key":{"content":"none"}, "value":{"element":"none"}}]}]}';

$this->class->parse(json_decode($json));

$this->assertSame($this->parent, $property->getValue($this->class));

$s_property = $this->reflection->getProperty('structures');
$s_property->setAccessible(TRUE);
$this->assertNotEmpty($s_property->getValue($this->class));
}

/**
* Test basic parse functions where 'element=henk'
*/
Expand Down
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/Tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function testParseIsCalledIsCopy()
$property->setAccessible(TRUE);
$property->setValue($this->class, $this->parent);

$obj = '{"content":[{"element":"copy", "content":""},{"element":"hello", "content":""}]}';
$obj = '{"content":[{"element":"copy", "content":""},{"element":"hello", "content":""}, {"element":"hello", "content":""}]}';

$this->class->parse(json_decode($obj));

Expand Down
20 changes: 20 additions & 0 deletions src/PHPDraft/Model/Tests/TransitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,26 @@ public function testParseIsCalledIsArrayContentResponse()
$this->assertSame('something', $href_property->getValue($this->class));
}

/**
* Test basic parse functions
*/
public function testParseIsCalledIsArrayContentDefault()
{
$property = $this->reflection->getProperty('parent');
$property->setAccessible(TRUE);
$property->setValue($this->class, $this->parent);

$obj = '{"attributes":{"href":"something", "data":"hello"}, "content":[{"element":"123", "content":[{"element":"Cow", "content":[], "attributes":{"statusCode":"1000", "headers":{"content":[]}}}]}]}';

$this->class->parse(json_decode($obj));

$this->assertSame($this->parent, $property->getValue($this->class));

$href_property = $this->reflection->getProperty('href');
$href_property->setAccessible(TRUE);
$this->assertSame('something', $href_property->getValue($this->class));
}

/**
* Test basic parse functions
*/
Expand Down
1 change: 1 addition & 0 deletions src/PHPDraft/Model/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function parse($object)

$this->href = (isset($object->attributes->href)) ? $object->attributes->href : $this->parent->href;


if (isset($object->attributes->hrefVariables)) {

$deps = [];
Expand Down
Loading

0 comments on commit 9ed179c

Please sign in to comment.