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

use PDOException;
5
use PHPUnit\Framework\TestCase;
6
7
8
9
10
11
12
13
14
15
16
17
use UnicaenAuth\Authentication\Adapter\Db;
use UnicaenAuth\Options\ModuleOptions;
use Zend\Http\PhpEnvironment\Request;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\Stdlib\Parameters;
use ZfcUser\Authentication\Adapter\AdapterChainEvent;

/**
 * Description of DbTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
18
class DbTest extends TestCase
19
20
21
22
{
    protected $adapter;
    protected $moduleOptions;
    protected $mapper;
23

24
25
26
27
28
29
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
30
31
32
33
34
        $this->moduleOptions = $moduleOptions = new ModuleOptions([
            'cas' => [
                'connection' => [
                    'default' => [
                        'params' => [
35
36
37
38
39
                            'hostname' => 'cas.unicaen.fr',
                            'port' => 443,
                            'version' => "2.0",
                            'uri' => "",
                            'debug' => false,
40
41
42
43
44
45
46
47
48
                        ],
                    ],
                ],
            ],
        ]);

        $this->mapper = $mapper = $this->getMock('ZfcUser\Mapper\User', ['findByUsername', 'findByEmail']);

        $serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager', ['get']);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        $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;
                       }));
63

64
65
66
        $this->adapter = new Db();
        $this->adapter->setServiceManager($serviceManager);
    }
67

68
69
    public function getException()
    {
70
71
72
73
        return [
            [new PDOException()],
            [new ServiceNotFoundException()],
        ];
74
    }
75

76
77
78
79
80
81
82
83
    /**
     * @dataProvider getException
     */
    public function testAuthenticateReturnsFalseIfExceptionThrown($exception)
    {
        $this->mapper->expects($this->once())
                     ->method($this->logicalOr('findByUsername', 'findByEmail'))
                     ->will($this->throwException($exception));
84

85
        $request = new Request();
86
87
        $request->setPost(new Parameters(['identity' => 'bob', 'credential' => "xxxxx"]));

88
89
        $event = new AdapterChainEvent();
        $event->setRequest($request);
90

91
92
93
        $result = $this->adapter->authenticate($event);
        $this->assertFalse($result);
    }
94

95
96
97
    public function testAuthenticateReturnsParentMethodResult()
    {
        $request = new Request();
98
99
        $request->setPost(new Parameters(['identity' => 'bob', 'credential' => "xxxxx"]));

100
101
        $event = new AdapterChainEvent();
        $event->setRequest($request);
102

103
104
105
        $result = $this->adapter->authenticate($event);
        $this->assertFalse($result);
    }
106
}