Completed
Pull Request — master (#5823)
by Mikhail
13:41
created

AbstractDriverTest::setField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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 LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
18
*/
19
20
namespace Doctrine\Tests\ORM\Mapping\Symfony;
21
22
/**
23
 * @group DDC-1418
24
 */
25
abstract class AbstractDriverTest extends \PHPUnit_Framework_TestCase
26
{
27
    public function testFindMappingFile()
28
    {
29
        $driver = $this->getDriver(array(
30
            'MyNamespace\MySubnamespace\EntityFoo' => 'foo',
31
            'MyNamespace\MySubnamespace\Entity' => $this->dir,
0 ignored issues
show
Bug introduced by
The property dir does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        ));
33
34
        touch($filename = $this->dir.'/Foo'.$this->getFileExtension());
35
        $this->assertEquals($filename, $driver->getLocator()->findMappingFile('MyNamespace\MySubnamespace\Entity\Foo'));
36
    }
37
38
    public function testFindMappingFileInSubnamespace()
39
    {
40
        $driver = $this->getDriver(array(
41
            'MyNamespace\MySubnamespace\Entity' => $this->dir,
42
        ));
43
44
        touch($filename = $this->dir.'/Foo.Bar'.$this->getFileExtension());
45
        $this->assertEquals($filename, $driver->getLocator()->findMappingFile('MyNamespace\MySubnamespace\Entity\Foo\Bar'));
46
    }
47
48
    public function testFindMappingFileNamespacedFoundFileNotFound()
49
    {
50
        $this->setExpectedException(
51
            'Doctrine\Common\Persistence\Mapping\MappingException',
52
            "No mapping file found named"
53
        );
54
55
        $driver = $this->getDriver(array(
56
            'MyNamespace\MySubnamespace\Entity' => $this->dir,
57
        ));
58
59
        $driver->getLocator()->findMappingFile('MyNamespace\MySubnamespace\Entity\Foo');
60
    }
61
62
    public function testFindMappingNamespaceNotFound()
63
    {
64
        $this->setExpectedException(
65
            'Doctrine\Common\Persistence\Mapping\MappingException',
66
            "No mapping file found named 'Foo".$this->getFileExtension()."' for class 'MyOtherNamespace\MySubnamespace\Entity\Foo'."
67
        );
68
69
        $driver = $this->getDriver(array(
70
            'MyNamespace\MySubnamespace\Entity' => $this->dir,
71
        ));
72
73
        $driver->getLocator()->findMappingFile('MyOtherNamespace\MySubnamespace\Entity\Foo');
74
    }
75
76
    protected function setUp()
77
    {
78
        $this->dir = sys_get_temp_dir().'/abstract_driver_test';
79
        @mkdir($this->dir, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
80
    }
81
82
    protected function tearDown()
83
    {
84
        $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->dir), \RecursiveIteratorIterator::CHILD_FIRST);
85
86
        foreach ($iterator as $path) {
87
            if ($path->isDir()) {
88
                @rmdir($path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
89
            } else {
90
                @unlink($path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
91
            }
92
        }
93
94
        @rmdir($this->dir);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
95
    }
96
97
    abstract protected function getFileExtension();
98
    abstract protected function getDriver(array $paths = array());
99
}
100