1+ <?php
2+
3+ namespace yiiunit \extensions \githubbot ;
4+
5+ use app \commands \GithubController ;
6+ use Yii ;
7+ use yiiunit \extensions \githubbot \mocks \CachedHttpClientMock ;
8+ use yiiunit \extensions \githubbot \mocks \GithubControllerMock ;
9+
10+ /**
11+ * Class GithubControllerTest
12+ * @package yiiunit\extensions\githubbot
13+ * @author Boudewijn Vahrmeijer <info@dynasource.eu>
14+ */
15+ class GithubControllerTestextends TestCase
16+ {
17+ public function testInit_HookSecretException ()
18+ {
19+ $ this ->setExpectedException ('yii\base\Exception ' ,'Config param "hook_secret" is not configured! ' );
20+ new GithubController ('github ' , Yii::$ app );
21+ }
22+
23+ public function testHooks_UndefinedWebUrl ()
24+ {
25+ $ this ->mockApplication ([
26+ 'params ' => [
27+ 'hook_secret ' =>'test-secret ' ,
28+ ],
29+ ]);
30+ $ controller =new GithubController ('github ' , Yii::$ app );
31+
32+ $ this ->setExpectedException ('PHPUnit_Framework_Error_Notice ' ,'Undefined index: webUrl ' );
33+ $ controller ->hooks ();
34+ }
35+
36+ public function testHooks ()
37+ {
38+ $ this ->mockApplication ([
39+ 'params ' => [
40+ 'hook_secret ' =>'test-secret ' ,
41+ 'webUrl ' =>'http://localhost '
42+ ],
43+ ]);
44+ $ controller =new GithubController ('github ' , Yii::$ app );
45+ $ this ->assertEquals ([
46+ 'issues ' =>'http://localhost/index.php?r=issues '
47+ ],$ controller ->hooks ());
48+ }
49+
50+ public function testActionRegister_RequiredGithubComponentException ()
51+ {
52+ $ this ->mockApplication ([
53+ 'params ' => [
54+ 'hook_secret ' =>'test-secret '
55+ ],
56+ ]);
57+ $ controller =new GithubController ('github ' , Yii::$ app );
58+ $ this ->setExpectedException ('\yii\base\UnknownPropertyException ' ,'Getting unknown property: yii\console\Application::github ' );
59+ $ controller ->runAction ('register ' );
60+ }
61+
62+ public function testActionRegister_RequiredRepositoriesParamException ()
63+ {
64+ $ this ->mockApplication ([
65+ 'components ' => [
66+ 'github ' =>'app\components\Github ' ,
67+ ],
68+ 'params ' => [
69+ 'github_token ' =>'test-token ' ,
70+ 'github_username ' =>'test-username ' ,
71+ 'hook_secret ' =>'test-secret '
72+ ],
73+ ]);
74+ $ controller =new GithubController ('github ' , Yii::$ app );
75+ $ this ->setExpectedException ('PHPUnit_Framework_Error_Notice ' ,'Undefined index: repositories ' );
76+ $ controller ->runAction ('register ' );
77+ }
78+
79+ public function testActionRegister_WrongTokenException ()
80+ {
81+ Yii::$ container ->set ('Github\Api\Repo ' ,function ($ container ,$ params ,$ config ) {
82+ return new \yiiunit \extensions \githubbot \mocks \RepoMock ($ params [0 ]);
83+ });
84+ Yii::$ container ->set ('Github\HttpClient\CachedHttpClient ' ,function () {
85+ return new CachedHttpClientMock ();
86+ });
87+
88+ $ this ->mockApplication ([
89+ 'components ' => [
90+ 'github ' =>'app\components\Github ' ,
91+ ],
92+ 'params ' => [
93+ 'github_token ' =>'wrong ' . CachedHttpClientMock::DUMMY_TOKEN ,
94+ 'github_username ' =>'username-test ' ,
95+ 'repositories ' => [
96+ 'dummy-test/hook-test '
97+ ],
98+ 'hook_secret ' =>'test-secret ' ,
99+ 'webUrl ' =>'http://www.domain.com/hookUrl '
100+ ],
101+ ]);
102+ $ controller =new GithubControllerMock ('github ' , Yii::$ app );
103+ $ this ->setExpectedException ('Github\Exception\RuntimeException ' ,'Bad credentials ' ,401 );
104+ $ controller ->runAction ('register ' );
105+ }
106+
107+ public function testActionRegister ()
108+ {
109+ Yii::$ container ->set ('Github\Api\Repo ' ,function ($ container ,$ params ) {
110+ return new \yiiunit \extensions \githubbot \mocks \RepoMock ($ params [0 ]);
111+ });
112+ Yii::$ container ->set ('Github\HttpClient\CachedHttpClient ' ,function () {
113+ return new CachedHttpClientMock ();
114+ });
115+
116+ $ config = [
117+ 'components ' => [
118+ 'github ' =>'app\components\Github ' ,
119+ ],
120+ 'params ' => [
121+ 'github_token ' => CachedHttpClientMock::DUMMY_TOKEN ,
122+ 'github_username ' =>'username-test ' ,
123+ 'repositories ' => [
124+ 'dummy-test/hook-test '
125+ ],
126+ 'hook_secret ' =>'test-secret ' ,
127+ 'webUrl ' =>'http://www.domain.com/hookUrl '
128+ ],
129+ ];
130+ $ this ->mockApplication ($ config );
131+ $ controller =new GithubControllerMock ('github ' , Yii::$ app );
132+ $ controller ->runAction ('register ' );
133+ $ actual =$ controller ->flushStdOutBuffer ();
134+ $ this ->assertEquals ("registering issues hook on " .$ config ['params ' ]['repositories ' ][0 ] ."...added. \n" ,$ actual );
135+ }
136+ }