Skip to content

Commit

Permalink
Merge pull request silverstripe#9744 from creative-commoners/pulls/4/…
Browse files Browse the repository at this point in the history
…improve-scalar-response-handling

Scalar fixes
  • Loading branch information
emteknetnz authored Oct 28, 2020
2 parents f00f641 + e89ae93 commit cf79be8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Control/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ public function handleRequest(HTTPRequest $request)
*/
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(
Expand Down
2 changes: 1 addition & 1 deletion src/Core/ClassInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public static function shortName($nameOrObject)
*/
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)) {
Expand Down
56 changes: 53 additions & 3 deletions tests/php/Core/ClassInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Core\Tests;

use DateTime;
use ReflectionException;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Tests\ClassInfoTest\BaseClass;
Expand All @@ -15,6 +16,7 @@
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;
Expand Down Expand Up @@ -266,9 +268,57 @@ public function testClassesWithExtensionWithDynamicallyRemovedExtensions()
);
}

/**
* @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(
Expand Down
19 changes: 19 additions & 0 deletions tests/php/Core/ClassInfoTest/HasMethod.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit cf79be8

Please sign in to comment.