File: ConnectionTest.php

package info (click to toggle)
php-pda-pheanstalk 4.0.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 608 kB
  • sloc: php: 2,658; xml: 60; makefile: 15
file content (98 lines) | stat: -rw-r--r-- 3,213 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

namespace Pheanstalk;

use Pheanstalk\Command\StatsCommand;
use Pheanstalk\Contract\SocketFactoryInterface;
use Pheanstalk\Contract\SocketInterface;
use Pheanstalk\Exception\ConnectionException;
use Pheanstalk\Exception\SocketException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
 * Tests for the Connection.
 * Relies on a running beanstalkd server.
 */
class ConnectionTest extends TestCase
{
    const CONNECT_TIMEOUT = 2;


    public function connectionProvider($test, $host = SERVER_HOST, $port = SERVER_PORT)
    {
        return [
            'stream' => [new Connection(new SocketFactory($host, $port, 1, SocketFactory::STREAM))],
            'fsockopen' => [new Connection(new SocketFactory($host, $port, 1, SocketFactory::FSOCKOPEN))],
            'socket' => [new Connection(new SocketFactory($host, $port, 1, SocketFactory::SOCKET))],
            'autodetect' =>[new Connection(new SocketFactory($host, $port, 1, SocketFactory::AUTODETECT))]
        ];
    }

    public function badPortConnectionProvider($test)
    {
        return $this->connectionProvider($test, SERVER_HOST, SERVER_PORT + 1);
    }

    public function badHostConnectionProvider($test)
    {
        return $this->connectionProvider($test, SERVER_HOST . 'abc', SERVER_PORT);
    }

    /**
     * @dataProvider badPortConnectionProvider
     */
    public function testConnectionFailsToIncorrectPort(Connection $connection)
    {
        $this->expectException(ConnectionException::class);
        $command = new Command\UseCommand('test');
        $connection->dispatchCommand($command);
    }


    /**
     * @dataProvider badHostConnectionProvider
     */
    public function testConnectionFailsToIncorrectHost(Connection $connection)
    {
        $this->expectException(ConnectionException::class);
        $command = new Command\UseCommand('test');
        $connection->dispatchCommand($command);
    }

    /**
     * @throws Exception\ClientException
     * @dataProvider connectionProvider
     */
    public function testDispatchCommandSuccessful(Connection $connection)
    {
        $command = new Command\UseCommand('test');
        $response = $connection->dispatchCommand($command);

        $this->assertInstanceOf(Contract\ResponseInterface::class, $response);
    }

    /**
     * @dataProvider connectionProvider
     */
    public function testDisconnect(Connection $connection)
    {
        $pheanstalk = new Pheanstalk(new Connection(new SocketFactory(SERVER_HOST, SERVER_PORT)));
        $baseCount = $pheanstalk->stats()['current-connections'];


        $this->assertEquals($baseCount, $pheanstalk->stats()['current-connections']);

        // initial connection
        $connection->dispatchCommand(new Command\StatsCommand());
        $this->assertEquals($baseCount + 1, $pheanstalk->stats()['current-connections']);

        // disconnect
        $connection->disconnect();
        $this->assertEquals($baseCount, $pheanstalk->stats()['current-connections']);

        // auto-reconnect
        $connection->dispatchCommand(new Command\StatsCommand());
        $this->assertEquals($baseCount + 1, $pheanstalk->stats()['current-connections']);
    }
}