DbTest.php 4.28 KB
Newer Older
1
2
3
4
<?php
namespace UnicaenAuthTest\Authentication\Adapter;

use PDOException;
5
use PHPUnit_Framework_TestCase;
6
7
use UnicaenAuth\Authentication\Adapter\Db;
use UnicaenAuth\Options\ModuleOptions;
8
use Zend\EventManager\EventInterface;
9
10
use Zend\Http\PhpEnvironment\Request;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
11
use Zend\ServiceManager\ServiceManager;
12
13
use Zend\Stdlib\Parameters;
use ZfcUser\Authentication\Adapter\AdapterChainEvent;
14
use ZfcUser\Mapper\User;
15
16
17
18
19
20

/**
 * Description of DbTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
21
class DbTest extends PHPUnit_Framework_TestCase
22
{
23
24
25
    /**
     * @var Db|\PHPUnit_Framework_MockObject_MockObject
     */
26
    protected $adapter;
27

28
    protected $moduleOptions;
29
30
31
32

    /**
     * @var User|\PHPUnit_Framework_MockObject_MockObject
     */
33
    protected $mapper;
34

35
36
37
38
39
40
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
41
42
43
44
45
        $this->moduleOptions = $moduleOptions = new ModuleOptions([
            'cas' => [
                'connection' => [
                    'default' => [
                        'params' => [
46
47
48
49
50
                            'hostname' => 'cas.unicaen.fr',
                            'port' => 443,
                            'version' => "2.0",
                            'uri' => "",
                            'debug' => false,
51
52
53
54
55
56
                        ],
                    ],
                ],
            ],
        ]);

57
        $this->mapper = $mapper = $this->createMock('ZfcUser\Mapper\User'/*, ['findByUsername', 'findByEmail']*/);
58

59
        /** @var ServiceManager|\PHPUnit_Framework_MockObject_MockObject $serviceManager */
60
        $serviceManager = $this->createMock('Zend\ServiceManager\ServiceManager'/*, ['get']*/);
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        $serviceManager->expects($this->any())
                       ->method('get')
                       ->will($this->returnCallback(function($serviceName) use ($moduleOptions, $mapper) {
                           if ('zfcuser_module_options' === $serviceName) {
                               return new \ZfcUser\Options\ModuleOptions();
                           }
                           if ('unicaen-auth_module_options' === $serviceName) {
                               return $moduleOptions;
                           }
                           if ('zfcuser_user_mapper' === $serviceName) {
                               return $mapper;
                           }
                           return null;
                       }));
75

76
77
78
        $this->adapter = new Db();
        $this->adapter->setServiceManager($serviceManager);
    }
79

80
81
    public function getException()
    {
82
        return [
83
            //[new PDOException()],
84
85
            [new ServiceNotFoundException()],
        ];
86
    }
87

88
89
    /**
     * @dataProvider getException
90
     * @param \Exception $exception
91
92
93
94
95
96
     */
    public function testAuthenticateReturnsFalseIfExceptionThrown($exception)
    {
        $this->mapper->expects($this->once())
                     ->method($this->logicalOr('findByUsername', 'findByEmail'))
                     ->will($this->throwException($exception));
97

98
        $request = new Request();
99
100
        $request->setPost(new Parameters(['identity' => 'bob', 'credential' => "xxxxx"]));

101
102
103
104
105
106
        /** @var EventInterface|\PHPUnit_Framework_MockObject_MockObject $event */
        $event = $this->createMock(EventInterface::class);
        $event
            ->expects($this->exactly(2))
            ->method('getTarget')
            ->willReturn((new AdapterChainEvent())->setRequest($request));
107

108
109
110
        $result = $this->adapter->authenticate($event);
        $this->assertFalse($result);
    }
111

112
113
114
    public function testAuthenticateReturnsParentMethodResult()
    {
        $request = new Request();
115
116
        $request->setPost(new Parameters(['identity' => 'bob', 'credential' => "xxxxx"]));

117
118
119
120
121
122
        /** @var EventInterface|\PHPUnit_Framework_MockObject_MockObject $event */
        $event = $this->createMock(EventInterface::class);
        $event
            ->expects($this->exactly(2))
            ->method('getTarget')
            ->willReturn((new AdapterChainEvent())->setRequest($request));
123

124
125
126
        $result = $this->adapter->authenticate($event);
        $this->assertFalse($result);
    }
127
}