Completed
Push — master ( 32bf99...3c4ae0 )
by Christopher
06:22
created

EdmxTest::pluralize()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 5
nop 3
1
<?php
2
3
namespace AlgoWeb\ODataMetadata\Tests;
4
5
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx;
6
7
class EdmxTest extends TestCase
8
{
9
    public function testIsOKAtDefault()
10
    {
11
        $msg = null;
12
        $edmx = new Edmx();
13
        $this->assertTrue($edmx->isOK($msg), $msg);
14
    }
15
16
    public function testDefaultSerializeOk()
17
    {
18
        $ds = DIRECTORY_SEPARATOR;
19
        $msg = null;
20
        $edmx = new Edmx();
21
        $this->assertTrue($edmx->isOK($msg), $msg);
22
        $this->assertNull($msg);
23
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
24
        $serializer =
25
            \JMS\Serializer\SerializerBuilder::create()
26
                ->addMetadataDir($ymlDir)
27
                ->build();
28
        $d = $serializer->serialize($edmx, "xml");
29
        $this->v3MetadataAgainstXSD($d);
30
    }
31
32
    public function v3MetadataAgainstXSD($data)
33
    {
34
        $ds = DIRECTORY_SEPARATOR;
35
        $xml = new \DOMDocument();
36
        $xml->loadXML($data);
37
        $xml->schemaValidate(dirname(__DIR__) . $ds . "xsd" . $ds . "/Microsoft.Data.Entity.Design.Edmx_3.xsd");
38
    }
39
40
    public function testWithSingleEntitySerializeOk()
41
    {
42
        $ds = DIRECTORY_SEPARATOR;
43
        $msg = null;
44
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
45
        $NewEntity->setName("simpleEntityType");
46
        $this->assertTrue($NewEntity->isOK($msg), $msg);
47
        $this->assertNull($msg);
48
        $edmx = new Edmx();
49
        $edmx->getDataServices()[0]->addToEntityType($NewEntity);
50
        $this->assertTrue($edmx->isOK($msg), $msg);
51
        $this->assertNull($msg);
52
53
54
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
55
        $serializer =
56
            \JMS\Serializer\SerializerBuilder::create()
57
                ->addMetadataDir($ymlDir)
58
                ->build();
59
        $d = $serializer->serialize($edmx, "xml");
60
        $this->v3MetadataAgainstXSD($d);
61
    }
62
63
    public function testWithSingleEntityWithPropertiesSerializeOk()
64
    {
65
        $ds = DIRECTORY_SEPARATOR;
66
        $msg = null;
67
        $NewProperty = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType();
68
        $NewProperty->setName("TheFirstProperty");
69
        $NewProperty->setType("String");
70
        $this->assertTrue($NewProperty->isOK($msg), $msg);
71
        $this->assertNull($msg);
72
73
        $NewEntity = new \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType();
74
        $NewEntity->setName("simpleEntityType");
75
        $NewEntity->addToProperty($NewProperty);
76
        $this->assertTrue($NewEntity->isOK($msg), $msg);
77
        $this->assertNull($msg);
78
79
        $entitySet = new \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType();
80
        $entitySet->setName($this->pluralize(2, $NewEntity->getName()));
81
        $entitySet->setEntityType($NewEntity->getname());
82
        $this->assertTrue($entitySet->isOK($msg), $msg);
83
        $this->assertNull($msg);
84
        $edmx = new Edmx();
85
        $edmx->getDataServices()[0]->addToEntityType($NewEntity);
86
        $edmx->getDataServices()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
87
        $this->assertTrue($edmx->isOK($msg), $msg);
88
        $this->assertNull($msg);
89
90
91
        $ymlDir = dirname(__DIR__) . $ds . "src" . $ds . "MetadataV3" . $ds . "JMSmetadata";
92
        $serializer =
93
            \JMS\Serializer\SerializerBuilder::create()
94
                ->addMetadataDir($ymlDir)
95
                ->build();
96
        $d = $serializer->serialize($edmx, "xml");
97
        die($d);
98
        $this->v3MetadataAgainstXSD($d);
0 ignored issues
show
Unused Code introduced by
$this->v3MetadataAgainstXSD($d); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
99
    }
100
101
    /**
102
     * Pluralizes a word if quantity is not one.
103
     *
104
     * @param int $quantity Number of items
105
     * @param string $singular Singular form of word
106
     * @param string $plural Plural form of word; function will attempt to deduce plural form from singular if not provided
107
     * @return string Pluralized word if quantity is not one, otherwise singular
108
     */
109 View Code Duplication
    public static function pluralize($quantity, $singular, $plural = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        if ($quantity == 1 || !strlen($singular)) return $singular;
112
        if ($plural !== null) return $plural;
113
114
        $last_letter = strtolower($singular[strlen($singular) - 1]);
115
        switch ($last_letter) {
116
            case 'y':
117
                return substr($singular, 0, -1) . 'ies';
118
            case 's':
119
                return $singular . 'es';
120
            default:
121
                return $singular . 's';
122
        }
123
    }
124
}
125