Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Avoid Attempted to call an undefined method named "getName" with tests #37

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Admin/BlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
44 changes: 44 additions & 0 deletions Tests/Admin/BlockAdminTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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;
use Sonata\DashboardBundle\Tests\Fixtures\Entity\FooGetName;
use Sonata\DashboardBundle\Tests\Fixtures\Entity\FooGetNameNull;
use Sonata\DashboardBundle\Tests\Fixtures\Entity\FooNoGetName;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add an @author tag

/**
* BlockAdminTest.
*
* @author Stéphane Paté <[email protected]>
*/
class BlockAdminTest extends \PHPUnit_Framework_TestCase
{
public function testToString()
{
$admin = new BlockAdmin(
'sonata.dashboard.admin.block',
'DashboardBundle\Entity\BaseBlock',
'SonataDashboardBundle:BlockAdmin'
);

$s = new FooGetName();
$this->assertSame('GetName', $admin->toString($s));

$s = new FooGetNameNull();
$this->assertSame('GetNameNull', $admin->toString($s));

$s = new FooNoGetName();
$this->assertSame('NoGetName', $admin->toString($s));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a data provider. Some will probably also ask you to move dummy classes to a fixtures namespace (what do you think @sonata-project/contributors ? ).

}
}
11 changes: 11 additions & 0 deletions Tests/Fixtures/Entity/FooGetName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Sonata\DashboardBundle\Tests\Fixtures\Entity;

class FooGetName
{
public function getName()
{
return 'GetName';
}
}
16 changes: 16 additions & 0 deletions Tests/Fixtures/Entity/FooGetNameNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Sonata\DashboardBundle\Tests\Fixtures\Entity;

class FooGetNameNull
{
public function getName()
{
return;
}

public function __toString()
{
return 'GetNameNull';
}
}
11 changes: 11 additions & 0 deletions Tests/Fixtures/Entity/FooNoGetName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Sonata\DashboardBundle\Tests\Fixtures\Entity;

class FooNoGetName
{
public function __toString()
{
return 'NoGetName';
}
}