Skip to content

Commit ab24be3

Browse files
committed
Restore unit tests
1 parent 305dadf commit ab24be3

File tree

6 files changed

+103
-24
lines changed

6 files changed

+103
-24
lines changed

src/PHPFUI/InstaDoc/FileManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function __construct(string $composerJsonPath = '')
2828
$this->composerJsonPath = str_replace('\\', '/', $composerJsonPath);
2929
}
3030

31+
public function getComposerPath() : string
32+
{
33+
return $this->composerJsonPath;
34+
}
35+
3136
/**
3237
* You can add a Namespace directly. Specify the namespace (no
3338
* leading \) and the directory containing the class files.

src/PHPFUI/InstaDoc/Section/Doc.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Doc extends \PHPFUI\InstaDoc\Section
77

88
private $factory;
99
private $reflection;
10-
private $fullClassPath;
10+
private $class;
1111

1212
public function generate(\PHPFUI\Page $page, string $fullClassPath) : \PHPFUI\Container
1313
{
@@ -17,15 +17,19 @@ public function generate(\PHPFUI\Page $page, string $fullClassPath) : \PHPFUI\Co
1717
// $page->addStyleSheet("highlighter/styles/{$parameters['CSS']}.css");
1818
$page->addStyleSheet("highlighter/styles/qtcreator_light.css");
1919
$this->factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
20-
$this->fullClassPath = $fullClassPath;
21-
$this->fullClassPath = str_replace(['/', '.php'], ['\\', ''], $this->fullClassPath);
22-
$start = strpos($this->fullClassPath, '.');
23-
if ($start !== false)
20+
$parameters = $this->controller->getParameters();
21+
$this->class = $parameters[\PHPFUI\InstaDoc\Controller::NAMESPACE] . '\\' . $parameters[\PHPFUI\InstaDoc\Controller::CLASS_NAME];
22+
23+
try
2424
{
25-
$this->fullClassPath = substr($this->fullClassPath, $start + 3);
25+
$this->reflection = new \ReflectionClass($this->class);
2626
}
27+
catch (\throwable $e)
28+
{
29+
$container->add(new \PHPFUI\SubHeader("{$this->class} is not a class"));
2730

28-
$this->reflection = new \ReflectionClass($this->fullClassPath);
31+
return $container;
32+
}
2933

3034
$table = new \PHPFUI\Table();
3135
$table->addClass('hover');
@@ -52,6 +56,15 @@ public function generate(\PHPFUI\Page $page, string $fullClassPath) : \PHPFUI\Co
5256

5357
$container->add($table);
5458

59+
$comments = $this->reflection->getDocComment();
60+
if ($comments)
61+
{
62+
$docblock = $this->factory->create($comments);
63+
$callout = new \PHPFUI\Callout('secondary');
64+
$callout->add($docblock->getSummary());
65+
$container->add($callout);
66+
}
67+
5568
$tabs = new \PHPFUI\Tabs();
5669
$tabs->addTab('Public', $this->getContent('isPublic'), true);
5770
$tabs->addTab('Protected', $this->getContent('isProtected'));
@@ -77,7 +90,7 @@ private function getContent(string $accessType) : \PHPFUI\Table
7790
$section = 'Constants';
7891
foreach ($constants as $name => $value)
7992
{
80-
$constant = new \ReflectionClassConstant($this->fullClassPath, $name);
93+
$constant = new \ReflectionClassConstant($this->class, $name);
8194

8295
if ($accessType != 'isStatic' && $constant->$accessType())
8396
{
@@ -280,9 +293,9 @@ private function getValueString($value) : string
280293
case 'array':
281294
$text = '[';
282295
$comma = '';
283-
foreach ($value as $part)
296+
foreach ($value as $key => $part)
284297
{
285-
$text .= $comma . $this->getValueString($part);
298+
$text .= $comma . $this->getValueString($key) . ' => ' . $this->getValueString($part);
286299
$comma = ', ';
287300
}
288301
$text .= ']';
@@ -295,7 +308,15 @@ private function getValueString($value) : string
295308
$value = $span;
296309
break;
297310
case 'object':
298-
$value = $this->getClassName(get_class($value));
311+
$class = get_class($value);
312+
if ($class == 'ReflectionNamedType')
313+
{
314+
$value = ($value->allowsNull() ? '?' : '') . $value->getName();
315+
}
316+
else
317+
{
318+
$value = $this->getClassName(get_class($value));
319+
}
299320
break;
300321
case 'resource':
301322
$value = 'resource';

src/PHPFUI/InstaDoc/Section/File.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ public function generate(\PHPFUI\Page $page, string $fullClassPath) : \PHPFUI\Co
99
{
1010
$container = new \PHPFUI\Container();
1111

12+
$fullClassPath = str_replace('\\', '/', $fullClassPath);
13+
if (! file_exists($fullClassPath))
14+
{
15+
$fullClassPath = $this->controller->getFileManager()->getComposerPath() . $fullClassPath;
16+
}
1217
$parameters = $this->controller->getParameters();
1318

1419
$page->addStyleSheet("highlighter/styles/{$parameters['CSS']}.css");
1520
$page->addCSS("code{tab-size:{$parameters['t']};-moz-tab-size:{$parameters['t']}}");
1621
$hl = new \Highlight\Highlighter();
17-
$php = file_get_contents(str_replace('\\', '/', $fullClassPath));
22+
$php = file_get_contents($fullClassPath);
1823

1924
// Highlight some code.
2025
$highlighted = $hl->highlight('php', $php);

src/PHPFUI/InstaDoc/Section/Git.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public function generate(\PHPFUI\Page $page, string $fullClassPath) : \PHPFUI\Co
2727
{
2828
$container = new \PHPFUI\Container();
2929

30+
$container->add(new \PHPFUI\SubHeader('.git support coming soon'));
31+
32+
return $container;
33+
3034
$repo = new \Gitonomy\Git\Repository($_SERVER['DOCUMENT_ROOT'] . '/..');
3135
$result = $repo->run('show-branch');
3236
$branch = substr($result, strpos($result, '[') + 1, strpos($result, ']') - 1);

src/PHPFUI/InstaDoc/TestClass.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace PHPFUI\InstaDoc;
4+
5+
/**
6+
* A test class with no functionality.
7+
*
8+
* It is just to test InstaDoc
9+
*
10+
* @author bruce (1/3/2020)
11+
*/
12+
class TestClass
13+
{
14+
private const CONST_PRIVATE_ARRAY = ['.Git', 0, true, 0.2, ];
15+
protected const CONST_PROTECTED_INT = 42;
16+
public const CONST_PUBLIC_STRING = 'Default';
17+
18+
private $private_array = ['fred', 1, false, 9.9, ['nested', self::CONST_PRIVATE_ARRAY]];
19+
protected $protected_string = 'whatever';
20+
public $public_float = 3.14;
21+
22+
/**
23+
* This function does nothing.
24+
*/
25+
private function private_function_no_return(string $fred = 'Eythel') {}
26+
27+
/**
28+
* This function does nothing. But it has a very long
29+
* meaningless description that just seems to go on and on and
30+
* on but does not really say anything except for being very
31+
* long and completely unreadable, but such is the nature of
32+
* long meaningless comments that really say nothing of any
33+
* importance that just seem to meander and never get to the
34+
* point and be concise and to the point, but that is the point,
35+
* that there is no point. Pointless really.....
36+
*/
37+
protected function protected_function_no_return(string $fred, $unknown = 3.14) {}
38+
39+
public function public_function_returning_and_taking_array(array $array = ['tom', 2 => 'Dick', 'harry' => "reasoner"]) : array
40+
{
41+
return [];
42+
}
43+
44+
}

tests/SectionTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function setUp() : void
3838
}
3939
}
4040

41-
$this->fileManager = new \PHPFUI\InstaDoc\FileManager('./');
41+
$this->fileManager = new \PHPFUI\InstaDoc\FileManager('src/');
4242
$this->fileManager->load();
4343
$this->controller = new \PHPFUI\InstaDoc\Controller($this->fileManager);
4444
}
@@ -48,17 +48,17 @@ public function testHaveSections() : void
4848
$this->assertNotEmpty($this->sections, 'No PHPFUI\InstaDoc\Section classes found');
4949
}
5050

51-
// public function testSectionsGenerateValidHTML() : void
52-
// {
53-
// $page = new \PHPFUI\Page();
54-
//
55-
// foreach ($this->sections as $section)
56-
// {
57-
// $sectionObject = new $section($this->controller);
58-
// $container = $sectionObject->generate($page, 'src/' . $section . '.php');
59-
// $this->assertValidHtml("{$container}");
60-
// }
61-
// }
51+
public function testSectionsGenerateValidHTML() : void
52+
{
53+
$page = new \PHPFUI\Page();
54+
55+
foreach ($this->sections as $section)
56+
{
57+
$sectionObject = new $section($this->controller);
58+
$container = $sectionObject->generate($page, $section . '.php');
59+
$this->assertValidHtml("{$container}");
60+
}
61+
}
6262

6363
}
6464

0 commit comments

Comments
 (0)