-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10516 from creative-commoners/pulls/4.11/override…
…-named-extension
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Core\Tests; | ||
|
||
use BadMethodCallException; | ||
use ReflectionProperty; | ||
use SilverStripe\Core\Config\Config; | ||
use SilverStripe\Core\Tests\ExtensionTest\NamedExtension; | ||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
class ExtensionTest extends SapphireTest | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
// Reset extra_methods so that when we set NamedExtension to null it re-evaluates which methods are available | ||
$reflectionProperty = new ReflectionProperty(DataObject::class, 'extra_methods'); | ||
$reflectionProperty->setAccessible(true); | ||
$reflectionProperty->setValue([]); | ||
// Add named extension config like we would in yaml | ||
Config::modify()->merge(DataObject::class, 'extensions', ['NamedExtension' => NamedExtension::class]); | ||
} | ||
|
||
public function testHasNamedExtension() | ||
{ | ||
$this->assertTrue(DataObject::has_extension(NamedExtension::class)); | ||
$instance = new DataObject(); | ||
$this->assertTrue($instance->hasMethod('getTestValue')); | ||
$this->assertSame('test', $instance->getTestValue()); | ||
} | ||
|
||
public function testRemoveNamedExtension() | ||
{ | ||
Config::modify()->merge(DataObject::class, 'extensions', ['NamedExtension' => null]); | ||
$this->assertFalse(DataObject::has_extension(NamedExtension::class)); | ||
$instance = new DataObject(); | ||
$this->assertFalse($instance->hasMethod('getTestValue')); | ||
} | ||
|
||
public function testRemoveNamedExtensionException() | ||
{ | ||
Config::modify()->merge(DataObject::class, 'extensions', ['NamedExtension' => null]); | ||
$instance = new DataObject(); | ||
$this->expectException(BadMethodCallException::class); | ||
$instance->getTestValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Core\Tests\ExtensionTest; | ||
|
||
use SilverStripe\Core\Extension; | ||
use SilverStripe\Dev\TestOnly; | ||
|
||
class NamedExtension extends Extension implements TestOnly | ||
{ | ||
public function getTestValue() | ||
{ | ||
return 'test'; | ||
} | ||
} |