Sfoglia il codice sorgente

Subject model: added methods to retrieve schema of identity table

Fabian Peter Hammerle 8 anni fa
parent
commit
cb29933afa
2 ha cambiato i file con 46 aggiunte e 0 eliminazioni
  1. 22 0
      Subject.php
  2. 24 0
      tests/SubjectTest.php

+ 22 - 0
Subject.php

@@ -33,6 +33,28 @@ class Subject extends \yii\db\ActiveRecord
         return \Yii::$app->user->identityClass;
     }
 
+    /**
+     * @return \yii\db\TableSchema
+     */
+    public static function getIdentityTableSchema()
+    {
+        $cls = Subject::getIdentityClass();
+        return (new $cls)->getTableSchema();
+    }
+
+    /**
+     * @return \yii\db\ColumnSchema
+     */
+    public static function getIdentityIdSchema()
+    {
+        $keys = array_filter(
+            self::getIdentityTableSchema()->columns,
+            function($c) { return $c->isPrimaryKey; }
+        );
+        assert(sizeof($keys) == 1);
+        return array_pop($keys);
+    }
+
     // public function getIdentity()
     // {
     //     return $this->hasOne(self::getIdentityClass(), ['id' => 'identity_id']);

+ 24 - 0
tests/SubjectTest.php

@@ -43,4 +43,28 @@ class SubjectTest extends TestCase
         $this->assertEquals(1, sizeof($dup->getErrors()));
         $this->assertEquals(1, sizeof($dup->getErrors('distinguished_name')));
     }
+
+    public function testGetIdentityClass()
+    {
+        $this->assertEquals(
+            'fphammerle\yii2\auth\clientcert\tests\models\User',
+            Subject::getIdentityClass()
+        );
+    }
+
+    public function testGetIdentityTableSchema()
+    {
+        $this->assertEquals(
+            'user',
+            Subject::getIdentityTableSchema()->name
+        );
+    }
+
+    public function testGetIdentityIdSchema()
+    {
+        $schema = Subject::getIdentityIdSchema();
+        $this->assertEquals('id', $schema->name);
+        $this->assertEquals('integer', $schema->type);
+        $this->assertTrue($schema->isPrimaryKey);
+    }
 }