d244b91c94d03ea83b2fe74ad4f4079db2821787

Author: Mark Story

Date: 2009-07-14 22:39:08 -0400

Adding package links to views. Adding tests for packageLink()

diff --git a/tests/cases/helpers/api_doc.test.php b/tests/cases/helpers/api_doc.test.php index c1dfd31..323a06f 100644 --- a/tests/cases/helpers/api_doc.test.php +++ b/tests/cases/helpers/api_doc.test.php @@ -36,6 +36,9 @@ class ApiDocHelperTestCase extends CakeTestCase { $view->set('basePath', $this->_pluginPath); $this->ApiDoc = new ApiDocHelper(); $this->ApiDoc->Html = new HtmlHelper(); + + Router::reload(); + Router::parse('/'); } /** * test inBasePath @@ -75,7 +78,7 @@ class ApiDocHelperTestCase extends CakeTestCase { $result = $this->ApiDoc->fileLink($testFile); $expected = array( - 'a' => array('href' => '/api_generator/view_file/views/helpers/api_doc.php'), + 'a' => array('href' => '/api_generator/api_files/view_file/views/helpers/api_doc.php'), 'views/helpers/api_doc.php', '/a' ); @@ -90,6 +93,37 @@ class ApiDocHelperTestCase extends CakeTestCase { $this->assertTags($result, $expected); } /** + * test generation of package links. + * + * @return void + **/ + function testPackageLink() { + $result = $this->ApiDoc->packageLink('foo'); + $expected = array( + 'a' => array('href' => '/api_generator/api_packages/view/foo'), + 'foo', + '/a' + ); + $this->assertTags($result, $expected); + + $result = $this->ApiDoc->packageLink('some.package.deep'); + $expected = array( + 'a' => array('href' => '/api_generator/api_packages/view/deep'), + 'some.package.deep', + '/a' + ); + $this->assertTags($result, $expected); + + $result = $this->ApiDoc->packageLink(' some.package.deep'); + $expected = array( + 'a' => array('href' => '/api_generator/api_packages/view/deep'), + ' some.package.deep', + '/a' + ); + $this->assertTags($result, $expected); + + } +/** * endTest * * @return void diff --git a/views/elements/tag_block.ctp b/views/elements/tag_block.ctp index 61f1234..a743e46 100644 --- a/views/elements/tag_block.ctp +++ b/views/elements/tag_block.ctp @@ -8,8 +8,12 @@ <dl> <?php foreach ($tags as $name => $value): ?> <dt><?php echo $name; ?></dt> - <?php if (strtolower($name) == 'link'): + <?php + $lower = strtolower($name); + if ($lower == 'link'): echo '<dd>' . $text->autoLink(h($value)) . '</dd>'; + elseif ($lower == 'package' || $lower == 'subpackage'): + echo '<dd>' . $apiDoc->packageLink(trim($value)) . '</dd>'; elseif (is_array($value)): foreach ($value as $line): echo '<dd>' . h($line) . '</dd>'; diff --git a/views/helpers/api_doc.php b/views/helpers/api_doc.php index a7b7ff6..5ab6a83 100644 --- a/views/helpers/api_doc.php +++ b/views/helpers/api_doc.php @@ -212,7 +212,7 @@ class ApiDocHelper extends AppHelper { * @param string $className Name of class to sluggify. * @return string **/ - public function slugClassName($className) { + public function slug($className) { return str_replace('_', '-', Inflector::underscore($className)); } @@ -270,11 +270,24 @@ class ApiDocHelper extends AppHelper { * @param array $attributes Additional attributes for an html link if generated. * @return string Html link **/ - public function packageLink($apiPackage, $url = array(), $attributes = array()) { + public function packageLink($package, $url = array(), $attributes = array()) { $url = array_merge($this->_defaultUrl['package'], $url); - $slug = $this->slugClassName($apiPackage); + $slug = $this->slug($package); + if (strpos($slug, '.') !== false) { + $slug = $this->_parsePackageString($slug); + } $url[] = $slug; - return $this->Html->link($apiPackage, $url, $attributes); + return $this->Html->link($package, $url, $attributes); + } +/** + * parse a dot split package string and return the last package element. + * + * @param string $package A package string with .'s in it. + * @return string + **/ + protected function _parsePackageString($package) { + $bits = explode('.', $package); + return $this->slug(end($bits)); } } \ No newline at end of file