File: MockRegistry.php

package info (click to toggle)
php-mock 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 340 kB
  • sloc: php: 1,740; makefile: 18; xml: 17; sh: 7
file content (92 lines) | stat: -rw-r--r-- 1,837 bytes parent folder | download | duplicates (3)
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
<?php

namespace phpmock;

/**
 * Enabled mock registry.
 *
 * @author Markus Malkusch <markus@malkusch.de>
 * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
 * @license http://www.wtfpl.net/txt/copying/ WTFPL
 * @see MockBuilder
 * @internal
 */
class MockRegistry
{
    /**
     * @var Mock[] Enabled mocks.
     */
    private $mocks = [];

    /**
     * @var MockRegistry Singleton.
     */
    private static $instance;

    /**
     * Returns the singleton.
     *
     * @return MockRegistry The singleton.
     */
    public static function getInstance()
    {
        if (empty(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Returns true if the mock is already registered.
     *
     * @param Mock $mock The mock.
     * @return bool True if the mock is registered.
     */
    public function isRegistered(Mock $mock)
    {
        return isset($this->mocks[$mock->getFQFN()]);
    }

    /**
     * Returns the registered mock.
     *
     * @param string $fqfn The fully qualified function name.
     * @return Mock The registered Mock.
     * @see Mock::getFQFN()
     */
    public function getMock($fqfn)
    {
        if (! isset($this->mocks[$fqfn])) {
            return null;
        }
        return $this->mocks[$fqfn];
    }

    /**
     * Registers a mock.
     *
     * @param Mock $mock The mock.
     */
    public function register(Mock $mock)
    {
        $this->mocks[$mock->getFQFN()] = $mock;
    }

    /**
     * Unregisters all mocks.
     */
    public function unregisterAll()
    {
        $this->mocks = [];
    }

    /**
     * Unregisters a mock.
     *
     * @param Mock $mock The mock.
     */
    public function unregister(Mock $mock)
    {
        unset($this->mocks[$mock->getFQFN()]);
    }
}