Completed
Push — master ( e6c9d7...cc6bd7 )
by Grégoire
12:23
created

tests/Action/SearchActionTest.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Action;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Action\SearchAction;
18
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
19
use Sonata\AdminBundle\Admin\Pool;
20
use Sonata\AdminBundle\Search\SearchHandler;
21
use Sonata\AdminBundle\Templating\TemplateRegistry;
22
use Sonata\AdminBundle\Tests\Fixtures\Admin\CleanAdmin;
23
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
24
use Symfony\Component\DependencyInjection\Container;
25
use Symfony\Component\HttpFoundation\JsonResponse;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
29
class SearchActionTest extends TestCase
30
{
31
    private $container;
32
    private $pool;
33
    private $action;
34
    private $templating;
35
    private $breadcrumbsBuilder;
36
37
    protected function setUp(): void
38
    {
39
        $this->container = new Container();
40
41
        $this->pool = new Pool($this->container, 'title', 'logo.png');
42
        $templateRegistry = new TemplateRegistry([
43
            'search' => 'search.html.twig',
44
            'layout' => 'layout.html.twig',
45
        ]);
46
47
        $this->breadcrumbsBuilder = $this->createMock(BreadcrumbsBuilderInterface::class);
48
        $this->searchHandler = $this->createMock(SearchHandler::class);
0 ignored issues
show
The property searchHandler does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
50
        $this->action = new SearchAction(
51
            $this->pool,
52
            $this->searchHandler,
0 ignored issues
show
$this->searchHandler is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundle\Search\SearchHandler>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
            $templateRegistry,
54
            $this->breadcrumbsBuilder
0 ignored issues
show
$this->breadcrumbsBuilder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...crumbsBuilderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
        );
56
        $this->action->setContainer($this->container);
57
        $this->templating = $this->prophesize(EngineInterface::class);
58
        $this->container->set('templating', $this->templating->reveal());
59
    }
60
61
    public function testGlobalPage(): void
62
    {
63
        $request = new Request(['q' => 'some search']);
64
        $this->templating->render('search.html.twig', [
65
            'base_template' => 'layout.html.twig',
66
            'breadcrumbs_builder' => $this->breadcrumbsBuilder,
67
            'admin_pool' => $this->pool,
68
            'query' => 'some search',
69
            'groups' => [],
70
        ])->willReturn(new Response());
71
        $this->templating->renderResponse('search.html.twig', [
72
            'base_template' => 'layout.html.twig',
73
            'breadcrumbs_builder' => $this->breadcrumbsBuilder,
74
            'admin_pool' => $this->pool,
75
            'query' => 'some search',
76
            'groups' => [],
77
        ], null)->willReturn(new Response());
78
79
        // NEXT_MAJOR: simplify this when dropping php 5
80
        $action = $this->action;
81
        $this->assertInstanceOf(Response::class, $action($request));
82
    }
83
84
    public function testAjaxCall(): void
85
    {
86
        $admin = new CleanAdmin('code', 'class', 'controller');
87
        $this->container->set('foo', $admin);
88
        $this->pool->setAdminServiceIds(['foo']);
89
        $request = new Request(['admin' => 'foo']);
90
        $request->headers->set('X-Requested-With', 'XMLHttpRequest');
91
92
        // NEXT_MAJOR: simplify this when dropping php 5
93
        $action = $this->action;
94
        $this->assertInstanceOf(JsonResponse::class, $action($request));
95
    }
96
}
97