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); |
49
|
|
|
|
50
|
|
|
$this->action = new SearchAction( |
51
|
|
|
$this->pool, |
52
|
|
|
$this->searchHandler, |
53
|
|
|
$templateRegistry, |
54
|
|
|
$this->breadcrumbsBuilder |
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
|
|
|
|