Browse Source

tests; LuBusiness getResourceName

develop
Robin Thoni 8 years ago
parent
commit
b8f3b0c404
2 changed files with 102 additions and 7 deletions
  1. 36
    7
      src/Utils/Business/LuBusiness.php
  2. 66
    0
      tests/LuBusinessTest.php

+ 36
- 7
src/Utils/Business/LuBusiness.php View File

@@ -6,7 +6,18 @@ use Luticate\Utils\DataAccess\LuDataAccess;
6 6
 use Luticate\Utils\Dbo\LuDbo;
7 7
 
8 8
 abstract class LuBusiness {
9
+    protected static $_resourceName = null;
10
+    protected static $_resourceNameUnPluralize = true;
9 11
 
12
+    /**
13
+     * @param string $reason
14
+     * @throws LuBusinessException
15
+     */
16
+    public static function badInput($reason = 'Invalid user input')
17
+    {
18
+        throw new LuBusinessException($reason, 400);
19
+    }
20
+    
10 21
     /**
11 22
      * @param string $reason
12 23
      * @throws LuBusinessException
@@ -20,18 +31,18 @@ abstract class LuBusiness {
20 31
      * @param string $reason
21 32
      * @throws LuBusinessException
22 33
      */
23
-    public static function notFound($reason = 'Resource not found')
34
+    public static function forbidden($reason = 'Forbidden')
24 35
     {
25
-        throw new LuBusinessException($reason, 404);
36
+        throw new LuBusinessException($reason, 403);
26 37
     }
27 38
 
28 39
     /**
29 40
      * @param string $reason
30 41
      * @throws LuBusinessException
31 42
      */
32
-    public static function badInput($reason = 'Invalid user input')
43
+    public static function notFound($reason = 'Resource not found')
33 44
     {
34
-        throw new LuBusinessException($reason, 400);
45
+        throw new LuBusinessException($reason, 404);
35 46
     }
36 47
 
37 48
     /**
@@ -79,8 +90,26 @@ abstract class LuBusiness {
79 90
 
80 91
     public static function getResourceName()
81 92
     {
82
-        $match = [];
83
-        preg_match('/([^\\\\]*)Business$/', get_called_class(), $match);
84
-        return $match[1];
93
+        $res = static::$_resourceName;
94
+        if ($res == null) {
95
+            $match = [];
96
+            $className = get_called_class();
97
+            preg_match('/([^\\\\]*)Business$/', get_called_class(), $match);
98
+            if (count($match) < 2) {
99
+                $res = $className;
100
+            }
101
+            else {
102
+                $res = $match[1];
103
+            }
104
+            if (static::$_resourceNameUnPluralize) {
105
+                if (LuStringUtils::endsWith($res, "ies")) {
106
+                    $res = substr($res, 0, strlen($res) - 3) . "y";
107
+                }
108
+                else if (LuStringUtils::endsWith($res, "s")) {
109
+                    $res = substr($res, 0, strlen($res) - 1);
110
+                }
111
+            }
112
+        }
113
+        return $res;
85 114
     }
86 115
 }

+ 66
- 0
tests/LuBusinessTest.php View File

@@ -0,0 +1,66 @@
1
+<?php
2
+use Luticate\Utils\Business\LuBusiness;
3
+
4
+/**
5
+ * Created by PhpStorm.
6
+ * User: robin
7
+ * Date: 5/29/16
8
+ * Time: 2:57 PM
9
+ */
10
+
11
+class MyEntitiesBusiness extends LuBusiness {
12
+
13
+}
14
+
15
+class My2EntitiesBusiness extends LuBusiness {
16
+    protected static $_resourceNameUnPluralize = false;
17
+}
18
+
19
+class My3EntitiesBusiness extends LuBusiness {
20
+    protected static $_resourceName = "MyEntity";
21
+}
22
+
23
+class MyUsersBusiness extends LuBusiness {
24
+    protected static $_resourceNameUnPluralize = false;
25
+}
26
+
27
+class MyBusinessDontRespectTheRules extends LuBusiness {
28
+
29
+}
30
+
31
+class My2BusinessDontRespectTheRules extends LuBusiness {
32
+    protected static $_resourceNameUnPluralize = false;
33
+}
34
+
35
+class LuBusinessTest extends \PHPUnit_Framework_TestCase
36
+{
37
+    public function testResourceNameMyEntities()
38
+    {
39
+        $this->assertSame("MyEntity", MyEntitiesBusiness::getResourceName());
40
+    }
41
+
42
+    public function testResourceNameMy2Entities()
43
+    {
44
+        $this->assertSame("My2Entities", My2EntitiesBusiness::getResourceName());
45
+    }
46
+
47
+    public function testResourceNameMy3Entities()
48
+    {
49
+        $this->assertSame("MyEntity", My3EntitiesBusiness::getResourceName());
50
+    }
51
+
52
+    public function testResourceNameMyUsersBusiness()
53
+    {
54
+        $this->assertSame("MyUsers", MyUsersBusiness::getResourceName());
55
+    }
56
+
57
+    public function testResourceNameMyBusinessDontRespectTheRules()
58
+    {
59
+        $this->assertSame("MyBusinessDontRespectTheRule", MyBusinessDontRespectTheRules::getResourceName());
60
+    }
61
+
62
+    public function testResourceNameMy2BusinessDontRespectTheRules()
63
+    {
64
+        $this->assertSame("My2BusinessDontRespectTheRules", My2BusinessDontRespectTheRules::getResourceName());
65
+    }
66
+}

Loading…
Cancel
Save