|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WikibaseSolutions\CypherDSL\Tests\Unit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use TypeError; |
| 7 | +use WikibaseSolutions\CypherDSL\Expressions\Functions\Func; |
| 8 | +use WikibaseSolutions\CypherDSL\Expressions\Literals\Boolean; |
| 9 | +use WikibaseSolutions\CypherDSL\Expressions\Literals\Float_; |
| 10 | +use WikibaseSolutions\CypherDSL\Expressions\Literals\Integer; |
| 11 | +use WikibaseSolutions\CypherDSL\Expressions\Literals\List_; |
| 12 | +use WikibaseSolutions\CypherDSL\Expressions\Literals\Literal; |
| 13 | +use WikibaseSolutions\CypherDSL\Expressions\Literals\Map; |
| 14 | +use WikibaseSolutions\CypherDSL\Expressions\Literals\String_; |
| 15 | +use WikibaseSolutions\CypherDSL\Expressions\RawExpression; |
| 16 | +use WikibaseSolutions\CypherDSL\Expressions\Variable; |
| 17 | +use WikibaseSolutions\CypherDSL\Patterns\Node; |
| 18 | +use WikibaseSolutions\CypherDSL\Patterns\Relationship; |
| 19 | +use WikibaseSolutions\CypherDSL\Query; |
| 20 | +use function WikibaseSolutions\CypherDSL\float; |
| 21 | +use function WikibaseSolutions\CypherDSL\function_; |
| 22 | +use function WikibaseSolutions\CypherDSL\integer; |
| 23 | +use function WikibaseSolutions\CypherDSL\list_; |
| 24 | +use function WikibaseSolutions\CypherDSL\literal; |
| 25 | +use function WikibaseSolutions\CypherDSL\map; |
| 26 | +use function WikibaseSolutions\CypherDSL\node; |
| 27 | +use function WikibaseSolutions\CypherDSL\query; |
| 28 | +use function WikibaseSolutions\CypherDSL\raw; |
| 29 | +use function WikibaseSolutions\CypherDSL\relationship; |
| 30 | +use function WikibaseSolutions\CypherDSL\string; |
| 31 | +use function WikibaseSolutions\CypherDSL\variable; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests the functions in "functions.php". |
| 35 | + * |
| 36 | + * @coversNothing |
| 37 | + */ |
| 38 | +class FunctionsTest extends TestCase |
| 39 | +{ |
| 40 | + public function testQueryReturnsQuery() |
| 41 | + { |
| 42 | + $query = query(); |
| 43 | + |
| 44 | + $this->assertInstanceOf(Query::class, $query); |
| 45 | + } |
| 46 | + |
| 47 | + public function testNodeReturnsNode() |
| 48 | + { |
| 49 | + $node = node('label'); |
| 50 | + |
| 51 | + $this->assertInstanceOf(Node::class, $node); |
| 52 | + $this->assertSame('(:label)', $node->toQuery()); |
| 53 | + } |
| 54 | + |
| 55 | + public function testNodeAcceptsEmptyLabel() |
| 56 | + { |
| 57 | + $node = node(); |
| 58 | + |
| 59 | + $this->assertInstanceOf(Node::class, $node); |
| 60 | + $this->assertSame('()', $node->toQuery()); |
| 61 | + } |
| 62 | + |
| 63 | + public function testNodeOnlyAcceptsStringLabel() |
| 64 | + { |
| 65 | + $this->expectException(TypeError::class); |
| 66 | + |
| 67 | + node([]); |
| 68 | + } |
| 69 | + |
| 70 | + public function testRelationshipReturnsRelationship() |
| 71 | + { |
| 72 | + $relationship = relationship(Relationship::DIR_RIGHT); |
| 73 | + |
| 74 | + $this->assertInstanceOf(Relationship::class, $relationship); |
| 75 | + $this->assertSame('-->', $relationship->toQuery()); |
| 76 | + } |
| 77 | + |
| 78 | + public function testVariableReturnsVariable() |
| 79 | + { |
| 80 | + $variable = variable('foobar'); |
| 81 | + |
| 82 | + $this->assertInstanceOf(Variable::class, $variable); |
| 83 | + $this->assertSame('foobar', $variable->toQuery()); |
| 84 | + } |
| 85 | + |
| 86 | + public function testVariableAcceptsEmptyArgument() |
| 87 | + { |
| 88 | + $variable = variable(); |
| 89 | + |
| 90 | + $this->assertInstanceOf(Variable::class, $variable); |
| 91 | + $this->assertStringMatchesFormat('var%s', $variable->toQuery()); |
| 92 | + } |
| 93 | + |
| 94 | + public function testVariableOnlyAcceptsString() |
| 95 | + { |
| 96 | + $this->expectException(TypeError::class); |
| 97 | + |
| 98 | + variable([]); |
| 99 | + } |
| 100 | + |
| 101 | + public function testLiteralReturnsLiteralClass() |
| 102 | + { |
| 103 | + $literal = literal(); |
| 104 | + |
| 105 | + $this->assertSame(Literal::class, $literal); |
| 106 | + } |
| 107 | + |
| 108 | + public function testLiteralReturnsLiteral() |
| 109 | + { |
| 110 | + $literal = literal('foobar'); |
| 111 | + |
| 112 | + $this->assertInstanceOf(String_::class, $literal); |
| 113 | + $this->assertSame("'foobar'", $literal->toQuery()); |
| 114 | + |
| 115 | + $literal = literal(true); |
| 116 | + |
| 117 | + $this->assertInstanceOf(Boolean::class, $literal); |
| 118 | + $this->assertSame("true", $literal->toQuery()); |
| 119 | + } |
| 120 | + |
| 121 | + public function testBooleanReturnsBoolean() |
| 122 | + { |
| 123 | + $boolean = \WikibaseSolutions\CypherDSL\boolean(true); |
| 124 | + |
| 125 | + $this->assertInstanceOf(Boolean::class, $boolean); |
| 126 | + $this->assertSame('true', $boolean->toQuery()); |
| 127 | + } |
| 128 | + |
| 129 | + public function testBooleanOnlyAcceptsBoolean() |
| 130 | + { |
| 131 | + $this->expectException(TypeError::class); |
| 132 | + |
| 133 | + \WikibaseSolutions\CypherDSL\boolean([]); |
| 134 | + } |
| 135 | + |
| 136 | + public function testStringReturnsString() |
| 137 | + { |
| 138 | + $string = string('hello world'); |
| 139 | + |
| 140 | + $this->assertInstanceOf(String_::class, $string); |
| 141 | + $this->assertSame("'hello world'", $string->toQuery()); |
| 142 | + } |
| 143 | + |
| 144 | + public function testStringOnlyAcceptsString() |
| 145 | + { |
| 146 | + $this->expectException(TypeError::class); |
| 147 | + |
| 148 | + string([]); |
| 149 | + } |
| 150 | + |
| 151 | + public function testIntegerReturnsInteger() |
| 152 | + { |
| 153 | + $integer = integer(1); |
| 154 | + |
| 155 | + $this->assertInstanceOf(Integer::class, $integer); |
| 156 | + $this->assertSame("1", $integer->toQuery()); |
| 157 | + } |
| 158 | + |
| 159 | + public function testIntegerOnlyAcceptsInteger() |
| 160 | + { |
| 161 | + $this->expectException(TypeError::class); |
| 162 | + |
| 163 | + integer([]); |
| 164 | + } |
| 165 | + |
| 166 | + public function testFloatReturnsFloat() |
| 167 | + { |
| 168 | + $float = float(1.1); |
| 169 | + |
| 170 | + $this->assertInstanceOf(Float_::class, $float); |
| 171 | + $this->assertSame("1.1", $float->toQuery()); |
| 172 | + } |
| 173 | + |
| 174 | + public function testFloatOnlyAcceptsFloat() |
| 175 | + { |
| 176 | + $this->expectException(TypeError::class); |
| 177 | + |
| 178 | + float([]); |
| 179 | + } |
| 180 | + |
| 181 | + public function testListReturnsList() |
| 182 | + { |
| 183 | + $list = list_(['a', 'b', 'c']); |
| 184 | + |
| 185 | + $this->assertInstanceOf(List_::class, $list); |
| 186 | + $this->assertSame("['a', 'b', 'c']", $list->toQuery()); |
| 187 | + } |
| 188 | + |
| 189 | + public function testListOnlyAcceptsArray() |
| 190 | + { |
| 191 | + $this->expectException(TypeError::class); |
| 192 | + |
| 193 | + list_(1); |
| 194 | + } |
| 195 | + |
| 196 | + public function testMapReturnsMap() |
| 197 | + { |
| 198 | + $map = map(['a' => 'b', 'c' => 'd']); |
| 199 | + |
| 200 | + $this->assertInstanceOf(Map::class, $map); |
| 201 | + $this->assertSame("{a: 'b', c: 'd'}", $map->toQuery()); |
| 202 | + } |
| 203 | + |
| 204 | + public function testMapOnlyAcceptsArray() |
| 205 | + { |
| 206 | + $this->expectException(TypeError::class); |
| 207 | + |
| 208 | + map(1); |
| 209 | + } |
| 210 | + |
| 211 | + public function testFunctionReturnsFuncClass() |
| 212 | + { |
| 213 | + $function = function_(); |
| 214 | + |
| 215 | + $this->assertSame(Func::class, $function); |
| 216 | + } |
| 217 | + |
| 218 | + public function testRawReturnsRawExpression() |
| 219 | + { |
| 220 | + $raw = raw('(unimplemented feature)'); |
| 221 | + |
| 222 | + $this->assertInstanceOf(RawExpression::class, $raw); |
| 223 | + $this->assertSame('(unimplemented feature)', $raw->toQuery()); |
| 224 | + } |
| 225 | + |
| 226 | + public function testRawOnlyAcceptsString() |
| 227 | + { |
| 228 | + $this->expectException(TypeError::class); |
| 229 | + |
| 230 | + raw([]); |
| 231 | + } |
| 232 | +} |
0 commit comments