Просмотр исходного кода

tests; added database aliases

develop
Robin Thoni 7 лет назад
Родитель
Сommit
bad1b44907
2 измененных файлов: 98 добавлений и 0 удалений
  1. 26
    0
      src/Utils/Controller/LuticateApplication.php
  2. 72
    0
      tests/LuApplicationTest.php

+ 26
- 0
src/Utils/Controller/LuticateApplication.php Просмотреть файл

@@ -31,6 +31,29 @@ class LuticateApplication implements MessageComponentInterface
31 31
         return self::$_app;
32 32
     }
33 33
 
34
+    public static function getDatabase($name, $databases)
35
+    {
36
+        foreach ($databases as $database) {
37
+            if (is_array($database) && $database["name"] == $name) {
38
+                return $database;
39
+            }
40
+        }
41
+        return null;
42
+    }
43
+
44
+    public static function resolveDatabases($databases) {
45
+        $dbs = [];
46
+        foreach ($databases as $database) {
47
+            if (is_string($database)) {
48
+                $database = static::getDatabase($database, $databases);
49
+            }
50
+            if (is_array($database)) {
51
+                $dbs[] = $database;
52
+            }
53
+        }
54
+        return $dbs;
55
+    }
56
+
34 57
     /**
35 58
      * @param $configuration array
36 59
      */
@@ -40,6 +63,9 @@ class LuticateApplication implements MessageComponentInterface
40 63
             error_reporting(0);
41 64
         }
42 65
         $this->_config = $configuration;
66
+
67
+        $this->_config["databases"] = static::resolveDatabases($this->_config["databases"]);
68
+
43 69
         $this->_clients = new \SplObjectStorage;
44 70
         $this->_router = new LuRoute();
45 71
         LuLog::setLogFilePath(__DIR__ . "/../../../../../../". $this->_config["logs"]);

+ 72
- 0
tests/LuApplicationTest.php Просмотреть файл

@@ -0,0 +1,72 @@
1
+<?php
2
+use Luticate\Utils\Controller\LuticateApplication;
3
+use Luticate\Utils\Dbo\LuDboDeserializeException;
4
+use Luticate\Utils\Dbo\LuBoolDboArray;
5
+
6
+/**
7
+ * Created by PhpStorm.
8
+ * User: robin
9
+ * Date: 5/29/16
10
+ * Time: 2:57 PM
11
+ */
12
+
13
+class LuApplicationTest extends \PHPUnit_Framework_TestCase
14
+{
15
+    public function testGetDatabases1()
16
+    {
17
+        $db1 = ["name" => "mydb", "other_info" => 42];
18
+        $dbs = [$db1];
19
+        $this->assertSame($db1, LuticateApplication::getDatabase($db1["name"], $dbs));
20
+    }
21
+    
22
+    public function testGetDatabases2()
23
+    {
24
+        $db1 = ["name" => "mydb", "other_info" => 42];
25
+        $dbs = [$db1];
26
+        $this->assertNull(LuticateApplication::getDatabase("db", $dbs));
27
+    }
28
+
29
+    public function testGetDatabases3()
30
+    {
31
+        $db1 = ["name" => "mydb", "other_info" => 42];
32
+        $db2 = ["name" => "myotherdb", "other_info" => 42];
33
+        $dbs = [$db1, $db2];
34
+        $this->assertSame($db2, LuticateApplication::getDatabase($db2["name"], $dbs));
35
+    }
36
+
37
+    public function testGetDatabases4()
38
+    {
39
+        $db1 = ["name" => "mydb", "other_info" => 42];
40
+        $db2 = ["name" => "myotherdb", "other_info" => 42];
41
+        $dbs = [$db1, $db2];
42
+        $this->assertNull(LuticateApplication::getDatabase("anotherdb", $dbs));
43
+    }
44
+
45
+    public function testResolveDatabases1()
46
+    {
47
+        $db1 = ["name" => "mydb", "other_info" => 42];
48
+        $dbs = [$db1, "mydb"];
49
+        $this->assertSame([$db1, $db1], LuticateApplication::resolveDatabases($dbs));
50
+    }
51
+
52
+    public function testResolveDatabases2()
53
+    {
54
+        $db1 = ["name" => "mydb", "other_info" => 42];
55
+        $dbs = [$db1, "mydb2"];
56
+        $this->assertSame([$db1], LuticateApplication::resolveDatabases($dbs));
57
+    }
58
+
59
+    public function testResolveDatabases3()
60
+    {
61
+        $db1 = ["name" => "mydb", "other_info" => 42];
62
+        $dbs = ["mydb2", $db1];
63
+        $this->assertSame([$db1], LuticateApplication::resolveDatabases($dbs));
64
+    }
65
+
66
+    public function testResolveDatabases4()
67
+    {
68
+        $db1 = ["name" => "mydb", "other_info" => 42];
69
+        $dbs = ["mydb", $db1];
70
+        $this->assertSame([$db1, $db1], LuticateApplication::resolveDatabases($dbs));
71
+    }
72
+}

Загрузка…
Отмена
Сохранить