Browse Source

Remove type getters.

Only keep a getter for the result type of nested `WhenThenExpression`'s,
it's somewhat internal and required to guess the `CASE` expression's
return type.
ndm2 4 years ago
parent
commit
ebf7d31ce1

+ 0 - 14
src/Database/Expression/CaseExpressionInterface.php

@@ -59,13 +59,6 @@ interface CaseExpressionInterface extends ExpressionInterface, TypedResultInterf
     public function value($value, ?string $valueType = null);
 
     /**
-     * Returns the case value type.
-     *
-     * @return string|null
-     */
-    public function getValueType(): ?string;
-
-    /**
      * Sets the `WHEN` value for a `WHEN ... THEN ...` expression, or a
      * self-contained expression that holds both the value for `WHEN`
      * and the value for `THEN`.
@@ -278,13 +271,6 @@ interface CaseExpressionInterface extends ExpressionInterface, TypedResultInterf
     public function else($result, ?string $type = null);
 
     /**
-     * Returns the type of the `ELSE` result value.
-     *
-     * @return string|null The result type, or `null` if none has been set yet.
-     */
-    public function getElseType(): ?string;
-
-    /**
      * Returns the abstract type that this expression will return.
      *
      * If no type has been explicitly set via `setReturnType()`, this

+ 1 - 17
src/Database/Expression/CaseStatementExpression.php

@@ -149,14 +149,6 @@ class CaseStatementExpression implements CaseExpressionInterface
     /**
      * @inheritDoc
      */
-    public function getValueType(): ?string
-    {
-        return $this->valueType;
-    }
-
-    /**
-     * @inheritDoc
-     */
     public function when($when, $type = null)
     {
         if ($this->whenBuffer !== null) {
@@ -249,14 +241,6 @@ class CaseStatementExpression implements CaseExpressionInterface
     /**
      * @inheritDoc
      */
-    public function getElseType(): ?string
-    {
-        return $this->elseType;
-    }
-
-    /**
-     * @inheritDoc
-     */
     public function getReturnType(): string
     {
         if ($this->returnType !== null) {
@@ -265,7 +249,7 @@ class CaseStatementExpression implements CaseExpressionInterface
 
         $types = [];
         foreach ($this->when as $when) {
-            $type = $when->getThenType();
+            $type = $when->getResultType();
             if ($type !== null) {
                 $types[] = $type;
             }

+ 1 - 9
src/Database/Expression/WhenThenExpression.php

@@ -176,14 +176,6 @@ class WhenThenExpression implements WhenThenExpressionInterface
     /**
      * @inheritDoc
      */
-    public function getWhenType()
-    {
-        return $this->whenType;
-    }
-
-    /**
-     * @inheritDoc
-     */
     public function then($result, ?string $type = null)
     {
         if (
@@ -215,7 +207,7 @@ class WhenThenExpression implements WhenThenExpressionInterface
     /**
      * @inheritDoc
      */
-    public function getThenType(): ?string
+    public function getResultType(): ?string
     {
         return $this->thenType;
     }

+ 2 - 10
src/Database/Expression/WhenThenExpressionInterface.php

@@ -60,14 +60,6 @@ interface WhenThenExpressionInterface extends ExpressionInterface
     public function when($when, $type = null);
 
     /**
-     * Returns the `WHEN` value type.
-     *
-     * @return array|string|null
-     * @see when()
-     */
-    public function getWhenType();
-
-    /**
      * Sets the `THEN` result value.
      *
      * @param \Cake\Database\ExpressionInterface|object|scalar|null $result The result value.
@@ -78,10 +70,10 @@ interface WhenThenExpressionInterface extends ExpressionInterface
     public function then($result, ?string $type = null);
 
     /**
-     * Returns the `THEN` result value type.
+     * Returns the expression's result value type.
      *
      * @return string|null
      * @see then()
      */
-    public function getThenType(): ?string;
+    public function getResultType(): ?string;
 }

+ 47 - 69
tests/TestCase/Database/Expression/CaseStatementExpressionTest.php

@@ -34,6 +34,8 @@ use Cake\I18n\FrozenDate;
 use Cake\I18n\FrozenTime;
 use Cake\I18n\Time;
 use Cake\Test\test_app\TestApp\Database\Expression\CustomWhenThenExpression;
+use Cake\Test\test_app\TestApp\Stub\CaseStatementExpressionStub;
+use Cake\Test\test_app\TestApp\Stub\WhenThenExpressionStub;
 use Cake\TestSuite\TestCase;
 use InvalidArgumentException;
 use LogicException;
@@ -230,28 +232,49 @@ class CaseStatementExpressionTest extends TestCase
      */
     public function testInferValueType($value, ?string $type): void
     {
-        $expression = (new CaseStatementExpression(new TypeMap(['Table.column' => 'boolean'])))
+        $expression = new CaseStatementExpressionStub(new TypeMap(['Table.column' => 'boolean']));
+
+        $this->assertNull($expression->getValueType());
+
+        $expression
             ->value($value)
             ->when(1)
             ->then(2);
+
         $this->assertSame($type, $expression->getValueType());
     }
 
+    public function whenTypeInferenceDataProvider(): array
+    {
+        return [
+            ['1', 'string'],
+            [1, 'integer'],
+            [1.0, 'float'],
+            [true, 'boolean'],
+            [ChronosDate::now(), 'date'],
+            [Chronos::now(), 'datetime'],
+            [new IdentifierExpression('Table.column'), 'boolean'],
+            [['Table.column' => true], null],
+            [new stdClass(), null],
+        ];
+    }
+
     /**
-     * @dataProvider typeInferenceDataProvider
+     * @dataProvider whenTypeInferenceDataProvider
      * @param mixed $value The value from which to infer the type.
      * @param string|null $type The expected type.
      */
     public function testInferWhenType($value, ?string $type): void
     {
-        $this->skipIf(
-            $value === null,
-            '`\Cake\Database\Expression\CaseExpression::when()` does not accept `null`'
-        );
+        $expression = new CaseStatementExpressionStub(new TypeMap(['Table.column' => 'boolean']));
+        $expression->when(new WhenThenExpressionStub($expression->getTypeMap()));
+
+        $this->assertNull($expression->clause('when')[0]->getWhenType());
 
-        $expression = (new CaseStatementExpression(new TypeMap(['Table.column' => 'boolean'])))
+        $expression->clause('when')[0]
             ->when($value)
             ->then(1);
+
         $this->assertSame($type, $expression->clause('when')[0]->getWhenType());
     }
 
@@ -260,12 +283,20 @@ class CaseStatementExpressionTest extends TestCase
      * @param mixed $value The value from which to infer the type.
      * @param string|null $type The expected type.
      */
-    public function testInferThenType($value, ?string $type): void
+    public function testInferResultType($value, ?string $type): void
     {
-        $expression = (new CaseStatementExpression(new TypeMap(['Table.column' => 'boolean'])))
+        $expression = (new CaseStatementExpressionStub(new TypeMap(['Table.column' => 'boolean'])))
+            ->when(function (WhenThenExpressionInterface $whenThen) {
+                return $whenThen;
+            });
+
+        $this->assertNull($expression->clause('when')[0]->getResultType());
+
+        $expression->clause('when')[0]
             ->when(['Table.column' => true])
             ->then($value);
-        $this->assertSame($type, $expression->clause('when')[0]->getThenType());
+
+        $this->assertSame($type, $expression->clause('when')[0]->getResultType());
     }
 
     /**
@@ -275,8 +306,12 @@ class CaseStatementExpressionTest extends TestCase
      */
     public function testInferElseType($value, ?string $type): void
     {
-        $expression = (new CaseStatementExpression(new TypeMap(['Table.column' => 'boolean'])))
-            ->else($value);
+        $expression = new CaseStatementExpressionStub(new TypeMap(['Table.column' => 'boolean']));
+
+        $this->assertNull($expression->getElseType());
+
+        $expression->else($value);
+
         $this->assertSame($type, $expression->getElseType());
     }
 
@@ -945,17 +980,6 @@ class CaseStatementExpressionTest extends TestCase
         $this->assertSame(1, $expression->clause('value'));
     }
 
-    public function testGetValueType(): void
-    {
-        $expression = new CaseStatementExpression();
-
-        $this->assertNull($expression->getValueType());
-
-        $expression->value(1);
-
-        $this->assertSame('integer', $expression->getValueType());
-    }
-
     public function testGetWhenClause(): void
     {
         $when = ['Table.column' => true];
@@ -1000,27 +1024,6 @@ class CaseStatementExpressionTest extends TestCase
         $this->assertSame(1, $expression->clause('when')[0]->clause('when'));
     }
 
-    public function testWhenGetWhenType(): void
-    {
-        $expression = (new CaseStatementExpression())
-            ->when(function (WhenThenExpressionInterface $whenThen) {
-                return $whenThen;
-            });
-
-        $this->assertNull($expression->clause('when')[0]->getWhenType());
-
-        $expression->clause('when')[0]->when(1);
-
-        $this->assertSame('integer', $expression->clause('when')[0]->getWhenType());
-
-        $types = [
-            'Table.column' => 'boolean',
-        ];
-        $expression->clause('when')[0]->when(['Table.column' => true], $types);
-
-        $this->assertSame($types, $expression->clause('when')[0]->getWhenType());
-    }
-
     public function testWhenGetThenClause(): void
     {
         $expression = (new CaseStatementExpression())
@@ -1035,20 +1038,6 @@ class CaseStatementExpressionTest extends TestCase
         $this->assertSame(1, $expression->clause('when')[0]->clause('then'));
     }
 
-    public function testWhenGetThenType(): void
-    {
-        $expression = (new CaseStatementExpression())
-            ->when(function (WhenThenExpressionInterface $whenThen) {
-                return $whenThen;
-            });
-
-        $this->assertNull($expression->clause('when')[0]->getThenType());
-
-        $expression->clause('when')[0]->then(1);
-
-        $this->assertSame('integer', $expression->clause('when')[0]->getThenType());
-    }
-
     public function testGetElseClause(): void
     {
         $expression = new CaseStatementExpression();
@@ -1063,17 +1052,6 @@ class CaseStatementExpressionTest extends TestCase
         $this->assertSame(2, $expression->clause('else'));
     }
 
-    public function testGetElseType(): void
-    {
-        $expression = new CaseStatementExpression();
-
-        $this->assertNull($expression->getElseType());
-
-        $expression->else(1);
-
-        $this->assertSame('integer', $expression->getElseType());
-    }
-
     // endregion
 
     // region Order based syntax

+ 28 - 0
tests/test_app/TestApp/Stub/CaseStatementExpressionStub.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Cake\Test\test_app\TestApp\Stub;
+
+use Cake\Database\Expression\CaseStatementExpression;
+
+class CaseStatementExpressionStub extends CaseStatementExpression
+{
+    /**
+     * Returns the case value type.
+     *
+     * @return string|null
+     */
+    public function getValueType(): ?string
+    {
+        return $this->valueType;
+    }
+
+    /**
+     * Returns the type of the `ELSE` result value.
+     *
+     * @return string|null The result type, or `null` if none has been set yet.
+     */
+    public function getElseType(): ?string
+    {
+        return $this->elseType;
+    }
+}

+ 19 - 0
tests/test_app/TestApp/Stub/WhenThenExpressionStub.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace Cake\Test\test_app\TestApp\Stub;
+
+use Cake\Database\Expression\WhenThenExpression;
+
+class WhenThenExpressionStub extends WhenThenExpression
+{
+    /**
+     * Returns the `WHEN` value type.
+     *
+     * @return array|string|null
+     * @see when()
+     */
+    public function getWhenType(): ?string
+    {
+        return $this->whenType;
+    }
+}