1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\AdminBundle\Tests\Action; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Sonata\AdminBundle\Action\SearchAction; |
16
|
|
|
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface; |
17
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
18
|
|
|
use Sonata\AdminBundle\Search\SearchHandler; |
19
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistry; |
20
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\Admin\CleanAdmin; |
21
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
22
|
|
|
use Symfony\Component\DependencyInjection\Container; |
23
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
26
|
|
|
|
27
|
|
|
class SearchActionTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
private $container; |
30
|
|
|
private $pool; |
31
|
|
|
private $action; |
32
|
|
|
private $templating; |
33
|
|
|
private $breadcrumbsBuilder; |
34
|
|
|
|
35
|
|
|
protected function setUp() |
36
|
|
|
{ |
37
|
|
|
$this->container = new Container(); |
38
|
|
|
|
39
|
|
|
$this->pool = new Pool($this->container, 'title', 'logo.png'); |
40
|
|
|
$templateRegistry = new TemplateRegistry([ |
41
|
|
|
'search' => 'search.html.twig', |
42
|
|
|
'layout' => 'layout.html.twig', |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
$this->breadcrumbsBuilder = $this->createMock(BreadcrumbsBuilderInterface::class); |
46
|
|
|
$this->searchHandler = $this->createMock(SearchHandler::class); |
47
|
|
|
|
48
|
|
|
$this->action = new SearchAction( |
49
|
|
|
$this->pool, |
50
|
|
|
$this->searchHandler, |
51
|
|
|
$templateRegistry, |
52
|
|
|
$this->breadcrumbsBuilder |
53
|
|
|
); |
54
|
|
|
$this->action->setContainer($this->container); |
55
|
|
|
$this->templating = $this->prophesize(EngineInterface::class); |
56
|
|
|
$this->container->set('templating', $this->templating->reveal()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGlobalPage() |
60
|
|
|
{ |
61
|
|
|
$request = new Request(['q' => 'some search']); |
62
|
|
|
$this->templating->render('search.html.twig', [ |
63
|
|
|
'base_template' => 'layout.html.twig', |
64
|
|
|
'breadcrumbs_builder' => $this->breadcrumbsBuilder, |
65
|
|
|
'admin_pool' => $this->pool, |
66
|
|
|
'query' => 'some search', |
67
|
|
|
'groups' => [], |
68
|
|
|
])->willReturn(new Response()); |
69
|
|
|
$this->templating->renderResponse('search.html.twig', [ |
70
|
|
|
'base_template' => 'layout.html.twig', |
71
|
|
|
'breadcrumbs_builder' => $this->breadcrumbsBuilder, |
72
|
|
|
'admin_pool' => $this->pool, |
73
|
|
|
'query' => 'some search', |
74
|
|
|
'groups' => [], |
75
|
|
|
], null)->willReturn(new Response()); |
76
|
|
|
|
77
|
|
|
// NEXT_MAJOR: simplify this when dropping php 5 |
78
|
|
|
$action = $this->action; |
79
|
|
|
$this->assertInstanceOf(Response::class, $action($request)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testAjaxCall() |
83
|
|
|
{ |
84
|
|
|
$admin = new CleanAdmin('code', 'class', 'controller'); |
85
|
|
|
$this->container->set('foo', $admin); |
86
|
|
|
$this->pool->setAdminServiceIds(['foo']); |
87
|
|
|
$request = new Request(['admin' => 'foo']); |
88
|
|
|
$request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
89
|
|
|
|
90
|
|
|
// NEXT_MAJOR: simplify this when dropping php 5 |
91
|
|
|
$action = $this->action; |
92
|
|
|
$this->assertInstanceOf(JsonResponse::class, $action($request)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|