From 7c355215ce2184fbd91a99ffdb7491232e2dab24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane?= Date: Mon, 6 Jun 2016 12:46:36 +0200 Subject: [PATCH 1/3] Avoid Attempted to call an undefined method named "getName" with tests --- Admin/BlockAdmin.php | 14 +++++--- Tests/Admin/BlockAdminTest.php | 61 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 Tests/Admin/BlockAdminTest.php diff --git a/Admin/BlockAdmin.php b/Admin/BlockAdmin.php index 72a5ad7..bd4ac53 100644 --- a/Admin/BlockAdmin.php +++ b/Admin/BlockAdmin.php @@ -255,9 +255,7 @@ public function getPersistentParameters() $parameters['composer'] = $composer; } - if ($composer = $this->getRequest()->get('type')) { - $parameters['type'] = $composer; - } + $parameters['type'] = $this->getRequest()->get('type'); return $parameters; } @@ -401,6 +399,14 @@ protected function configureFormFields(FormMapper $formMapper) */ public function toString($object) { - return $object->getName(); + if (!is_object($object)) { + return ''; + } + + if (method_exists($object, 'getName') && null !== $object->getName()) { + return (string) $object->getName(); + } + + return parent::toString($object); } } diff --git a/Tests/Admin/BlockAdminTest.php b/Tests/Admin/BlockAdminTest.php new file mode 100644 index 0000000..10f12c5 --- /dev/null +++ b/Tests/Admin/BlockAdminTest.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\DashboardBundle\Tests\Admin; + +use Sonata\AdminBundle\Admin\Admin; +use Sonata\DashboardBundle\Admin\BlockAdmin; + +class BlockAdminTest extends \PHPUnit_Framework_TestCase +{ + public function testToString() + { + $admin = new BlockAdmin('sonata.dashboard.admin.block', 'DashboardBundle\Entity\BaseBlock', 'SonataDashboardBundle:BlockAdmin'); + + $s = new DummyGetName(); + $this->assertSame('GetName', $admin->toString($s)); + + $s = new DummyGetNameNull(); + $this->assertSame('GetNameNull', $admin->toString($s)); + + $s = new DummyNoGetName(); + $this->assertSame('NoGetName', $admin->toString($s)); + } +} + +class DummyGetName +{ + public function getName() + { + return 'GetName'; + } +} + +class DummyGetNameNull +{ + public function getName() + { + return; + } + + public function __toString() + { + return 'GetNameNull'; + } +} + +class DummyNoGetName +{ + public function __toString() + { + return 'NoGetName'; + } +} From bf8651f6e644ac54ec5a046bd5adfaa0901045fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane?= Date: Mon, 6 Jun 2016 15:03:08 +0200 Subject: [PATCH 2/3] toString test updated with fixtures --- Tests/Admin/BlockAdminTest.php | 38 ++++-------------------- Tests/Fixtures/Entity/FooGetName.php | 11 +++++++ Tests/Fixtures/Entity/FooGetNameNull.php | 16 ++++++++++ Tests/Fixtures/Entity/FooNoGetName.php | 11 +++++++ 4 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 Tests/Fixtures/Entity/FooGetName.php create mode 100644 Tests/Fixtures/Entity/FooGetNameNull.php create mode 100644 Tests/Fixtures/Entity/FooNoGetName.php diff --git a/Tests/Admin/BlockAdminTest.php b/Tests/Admin/BlockAdminTest.php index 10f12c5..74bab8d 100644 --- a/Tests/Admin/BlockAdminTest.php +++ b/Tests/Admin/BlockAdminTest.php @@ -13,6 +13,9 @@ use Sonata\AdminBundle\Admin\Admin; use Sonata\DashboardBundle\Admin\BlockAdmin; +use Sonata\DashboardBundle\Tests\Fixtures\Entity\FooGetName; +use Sonata\DashboardBundle\Tests\Fixtures\Entity\FooGetNameNull; +use Sonata\DashboardBundle\Tests\Fixtures\Entity\FooNoGetName; class BlockAdminTest extends \PHPUnit_Framework_TestCase { @@ -20,42 +23,13 @@ public function testToString() { $admin = new BlockAdmin('sonata.dashboard.admin.block', 'DashboardBundle\Entity\BaseBlock', 'SonataDashboardBundle:BlockAdmin'); - $s = new DummyGetName(); + $s = new FooGetName(); $this->assertSame('GetName', $admin->toString($s)); - $s = new DummyGetNameNull(); + $s = new FooGetNameNull(); $this->assertSame('GetNameNull', $admin->toString($s)); - $s = new DummyNoGetName(); + $s = new FooNoGetName(); $this->assertSame('NoGetName', $admin->toString($s)); } } - -class DummyGetName -{ - public function getName() - { - return 'GetName'; - } -} - -class DummyGetNameNull -{ - public function getName() - { - return; - } - - public function __toString() - { - return 'GetNameNull'; - } -} - -class DummyNoGetName -{ - public function __toString() - { - return 'NoGetName'; - } -} diff --git a/Tests/Fixtures/Entity/FooGetName.php b/Tests/Fixtures/Entity/FooGetName.php new file mode 100644 index 0000000..62c10d3 --- /dev/null +++ b/Tests/Fixtures/Entity/FooGetName.php @@ -0,0 +1,11 @@ + Date: Mon, 6 Jun 2016 15:58:05 +0200 Subject: [PATCH 3/3] with @author and line breaks --- Tests/Admin/BlockAdminTest.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Tests/Admin/BlockAdminTest.php b/Tests/Admin/BlockAdminTest.php index 74bab8d..7a34e2a 100644 --- a/Tests/Admin/BlockAdminTest.php +++ b/Tests/Admin/BlockAdminTest.php @@ -17,11 +17,20 @@ use Sonata\DashboardBundle\Tests\Fixtures\Entity\FooGetNameNull; use Sonata\DashboardBundle\Tests\Fixtures\Entity\FooNoGetName; +/** + * BlockAdminTest. + * + * @author Stéphane Paté + */ class BlockAdminTest extends \PHPUnit_Framework_TestCase { public function testToString() { - $admin = new BlockAdmin('sonata.dashboard.admin.block', 'DashboardBundle\Entity\BaseBlock', 'SonataDashboardBundle:BlockAdmin'); + $admin = new BlockAdmin( + 'sonata.dashboard.admin.block', + 'DashboardBundle\Entity\BaseBlock', + 'SonataDashboardBundle:BlockAdmin' + ); $s = new FooGetName(); $this->assertSame('GetName', $admin->toString($s));