From e3812036da455a233c7a8671fa30c1dcb59d623b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Gaulin?= Date: Sat, 21 May 2016 16:20:51 +0200 Subject: [PATCH] Allow usage of namespace alias in the code section a a CMS page, with unit test. Fixes #2006 --- modules/cms/classes/CodeParser.php | 2 +- .../cms/reference/namespaces-aliases.php.stub | 9 ++++++ .../test/pages/code-namespaces-aliases.htm | 13 +++++++++ tests/unit/cms/classes/CmsObjectQueryTest.php | 1 + tests/unit/cms/classes/CodeParserTest.php | 29 +++++++++++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/cms/reference/namespaces-aliases.php.stub create mode 100644 tests/fixtures/themes/test/pages/code-namespaces-aliases.htm diff --git a/modules/cms/classes/CodeParser.php b/modules/cms/classes/CodeParser.php index e0761dbdb..8242b9085 100644 --- a/modules/cms/classes/CodeParser.php +++ b/modules/cms/classes/CodeParser.php @@ -91,7 +91,7 @@ class CodeParser $body = preg_replace('/^\s*function/m', 'public function', $body); $codeNamespaces = []; - $pattern = '/(use\s+[a-z0-9_\\\\]+;\n?)/mi'; + $pattern = '/(use\s+[a-z0-9_\\\\]+(\s+as\s+[a-z0-9_]+)?;\n?)/mi'; preg_match_all($pattern, $body, $namespaces); $body = preg_replace($pattern, '', $body); diff --git a/tests/fixtures/cms/reference/namespaces-aliases.php.stub b/tests/fixtures/cms/reference/namespaces-aliases.php.stub new file mode 100644 index 000000000..4213c29d1 --- /dev/null +++ b/tests/fixtures/cms/reference/namespaces-aliases.php.stub @@ -0,0 +1,9 @@ + +== +

Page

\ No newline at end of file diff --git a/tests/unit/cms/classes/CmsObjectQueryTest.php b/tests/unit/cms/classes/CmsObjectQueryTest.php index d9b3c57e3..ef89bda17 100644 --- a/tests/unit/cms/classes/CmsObjectQueryTest.php +++ b/tests/unit/cms/classes/CmsObjectQueryTest.php @@ -65,6 +65,7 @@ class CmsObjectQueryTest extends TestCase "blog-archive", "blog-post", "code-namespaces", + "code-namespaces-aliases", "component-custom-render", "component-partial", "component-partial-nesting", diff --git a/tests/unit/cms/classes/CodeParserTest.php b/tests/unit/cms/classes/CodeParserTest.php index 431aa129d..da9443dbe 100644 --- a/tests/unit/cms/classes/CodeParserTest.php +++ b/tests/unit/cms/classes/CodeParserTest.php @@ -260,6 +260,35 @@ class CodeParserTest extends TestCase $this->assertEquals($referenceContents, $this->getContents($info['filePath'])); } + public function testNamespacesAliases() + { + $theme = Theme::load('test'); + + $page = Page::load($theme, 'code-namespaces-aliases.htm'); + $this->assertNotEmpty($page); + + $parser = new CodeParser($page); + $info = $parser->parse(); + + $this->assertInternalType('array', $info); + $this->assertArrayHasKey('filePath', $info); + $this->assertArrayHasKey('className', $info); + $this->assertArrayHasKey('source', $info); + + $this->assertFileExists($info['filePath']); + $controller = new Controller($theme); + $obj = $parser->source($page, null, $controller); + $this->assertInstanceOf('\Cms\Classes\PageCode', $obj); + + $referenceFilePath = base_path().'/tests/fixtures/cms/reference/namespaces-aliases.php.stub'; + $this->assertFileExists($referenceFilePath); + $referenceContents = $this->getContents($referenceFilePath); + + $referenceContents = str_replace('{className}', $info['className'], $referenceContents); + + $this->assertEquals($referenceContents, $this->getContents($info['filePath'])); + } + // // Helpers //