AbstractControllerTestCase.php 3.06 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
4
5
6
<?php

namespace UnicaenTest\Controller;

use Doctrine\ORM\EntityManager;
use Prophecy\Argument;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
7
use Prophecy\Prophecy\ObjectProphecy;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
8
use Prophecy\Prophet;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
9
10
use Laminas\ServiceManager\ServiceManager;
use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

/**
 * Class AbstractControllerTest
 *
 * @package ApplicationTest\Controller
 */
abstract class AbstractControllerTestCase extends AbstractHttpControllerTestCase
{
//    public function setUp()
//    {
//        // The module configuration should still be applicable for tests.
//        // You can override configuration here with test case specific values,
//        // such as sample view templates, path stacks, module_listener_options,
//        // etc.
//        $configOverrides = [];
//
//        $this->setApplicationConfig(ArrayUtils::merge(
//            // Grabbing the full application configuration:
//            include __DIR__ . '/../../../../config/application.config.php',
//            $configOverrides
//        ));
//
//        parent::setUp();
//    }

    /**
     * @param string $authorizeServiceClass
38
     * @return ObjectProphecy|UnicaenAuth\Service\AuthorizeService
Bertrand Gauthier's avatar
Bertrand Gauthier committed
39
40
41
42
     */
    protected function mockAuthorizeService($authorizeServiceClass)
    {
        $prophet = new Prophet();
Bertrand Gauthier's avatar
Bertrand Gauthier committed
43

44
        /** @var ObjectProphecy|UnicaenAuth\Service\AuthorizeService $prophecy */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
45
46
47
48
49
        $prophecy = $prophet->prophesize();
        $prophecy->willExtend($authorizeServiceClass);

        $service = $prophecy->reveal();

Bertrand Gauthier's avatar
Bertrand Gauthier committed
50
        /* @var $services ServiceManager */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
51
52
53
54
55
56
57
58
59
60
61
        $services = $this->getApplication()->getServiceManager();
        $services->setAllowOverride(true);
        $services->setFactory('BjyAuthorize\Service\Authorize', function() use ($service) {
            return $service;
        });

        return $prophecy;
    }

    /**
     * @param string $userContextServiceClass
62
     * @return ObjectProphecy|UnicaenAuth\Service\UserContext
Bertrand Gauthier's avatar
Bertrand Gauthier committed
63
64
65
66
     */
    protected function mockUserContextService($userContextServiceClass)
    {
        $prophet = new Prophet();
Bertrand Gauthier's avatar
Bertrand Gauthier committed
67

68
        /** @var ObjectProphecy|UnicaenAuth\Service\UserContext $prophecy */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
69
70
71
72
73
74
75
76
        $prophecy = $prophet->prophesize();
        $prophecy->willExtend($userContextServiceClass);

        $prophecy->getEventManager()->willReturn($this->getApplication()->getEventManager());
        $prophecy->setServiceLocator(Argument::any())->willReturn();

        $service = $prophecy->reveal();

Bertrand Gauthier's avatar
Bertrand Gauthier committed
77
        /* @var $services ServiceManager */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        $services = $this->getApplication()->getServiceManager();
        $services->setAllowOverride(true);
        $services->setFactory('UnicaenAuth\Service\UserContext', function() use ($service) {
            return $service;
        });

        return $prophecy;
    }

    /**
     * @var EntityManager
     */
    private $em;

    /**
     * @param string $name
     * @return EntityManager
     */
    protected function em($name = 'orm_default')
    {
        if (null === $this->em) {
            $this->em = $this->getApplicationServiceLocator()->get("doctrine.entitymanager.$name");
        }

        return $this->em;
    }
}