Completed
Push — master ( d002ef...5731d9 )
by Vincent
03:51 queued 01:08
created

testGlobalSearchReturnsResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Block;
15
16
use Sonata\AdminBundle\Admin\AbstractAdmin;
17
use Sonata\AdminBundle\Admin\Pool;
18
use Sonata\AdminBundle\Block\AdminSearchBlockService;
19
use Sonata\AdminBundle\Search\SearchHandler;
20
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
21
use Sonata\BlockBundle\Test\BlockServiceTestCase;
22
use Symfony\Component\HttpFoundation\Response;
23
24
/**
25
 * @author Sullivan Senechal <[email protected]>
26
 */
27
class AdminSearchBlockServiceTest extends BlockServiceTestCase
28
{
29
    /**
30
     * @var Pool
31
     */
32
    private $pool;
33
34
    /**
35
     * @var SearchHandler
36
     */
37
    private $searchHandler;
38
39
    /**
40
     * @var TemplateRegistryInterface
41
     */
42
    private $templateRegistry;
43
44
    protected function setUp(): void
45
    {
46
        parent::setUp();
47
48
        $this->pool = $this->createMock(Pool::class);
49
        $this->searchHandler = $this->createMock(SearchHandler::class);
50
        $this->templateRegistry = $this->createMock(TemplateRegistryInterface::class);
51
        $this->templateRegistry->method('getTemplate')->willReturn('@SonataAdmin/Block/block_search_result.html.twig');
52
    }
53
54
    public function testDefaultSettings(): void
55
    {
56
        $blockService = new AdminSearchBlockService($this->twig, $this->pool, $this->searchHandler);
57
        $blockContext = $this->getBlockContext($blockService);
58
59
        $this->assertSettings([
60
            'admin_code' => '',
61
            'query' => '',
62
            'page' => 0,
63
            'per_page' => 10,
64
            'icon' => '<i class="fa fa-list"></i>',
65
        ], $blockContext);
66
    }
67
68
    public function testGlobalSearchReturnsResponse(): void
69
    {
70
        $admin = $this->createMock(AbstractAdmin::class);
71
72
        $blockService = new AdminSearchBlockService($this->twig, $this->pool, $this->searchHandler, $this->templateRegistry);
73
        $blockContext = $this->getBlockContext($blockService);
74
75
        $this->searchHandler->expects(self::once())->method('search')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Search\SearchHandler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
        $this->pool->expects(self::once())->method('getAdminByAdminCode')->willReturn($admin);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
        $admin->expects(self::once())->method('checkAccess')->with('list');
78
79
        $response = $blockService->execute($blockContext);
80
81
        static::assertSame('', $response->getContent());
82
        static::assertSame(Response::HTTP_OK, $response->getStatusCode());
83
    }
84
85
    public function testGlobalSearchReturnsEmptyWhenFiltersAreDisabled(): void
86
    {
87
        $admin = $this->createMock(AbstractAdmin::class);
88
89
        $blockService = new AdminSearchBlockService($this->twig, $this->pool, $this->searchHandler);
90
        $blockContext = $this->getBlockContext($blockService);
91
92
        $this->searchHandler->expects(self::once())->method('search')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Search\SearchHandler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
        $this->pool->expects(self::once())->method('getAdminByAdminCode')->willReturn($admin);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
        $admin->expects(self::once())->method('checkAccess')->with('list');
95
96
        $this->twig->expects(self::never())->method('render');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Twig\Environment>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
98
        $response = $blockService->execute($blockContext);
99
100
        static::assertSame('', $response->getContent());
101
        static::assertSame(Response::HTTP_NO_CONTENT, $response->getStatusCode());
102
    }
103
}
104