mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #9744 from creative-commoners/pulls/4/improve-scalar-response-handling
Scalar fixes
This commit is contained in:
commit
cf79be8e2c
@ -230,7 +230,9 @@ class Controller extends RequestHandler implements TemplateGlobalProvider
|
||||
*/
|
||||
protected function prepareResponse($response)
|
||||
{
|
||||
if ($response instanceof HTTPResponse) {
|
||||
if (!is_object($response)) {
|
||||
$this->getResponse()->setBody($response);
|
||||
} elseif ($response instanceof HTTPResponse) {
|
||||
if (isset($_REQUEST['debug_request'])) {
|
||||
$class = static::class;
|
||||
Debug::message(
|
||||
|
@ -400,7 +400,7 @@ class ClassInfo
|
||||
*/
|
||||
public static function hasMethod($object, $method)
|
||||
{
|
||||
if (empty($object)) {
|
||||
if (empty($object) || (!is_object($object) && !is_string($object))) {
|
||||
return false;
|
||||
}
|
||||
if (method_exists($object, $method)) {
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace SilverStripe\Core\Tests;
|
||||
|
||||
use DateTime;
|
||||
use ReflectionException;
|
||||
use SilverStripe\Core\ClassInfo;
|
||||
use SilverStripe\Core\Tests\ClassInfoTest\BaseClass;
|
||||
@ -15,6 +16,7 @@ use SilverStripe\Core\Tests\ClassInfoTest\ExtensionTest1;
|
||||
use SilverStripe\Core\Tests\ClassInfoTest\ExtensionTest2;
|
||||
use SilverStripe\Core\Tests\ClassInfoTest\GrandChildClass;
|
||||
use SilverStripe\Core\Tests\ClassInfoTest\HasFields;
|
||||
use SilverStripe\Core\Tests\ClassInfoTest\HasMethod;
|
||||
use SilverStripe\Core\Tests\ClassInfoTest\NoFields;
|
||||
use SilverStripe\Core\Tests\ClassInfoTest\WithCustomTable;
|
||||
use SilverStripe\Core\Tests\ClassInfoTest\WithRelation;
|
||||
@ -266,9 +268,57 @@ class ClassInfoTest extends SapphireTest
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideClassSpecCases
|
||||
*/
|
||||
/** @dataProvider provideHasMethodCases */
|
||||
public function testHasMethod($object, $method, $output)
|
||||
{
|
||||
$this->assertEquals(
|
||||
$output,
|
||||
ClassInfo::hasMethod($object, $method)
|
||||
);
|
||||
}
|
||||
|
||||
public function provideHasMethodCases()
|
||||
{
|
||||
return [
|
||||
'Basic object' => [
|
||||
new DateTime(),
|
||||
'format',
|
||||
true,
|
||||
],
|
||||
'CustomMethod object' => [
|
||||
new HasMethod(),
|
||||
'example',
|
||||
true,
|
||||
],
|
||||
'Class Name' => [
|
||||
'DateTime',
|
||||
'format',
|
||||
true,
|
||||
],
|
||||
'FQCN' => [
|
||||
'\DateTime',
|
||||
'format',
|
||||
true,
|
||||
],
|
||||
'Invalid FQCN' => [
|
||||
'--GreatTime',
|
||||
'format',
|
||||
false,
|
||||
],
|
||||
'Integer' => [
|
||||
1,
|
||||
'format',
|
||||
false,
|
||||
],
|
||||
'Array' => [
|
||||
['\DateTime'],
|
||||
'format',
|
||||
false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/** @dataProvider provideClassSpecCases */
|
||||
public function testParseClassSpec($input, $output)
|
||||
{
|
||||
$this->assertEquals(
|
||||
|
19
tests/php/Core/ClassInfoTest/HasMethod.php
Normal file
19
tests/php/Core/ClassInfoTest/HasMethod.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\Core\Tests\ClassInfoTest;
|
||||
|
||||
use SilverStripe\Core\CustomMethods;
|
||||
use SilverStripe\Dev\TestOnly;
|
||||
|
||||
/**
|
||||
* Example of class with hasMethod() implementation
|
||||
*/
|
||||
class HasMethod implements TestOnly
|
||||
{
|
||||
use CustomMethods;
|
||||
|
||||
public function example()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user