Skip to content

Commit 9ed179c

Browse files
authored
Merge pull request #38 from SMillerDev/dev/run
Add material theme and a lot of unittests
2 parents dd29e16 + fdca4d4 commit 9ed179c

28 files changed

+3095
-68
lines changed

index.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,42 @@
1616
use PHPDraft\Parse\JsonToHTML;
1717

1818
define('VERSION', '0');
19-
$values = UI::main($argv);
20-
21-
$apib = new ApibFileParser($values['file']);
22-
$apib = $apib->parse();
23-
24-
$json = new DrafterAPI($apib);
25-
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1))
19+
try
2620
{
27-
try
28-
{
29-
$json = new Drafter($apib);
30-
}
31-
catch (RuntimeException $exception)
21+
$values = UI::main($argv);
22+
$apib = new ApibFileParser($values['file']);
23+
$apib = $apib->parse();
24+
25+
$json = new DrafterAPI($apib);
26+
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1))
3227
{
33-
file_put_contents('php://stderr', $exception->getMessage() . "\n");
34-
$options = [
35-
'y' => 'Yes',
36-
'n' => 'No',
37-
];
38-
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
39-
if (!$answer)
28+
try
4029
{
41-
file_put_contents('php://stderr', 'Could not find a suitable drafter version');
42-
exit(1);
30+
$json = new Drafter($apib);
31+
}
32+
catch (\PHPDraft\Parse\ResourceException $exception)
33+
{
34+
file_put_contents('php://stderr', $exception->getMessage() . "\n");
35+
$options = [
36+
'y' => 'Yes',
37+
'n' => 'No',
38+
];
39+
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
40+
if (!$answer)
41+
{
42+
throw new \RuntimeException('Could not find a suitable drafter version', 1);
43+
}
4344
}
4445
}
45-
}
46-
47-
$html = new JsonToHTML($json->parseToJson());
48-
$html->sorting = $values['sorting'];
49-
$generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
5046

47+
$html = new JsonToHTML($json->parseToJson());
48+
$html->sorting = $values['sorting'];
49+
$generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
50+
} catch (RuntimeException $exception)
51+
{
52+
file_put_contents('php://stderr', $exception->getMessage().PHP_EOL);
53+
exit($exception->getCode());
54+
}
5155

5256
function phpdraft_var_dump(...$vars)
5357
{

src/PHPDraft/In/ApibFileParser.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ private function get_apib($filename)
9191
private function file_check($filename)
9292
{
9393
if (!file_exists($filename)) {
94-
file_put_contents('php://stderr', "API File not found: $filename\n");
95-
exit(1);
94+
throw new \RuntimeException("API File not found: $filename", 1);
9695
}
9796
}
9897

src/PHPDraft/In/Tests/ApibFileParserTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public function testFilenameSetup()
5151
$this->assertSame(__DIR__ . '/ApibFileParserTest.php', $property->getValue($this->class));
5252
}
5353

54+
/**
55+
* Test if exception when the file doesn't exist
56+
* @expectedException \RuntimeException
57+
* @expectedExceptionCode 1
58+
* @expectedExceptionMessageRegExp "API File not found: [a-zA-Z0-9\/]*\/drafter\/non_existing_including_apib"
59+
*
60+
* @return void
61+
*/
62+
public function testFilenameSetupWrong()
63+
{
64+
$property = $this->reflection->getProperty('filename');
65+
$property->setAccessible(true);
66+
$property->setValue($this->class, TEST_STATICS . '/drafter/non_existing_including_apib');
67+
$this->class->parse();
68+
}
69+
5470
/**
5571
* Test if setup is successful
5672
* @return void

src/PHPDraft/Model/Elements/ArrayStructureElement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function parse($object, &$dependencies)
5656
*/
5757
function __toString()
5858
{
59-
$return = '<ul class="list-group">';
59+
$return = '<ul class="list-group mdl-list">';
6060

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

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

7373
$return .= '</ul>';

src/PHPDraft/Model/Elements/BasicStructureElement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ abstract protected function new_instance();
9090
*/
9191
protected function parse_common($object, &$dependencies)
9292
{
93-
$this->key = $object->content->key->content;
94-
$this->type = $object->content->value->element;
93+
$this->key = (isset($object->content->key->content)) ? $object->content->key->content : NULL;
94+
$this->type = (isset($object->content->value->element)) ? $object->content->value->element : NULL;
9595
$this->description = isset($object->meta->description) ? htmlentities($object->meta->description) : NULL;
9696
$this->status =
9797
isset($object->attributes->typeAttributes) ? join(', ', $object->attributes->typeAttributes) : NULL;

src/PHPDraft/Model/Elements/EnumStructureElement.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ class EnumStructureElement extends BasicStructureElement
2020
*/
2121
public function parse($object, &$dependencies)
2222
{
23-
$this->element = (isset($object->element)) ? $object->element : 'array';
23+
$this->element = (isset($object->element)) ? $object->element : 'enum';
2424

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

27+
$this->key = is_null($this->key) ? $object->content : $this->key;
28+
$this->type = is_null($this->type) ? $object->element : $this->type;
29+
2730
if(!isset($object->content->value->content))
2831
{
29-
$this->value = [];
32+
$this->value = $this->key;
3033
return $this;
3134
}
3235

@@ -52,7 +55,14 @@ public function parse($object, &$dependencies)
5255
*/
5356
function __toString()
5457
{
55-
$return = '<ul class="list-group">';
58+
$return = '<ul class="list-group mdl-list">';
59+
60+
if (is_string($this->value))
61+
{
62+
$type = (in_array($this->element, self::DEFAULTS)) ? $this->element : '<a href="#object-' . str_replace(' ', '-',
63+
strtolower($this->element)) . '">' . $this->element . '</a>';
64+
return '<tr><td>' . $this->key . '</td><td><code>' . $type . '</code></td><td>' . $this->description . '</td></tr>';
65+
}
5666

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

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

6979
$return .= '</ul>';

src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,17 @@ public function testToStringWithArray()
187187
{
188188
$this->class->value = ['string', 'int'];
189189
$return = $this->class->__toString();
190-
$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);
190+
$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);
191+
}
192+
193+
/**
194+
* Test setup of new instances
195+
*/
196+
public function testToStringWithString()
197+
{
198+
$this->class->value = 'hello';
199+
$return = $this->class->__toString();
200+
$this->assertSame('<span class="example-value pull-right">[ ]</span>', $return);
191201
}
192202

193203
/**
@@ -197,6 +207,6 @@ public function testToStringWithComplexArray()
197207
{
198208
$this->class->value = ['Bike', 'car'];
199209
$return = $this->class->__toString();
200-
$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);
210+
$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);
201211
}
202212
}

src/PHPDraft/Model/Elements/Tests/EnumStructureElementTest.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,31 @@ public function testToStringWithArray()
6464
{
6565
$this->class->value = ['hello'=>'string', 'test'=>'int'];
6666
$return = $this->class->__toString();
67-
$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);
67+
$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);
68+
}
69+
70+
/**
71+
* Test setup of new instances
72+
*/
73+
public function testToStringWithString()
74+
{
75+
$this->class->value = 'hello';
76+
$this->class->key = 'key';
77+
$this->class->element = 'string';
78+
$return = $this->class->__toString();
79+
$this->assertSame('<tr><td>key</td><td><code>string</code></td><td></td></tr>', $return);
80+
}
81+
82+
/**
83+
* Test setup of new instances
84+
*/
85+
public function testToStringWithStringComplex()
86+
{
87+
$this->class->value = 'hello';
88+
$this->class->key = 'key';
89+
$this->class->element = 'Car';
90+
$return = $this->class->__toString();
91+
$this->assertSame('<tr><td>key</td><td><code><a href="#object-car">Car</a></code></td><td></td></tr>', $return);
6892
}
6993

7094
/**
@@ -74,7 +98,7 @@ public function testToStringWithComplexArray()
7498
{
7599
$this->class->value = ['hello'=>'bike', 'test'=>'Car'];
76100
$return = $this->class->__toString();
77-
$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);
101+
$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);
78102
}
79103

80104
/**
@@ -122,7 +146,7 @@ public function parseObjectProvider()
122146

123147
$base3 = new EnumStructureElement();
124148
$base3->key = 'car_id_list';
125-
$base3->value = [];
149+
$base3->value = 'car_id_list';
126150
$base3->status = 'optional';
127151
$base3->element = 'member';
128152
$base3->type = 'array';

src/PHPDraft/Model/HTTPRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ public function get_curl_command($base_url, $additional = [])
157157
return htmlspecialchars('curl ' . join(' ', $options) . ' ' . escapeshellarg($this->parent->build_url($base_url, true)));
158158
}
159159

160-
161160
/**
162161
* Check if item is the same as other item
163162
*

src/PHPDraft/Model/HTTPResponse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ public function __construct($parent)
6969
*/
7070
public function parse($object)
7171
{
72-
if (isset($object->attributes)) {
72+
if (isset($object->attributes->statusCode)) {
7373
$this->statuscode = intval($object->attributes->statusCode);
74+
}
75+
if (isset($object->attributes->headers)) {
7476
$this->parse_headers($object->attributes->headers);
7577
}
7678

src/PHPDraft/Model/Tests/CategoryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ public function testParseIsCalledObject()
104104
$this->assertNotEmpty($s_property->getValue($this->class));
105105
}
106106

107+
/**
108+
* Test basic parse functions where 'element=dataStructure'
109+
*/
110+
public function testParseIsCalledObjectMetaID()
111+
{
112+
$property = $this->reflection->getProperty('parent');
113+
$property->setAccessible(TRUE);
114+
$property->setValue($this->class, $this->parent);
115+
116+
$json = '{"content":[{"element":"dataStructure", "content":[{"meta":{"id":4}, "key":{"content":"none"}, "value":{"element":"none"}}]}]}';
117+
118+
$this->class->parse(json_decode($json));
119+
120+
$this->assertSame($this->parent, $property->getValue($this->class));
121+
122+
$s_property = $this->reflection->getProperty('structures');
123+
$s_property->setAccessible(TRUE);
124+
$this->assertNotEmpty($s_property->getValue($this->class));
125+
}
126+
107127
/**
108128
* Test basic parse functions where 'element=henk'
109129
*/

src/PHPDraft/Model/Tests/ResourceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function testParseIsCalledIsCopy()
106106
$property->setAccessible(TRUE);
107107
$property->setValue($this->class, $this->parent);
108108

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

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

src/PHPDraft/Model/Tests/TransitionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,26 @@ public function testParseIsCalledIsArrayContentResponse()
209209
$this->assertSame('something', $href_property->getValue($this->class));
210210
}
211211

212+
/**
213+
* Test basic parse functions
214+
*/
215+
public function testParseIsCalledIsArrayContentDefault()
216+
{
217+
$property = $this->reflection->getProperty('parent');
218+
$property->setAccessible(TRUE);
219+
$property->setValue($this->class, $this->parent);
220+
221+
$obj = '{"attributes":{"href":"something", "data":"hello"}, "content":[{"element":"123", "content":[{"element":"Cow", "content":[], "attributes":{"statusCode":"1000", "headers":{"content":[]}}}]}]}';
222+
223+
$this->class->parse(json_decode($obj));
224+
225+
$this->assertSame($this->parent, $property->getValue($this->class));
226+
227+
$href_property = $this->reflection->getProperty('href');
228+
$href_property->setAccessible(TRUE);
229+
$this->assertSame('something', $href_property->getValue($this->class));
230+
}
231+
212232
/**
213233
* Test basic parse functions
214234
*/

src/PHPDraft/Model/Transition.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ function parse($object)
8787

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

90+
9091
if (isset($object->attributes->hrefVariables)) {
9192

9293
$deps = [];

0 commit comments

Comments
 (0)