Browse Source

test creation of users

Fabian Peter Hammerle 8 years ago
parent
commit
f6d78db48c
3 changed files with 20 additions and 5 deletions
  1. 2 1
      composer.json
  2. 13 4
      tests/ArrayHelperTest.php
  3. 5 0
      tests/models/User.php

+ 2 - 1
composer.json

@@ -12,7 +12,8 @@
         "yiisoft/yii2": "^2.0"
     },
     "require-dev": {
-        "phpunit/phpunit": "4.8.*"
+        "phpunit/phpunit": "4.8.*",
+        "fphammerle/helpers": "^1.2"
     },
     "autoload": {
         "psr-4": {

+ 13 - 4
tests/ArrayHelperTest.php

@@ -2,6 +2,7 @@
 
 namespace fphammerle\yii2\auth\clientcert\tests;
 
+use \fphammerle\helpers\ArrayHelper;
 use \fphammerle\yii2\auth\clientcert\Authenticator;
 
 class ClientCertAuthTest extends \PHPUnit_Framework_TestCase
@@ -22,15 +23,23 @@ class ClientCertAuthTest extends \PHPUnit_Framework_TestCase
                 ],
             ],
         ]);
+        $this->assertEquals([], $app->db->getSchema()->getTableNames());
         (new migrations\CreateUserTable)->up();
+        $this->assertNull($app->user->getIdentity());
         return $app;
     }
 
-    public function testDb()
+    public function testCreateUser()
     {
         $app = $this->mockApplication();
-        var_dump($app->db->createCommand('SELECT * FROM user')->queryAll());
-        $this->assertEquals('bar', Authenticator::foo());
-        $this->assertNull($app->user->getIdentity());
+        (new models\User('a'))->save();
+        (new models\User('b'))->save();
+        $users = ArrayHelper::map(
+            models\User::find()->all(),
+            function($u) { return $u->getAttributes(); }
+        );
+        $this->assertEquals(2, sizeof($users));
+        $this->assertContains(['id' => 1, 'username' => 'a'], $users);
+        $this->assertContains(['id' => 2, 'username' => 'b'], $users);
     }
 }

+ 5 - 0
tests/models/User.php

@@ -10,6 +10,11 @@ class User extends \yii\db\ActiveRecord
         return 'user';
     }
 
+    public function __construct($username = null)
+    {
+        $this->username = $username;
+    }
+
     public static function findIdentity($id)
     {
         return static::findOne(['id' => $id]);