colly_wyx
2018-05-18 50a53f7db63c5729b0ddbc93367117cf35ccf03c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
 * PhpUnderControl_PhalApi_Test
 *
 * 针对 ../PhalApi.php PhalApi 类的PHPUnit单元测试
 *
 * @author: dogstar 20150209
 */
 
require_once dirname(__FILE__) . '/test_env.php';
 
if (!class_exists('PhalApi')) {
    require dirname(__FILE__) . '/../PhalApi.php';
}
 
class PhpUnderControl_PhalApi_Test extends PHPUnit_Framework_TestCase
{
    public $phalApi;
 
    protected function setUp()
    {
        parent::setUp();
 
        $data = array(
            'service' => 'AnotherImpl.doSth',
        );
 
        DI()->request = new PhalApi_Request($data);
 
        $this->phalApi = new PhalApi();
    }
 
    protected function tearDown()
    {
        DI()->response = 'PhalApi_Response_Json';
    }
 
    /**
     * @group testResponse
     */ 
    public function testResponseWithJsonMock()
    {
        DI()->response = 'PhalApi_Response_Json_Mock';
 
        $rs = $this->phalApi->response();
 
        $rs->output();
 
        $this->expectOutputRegex('/"ret":200/');
    }
 
    /**
     * @group testResponse
     */ 
    public function testResponseWithJsonPMock()
    {
        DI()->response = new PhalApi_Response_JsonP_Mock('test');
 
        $rs = $this->phalApi->response();
 
        $rs->output();
 
        $this->expectOutputRegex('/"ret":200/');
    }
 
    /**
     * @group testResponse
     */ 
    public function testResponseWithExplorer()
    {
        DI()->response = 'PhalApi_Response_Explorer';
 
        $rs = $this->phalApi->response();
 
        $rs->output();
        $res = $rs->getResult();
 
        $this->assertEquals(200, $res['ret']);
    }
 
    public function testResponseWithBadRequest() {
        $data = array(
            'service' => 'AnotherImpl',
        );
 
        DI()->request = new PhalApi_Request($data);
        DI()->response = 'PhalApi_Response_Json_Mock';
 
        $phalApi = new PhalApi();
 
        $rs = $phalApi->response();
 
        $rs->output();
 
        $this->expectOutputRegex('/"ret":400/');
    }
 
    /**
     * @expectedException Exception
     * @expectedExceptionMessage trouble
     */
    public function testResponseWithException() {
        $data = array(
            'service' => 'AnotherImpl.MakeSomeTrouble',
        );
 
        DI()->request = new PhalApi_Request($data);
 
        $rs = $this->phalApi->response();
    }
}
 
class Api_AnotherImpl extends PhalApi_Api {
 
    public function doSth() {
        return 'hello wolrd!';
    }
 
    public function makeSomeTrouble() {
        throw new Exception('as u can see, i mean to make some trouble');
    }
}