2008-04-08 08:17:58 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Core\Manifest\ClassManifest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
class TokenisedRegularExpressionTest extends SapphireTest {
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTokens() {
|
2008-04-08 08:17:58 +02:00
|
|
|
return token_get_all(<<<PHP
|
|
|
|
<?php
|
|
|
|
|
|
|
|
class ClassA {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClassB{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClassC extends ParentClassC {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClassD extends ParentClassD
|
|
|
|
implements InterfaceA {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface InterfaceA {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface InterfaceB extends Something{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
class ClassE extends ParentClassE
|
2008-04-08 08:17:58 +02:00
|
|
|
implements InterfaceA,InterfaceB {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
class ClassF extends ParentClassF
|
2008-04-08 08:17:58 +02:00
|
|
|
implements InterfaceA, InterfaceB {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
}
|
|
|
|
|
2011-04-19 05:52:20 +02:00
|
|
|
interface InterfaceC extends InterfaceA, InterfaceB {
|
|
|
|
}
|
|
|
|
interface InterfaceD extends InterfaceA, InterfaceB, InterfaceC {
|
|
|
|
}
|
2012-02-12 21:22:11 +01:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
PHP
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getNamespaceTokens() {
|
2011-12-17 02:03:53 +01:00
|
|
|
return token_get_all(<<<PHP
|
|
|
|
<?php
|
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
namespace silverstripe\\test;
|
2011-12-17 02:03:53 +01:00
|
|
|
|
|
|
|
class ClassA {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClassB extends ParentClassB {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClassC extends \\ParentClassC {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClassD extends subtest\\ParentClassD {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClassE implements InterfaceE {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClassF implements \\InterfaceF {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClassG implements subtest\\InterfaceG {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
PHP
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testClassDefParser() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$parser = ClassManifest::get_class_parser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
$tokens = $this->getTokens();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
$matches = $parser->findAll($tokens);
|
|
|
|
$classes = array();
|
|
|
|
if($matches) foreach($matches as $match) $classes[$match['className']] = $match;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
$this->assertArrayHasKey('ClassA', $classes);
|
|
|
|
$this->assertArrayHasKey('ClassB', $classes);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
$this->assertArrayHasKey('ClassC', $classes);
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertEquals(array('ParentClassC'), $classes['ClassC']['extends']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
$this->assertArrayHasKey('ClassD', $classes);
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertEquals(array('ParentClassD'), $classes['ClassD']['extends']);
|
2008-04-08 08:17:58 +02:00
|
|
|
$this->assertContains('InterfaceA', $classes['ClassD']['interfaces']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
$this->assertArrayHasKey('ClassE', $classes);
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertEquals(array('ParentClassE'), $classes['ClassE']['extends']);
|
2008-04-08 08:17:58 +02:00
|
|
|
$this->assertContains('InterfaceA', $classes['ClassE']['interfaces']);
|
|
|
|
$this->assertContains('InterfaceB', $classes['ClassE']['interfaces']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
$this->assertArrayHasKey('ClassF', $classes);
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertEquals(array('ParentClassF'), $classes['ClassF']['extends']);
|
2008-04-08 08:17:58 +02:00
|
|
|
$this->assertContains('InterfaceA', $classes['ClassF']['interfaces']);
|
|
|
|
$this->assertContains('InterfaceB', $classes['ClassF']['interfaces']);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNamesapcedClassDefParser() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$parser = ClassManifest::get_namespaced_class_parser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$tokens = $this->getNamespaceTokens();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$matches = $parser->findAll($tokens);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$classes = array();
|
|
|
|
if($matches) foreach($matches as $match) $classes[$match['className']] = $match;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertArrayHasKey('ClassA', $classes);
|
|
|
|
$this->assertArrayHasKey('ClassB', $classes);
|
|
|
|
$this->assertEquals(array('ParentClassB'), $classes['ClassB']['extends']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertArrayHasKey('ClassC', $classes);
|
|
|
|
$this->assertEquals(array('\\', 'ParentClassC'), $classes['ClassC']['extends']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertArrayHasKey('ClassD', $classes);
|
|
|
|
$this->assertEquals(array('subtest', '\\', 'ParentClassD'), $classes['ClassD']['extends']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertArrayHasKey('ClassE', $classes);
|
|
|
|
$this->assertContains('InterfaceE', $classes['ClassE']['interfaces']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertArrayHasKey('ClassF', $classes);
|
|
|
|
$this->assertEquals(array('\\', 'InterfaceF'), $classes['ClassF']['interfaces']);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInterfaceDefParser() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$parser = ClassManifest::get_interface_parser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
$tokens = $this->getTokens();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
$matches = $parser->findAll($tokens);
|
|
|
|
$interfaces = array();
|
|
|
|
if($matches) foreach($matches as $match) $interfaces[$match['interfaceName']] = $match;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
$this->assertArrayHasKey('InterfaceA', $interfaces);
|
|
|
|
$this->assertArrayHasKey('InterfaceB', $interfaces);
|
2011-04-19 05:52:20 +02:00
|
|
|
$this->assertArrayHasKey('InterfaceC', $interfaces);
|
|
|
|
$this->assertArrayHasKey('InterfaceD', $interfaces);
|
2008-04-08 08:17:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNamespaceDefParser() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$parser = ClassManifest::get_namespace_parser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$namespacedTokens = $this->getNamespaceTokens();
|
|
|
|
$tokens = $this->getTokens();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$namespacedMatches = $parser->findAll($namespacedTokens);
|
|
|
|
$matches = $parser->findAll($tokens);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 02:03:53 +01:00
|
|
|
$this->assertEquals(array(), $matches);
|
2012-03-24 04:38:57 +01:00
|
|
|
$this->assertEquals(array('silverstripe', '\\', 'test'), $namespacedMatches[0]['namespaceName']);
|
2011-12-17 02:03:53 +01:00
|
|
|
}
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|