RepositoryProviderTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 124
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A tearDown() 0 8 1
A testRegister() 0 18 1
B testFactoryCreatesDirectory() 0 29 2
B testFactoryFailToCreateDirectory() 0 25 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
/**
21
 * Override mkdir() in target namespace for testing
22
 *
23
 * @return int
24
 */
25
namespace Baleen\Cli\Provider;
26
27
use BaleenTest\Cli\Provider\RepositoryProviderTest;
28
29
function mkdir()
30
{
31
    $mkDirResult = RepositoryProviderTest::$mkDirResult;
32
    if (null === $mkDirResult) { // means we didn't want to mock it, so call the real function
33
        $mkDirResult = call_user_func_array('mkdir', func_get_args());
34
    }
35
    return $mkDirResult;
36
}
37
38
namespace BaleenTest\Cli\Provider;
39
40
use Baleen\Cli\Exception\CliException;
41
use Baleen\Cli\Provider\Services;
42
use Baleen\Migrations\Migration\Factory\SimpleFactory;
43
use Baleen\Migrations\Repository\DirectoryRepository;
44
use Composer\Autoload\ClassLoader;
45
use Mockery as m;
46
47
/**
48
 * Class RepositoryProviderTest
49
 * @author Gabriel Somoza <[email protected]>
50
 */
51
class RepositoryProviderTest extends ServiceProviderTestCase
52
{
53
    /** @var ClassLoader */
54
    protected $autoloader;
55
56
    /** @var boolean Used to mock PHP's "mkdir" function */
57
    public static $mkDirResult;
58
59
    /**
60
     * setUp
61
     */
62
    public function setUp()
63
    {
64
        parent::setUp();
65
66
        $autoloaderMock = m::mock(ClassLoader::class);
67
        $autoloaderMock->shouldReceive('addPsr4')->with(__NAMESPACE__ . '\\', __DIR__);
68
        $this->autoloader = $autoloaderMock;
69
70
        $this->setInstance(m::mock(\Baleen\Cli\Provider\RepositoryProvider::class)->makePartial());
71
72
        $this->getContainer()
73
            ->shouldReceive('get')
74
            ->zeroOrMoreTimes()
75
            ->with(Services::AUTOLOADER)
76
            ->andReturn($this->autoloader);
77
    }
78
79
    /**
80
     * tearDown
81
     */
82
    public function tearDown()
83
    {
84
        $this->config = null;
85
        $this->autoloader = null;
86
        $this->instance = null;
87
        $this->container = null;
88
        parent::tearDown();
89
    }
90
91
    /**
92
     * testRegister
93
     */
94
    public function testRegister()
95
    {
96
        $this->config->shouldReceive('getMigrationsNamespace')->once()->andReturn(__NAMESPACE__);
97
        $this->config->shouldReceive('getMigrationsDirectoryPath')->once()->andReturn(__DIR__);
98
99
        $this->assertSingletonProvided(
100
            Services::MIGRATION_FACTORY,
101
            $this->assertCallbackInstanceOf(SimpleFactory::class),
102
            'string'
103
        );
104
105
        $this->assertSingletonProvided(
106
            Services::REPOSITORY,
107
            $this->assertCallbackInstanceOf(DirectoryRepository::class, [$this->config, new SimpleFactory()])
108
        )->shouldReceive('withArguments')->with([Services::CONFIG, Services::MIGRATION_FACTORY]);
109
110
        $this->getInstance()->register();
111
    }
112
113
    /**
114
     * testFactoryCreatesDirectory
115
     */
116
    public function testFactoryCreatesDirectory()
117
    {
118
        $newDir = __DIR__ . '/newdir';
119
        $this->assertFalse(file_exists($newDir), sprintf('expected directory "%s" to not exist', $newDir));
120
121
        $this->config->shouldReceive('getMigrationsNamespace')->once()->andReturn(__NAMESPACE__);
122
        $this->config->shouldReceive('getMigrationsDirectoryPath')->once()->andReturn($newDir);
123
124
        // TODO: refactor across tests
125
        $this->assertSingletonProvided(
126
            Services::MIGRATION_FACTORY,
127
            $this->assertCallbackInstanceOf(SimpleFactory::class),
128
            'string'
129
        );
130
131
        $this->assertSingletonProvided(
132
            Services::REPOSITORY,
133
            $this->assertCallbackInstanceOf(DirectoryRepository::class, [$this->config, new SimpleFactory()])
134
        )->shouldReceive('withArguments')->with([Services::CONFIG, Services::MIGRATION_FACTORY]);
135
136
        try {
137
            $this->getInstance()->register();
138
            $this->assertTrue(file_exists($newDir), sprintf('expected directory "%s" to have been created', $newDir));
139
        } catch (\Exception $e) {
140
            // nothing
141
        } finally {
142
            rmdir($newDir);
143
        }
144
    }
145
146
    /**
147
     * testFactoryFailToCreateDirectory
148
     */
149
    public function testFactoryFailToCreateDirectory()
150
    {
151
        $newDir = __DIR__ . '/newdir';
152
        $this->assertFalse(file_exists($newDir), sprintf('expected directory "%s" to not exist', $newDir));
153
154
        $this->config->shouldNotReceive('getMigrationsNamespace');
155
        $this->config->shouldReceive('getMigrationsDirectoryPath')->once()->andReturn($newDir);
156
157
        // TODO: refactor across tests
158
        $this->assertSingletonProvided(
159
            Services::MIGRATION_FACTORY,
160
            $this->assertCallbackInstanceOf(SimpleFactory::class),
161
            'string'
162
        );
163
164
        $this->assertSingletonProvided(
165
            Services::REPOSITORY,
166
            $this->assertCallbackInstanceOf(DirectoryRepository::class, [$this->config, new SimpleFactory()])
167
        )->shouldReceive('withArguments')->with([Services::CONFIG, Services::MIGRATION_FACTORY]);
168
169
        self::$mkDirResult = false;
170
171
        $this->setExpectedException(CliException::class);
172
        $this->getInstance()->register();
173
    }
174
}
175