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

Commit

Permalink
Merge pull request #54 from stphnpt/0.x
Browse files Browse the repository at this point in the history
Check for a valid object in `BlockAdmin::toString`
  • Loading branch information
greg0ire authored Jun 17, 2016
2 parents 81beb25 + 6363e44 commit 745ae4f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Admin/BlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,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 All @@ -243,7 +241,14 @@ public function getPersistentParameters()
*/
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);
}

/**
Expand Down
59 changes: 59 additions & 0 deletions Tests/Admin/BlockAdminTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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;
use Symfony\Component\HttpFoundation\Request;

/**
* BlockAdminTest.
*
* @author Stéphane Paté <[email protected]>
*/
final 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));
}

public function testGetPersistentParameters()
{
$admin = new BlockAdmin(
'sonata.dashboard.admin.block',
'DashboardBundle\Entity\BaseBlock',
'SonataDashboardBundle:BlockAdmin'
);

$request = new Request();
$admin->setRequest($request);

$this->assertArrayHasKey('type', $admin->getPersistentParameters());
}
}
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;

final 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;

final 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;

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

0 comments on commit 745ae4f

Please sign in to comment.