瀏覽代碼

tests: setup identity model

Fabian Peter Hammerle 8 年之前
父節點
當前提交
7e5c44c9ee
共有 5 個文件被更改,包括 76 次插入9 次删除
  1. 2 2
      Authenticator.php
  2. 1 1
      composer.json
  3. 12 6
      tests/ArrayHelperTest.php
  4. 24 0
      tests/migrations/CreateUserTable.php
  5. 37 0
      tests/models/User.php

+ 2 - 2
ClientCertAuth.php → Authenticator.php

@@ -1,8 +1,8 @@
 <?php
 
-namespace fphammerle\yii2;
+namespace fphammerle\yii2\auth\clientcert;
 
-class ClientCertAuth extends \yii\base\Component
+class Authenticator extends \yii\base\Component
 {
     /**
      * @return string

+ 1 - 1
composer.json

@@ -16,7 +16,7 @@
     },
     "autoload": {
         "psr-4": {
-            "fphammerle\\yii2\\": ""
+            "fphammerle\\yii2\\auth\\clientcert\\": ""
         }
     }
 }

+ 12 - 6
tests/ArrayHelperTest.php

@@ -1,12 +1,14 @@
 <?php
 
-use fphammerle\yii2\ClientCertAuth;
+namespace fphammerle\yii2\auth\clientcert\tests;
+
+use \fphammerle\yii2\auth\clientcert\Authenticator;
 
 class ClientCertAuthTest extends \PHPUnit_Framework_TestCase
 {
     public function mockApplication()
     {
-        return new \yii\web\Application([
+        $app = new \yii\web\Application([
             'id' => 'yii2-client-cert-auth-test',
             'basePath' => __DIR__,
             // 'vendorPath' => dirname(__DIR__) . '/vendor',
@@ -15,16 +17,20 @@ class ClientCertAuthTest extends \PHPUnit_Framework_TestCase
                     'class' => '\yii\db\Connection',
                     'dsn' => 'sqlite::memory:',
                 ],
+                'user' => [
+                    'identityClass' => models\User::className(),
+                ],
             ],
         ]);
+        (new migrations\CreateUserTable)->up();
+        return $app;
     }
 
     public function testDb()
     {
         $app = $this->mockApplication();
-        $app->db->createCommand('CREATE TABLE test ( x INT )')->execute();
-        $app->db->createCommand('INSERT INTO test (x) VALUES (1), (2), (4)')->execute();
-        var_dump($app->db->createCommand('SELECT * FROM test')->queryAll());
-        $this->assertEquals('bar', ClientCertAuth::foo());
+        var_dump($app->db->createCommand('SELECT * FROM user')->queryAll());
+        $this->assertEquals('bar', Authenticator::foo());
+        $this->assertNull($app->user->getIdentity());
     }
 }

+ 24 - 0
tests/migrations/CreateUserTable.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace fphammerle\yii2\auth\clientcert\tests\migrations;
+
+use fphammerle\yii2\auth\clientcert\tests\models\User;
+
+class CreateUserTable extends \yii\db\Migration
+{
+    public function safeUp()
+    {
+        $this->createTable(
+            User::tableName(),
+            [
+                'id' => $this->primaryKey(),
+                'username' => $this->string(16)->notNull()->unique(),
+                ]
+            );
+    }
+
+    public function safeDown()
+    {
+        $this->dropTable(User::tableName());
+    }
+}

+ 37 - 0
tests/models/User.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace fphammerle\yii2\auth\clientcert\tests\models;
+
+class User extends \yii\db\ActiveRecord
+    implements \yii\web\IdentityInterface
+{
+    public static function tableName()
+    {
+        return 'user';
+    }
+
+    public static function findIdentity($id)
+    {
+        return static::findOne(['id' => $id]);
+    }
+
+    public static function findIdentityByAccessToken($token, $type = null)
+    {
+        throw new NotSupportedException();
+    }
+
+    public function getId()
+    {
+        return $this->primaryKey;
+    }
+
+    public function getAuthKey()
+    {
+        return sprintf('auth-key-%d', $this->id);
+    }
+
+    public function validateAuthKey($authKey)
+    {
+        return $this->getAuthKey() === $authKey;
+    }
+}