Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc0a4212

Browse files
dynasourcecebe
authored andcommitted
suite for unit tests added
DI object creation installedclose#8
1 parent5194029 commitc0a4212

File tree

12 files changed

+473
-4
lines changed

12 files changed

+473
-4
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/config.local.php
33
/tmp
44
/logs
5+

‎commands/GithubController.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
*
4-
*
3+
*
4+
*
55
* @author Carsten Brandt <mail@cebe.cc>
66
*/
77

@@ -14,6 +14,7 @@
1414
useyii\console\Controller;
1515
useyii\helpers\Console;
1616

17+
1718
class GithubControllerextends Controller
1819
{
1920
publicfunctioninit()
@@ -54,7 +55,7 @@ public function actionRegister(array $limitRepos = [])
5455
list($user,$repo) =explode('/',$urepo);
5556

5657
// https://developer.github.com/v3/repos/hooks/#create-a-hook
57-
$api =newRepo($client);
58+
$api =Yii::createObject(\Github\Api\Repo::class, [$client]);
5859

5960
// check if hook exists
6061
$hookId =null;

‎components/Github.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Github extends Component
2222
publicfunctionclient()
2323
{
2424
// create client
25-
$client =new \Github\HttpClient\CachedHttpClient();
25+
$client =Yii::createObject('Github\HttpClient\CachedHttpClient');
2626
$client->setCache(new \Github\HttpClient\Cache\FilesystemCache(__DIR__ .'/../tmp/github-cache'));
2727
$client =new \Github\Client($client);
2828

‎phpunit.xml.dist‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunitbootstrap="./tests/bootstrap.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false">
8+
<testsuites>
9+
<testsuitename="Test Suite">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

‎tests/GithubControllerTest.php‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespaceyiiunit\extensions\githubbot;
4+
5+
useapp\commands\GithubController;
6+
useYii;
7+
useyiiunit\extensions\githubbot\mocks\CachedHttpClientMock;
8+
useyiiunit\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+
publicfunctiontestInit_HookSecretException()
18+
{
19+
$this->setExpectedException('yii\base\Exception','Config param "hook_secret" is not configured!');
20+
newGithubController('github', Yii::$app);
21+
}
22+
23+
publicfunctiontestHooks_UndefinedWebUrl()
24+
{
25+
$this->mockApplication([
26+
'params' => [
27+
'hook_secret' =>'test-secret',
28+
],
29+
]);
30+
$controller =newGithubController('github', Yii::$app);
31+
32+
$this->setExpectedException('PHPUnit_Framework_Error_Notice','Undefined index: webUrl');
33+
$controller->hooks();
34+
}
35+
36+
publicfunctiontestHooks()
37+
{
38+
$this->mockApplication([
39+
'params' => [
40+
'hook_secret' =>'test-secret',
41+
'webUrl' =>'http://localhost'
42+
],
43+
]);
44+
$controller =newGithubController('github', Yii::$app);
45+
$this->assertEquals([
46+
'issues' =>'http://localhost/index.php?r=issues'
47+
],$controller->hooks());
48+
}
49+
50+
publicfunctiontestActionRegister_RequiredGithubComponentException()
51+
{
52+
$this->mockApplication([
53+
'params' => [
54+
'hook_secret' =>'test-secret'
55+
],
56+
]);
57+
$controller =newGithubController('github', Yii::$app);
58+
$this->setExpectedException('\yii\base\UnknownPropertyException','Getting unknown property: yii\console\Application::github');
59+
$controller->runAction('register');
60+
}
61+
62+
publicfunctiontestActionRegister_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 =newGithubController('github', Yii::$app);
75+
$this->setExpectedException('PHPUnit_Framework_Error_Notice','Undefined index: repositories');
76+
$controller->runAction('register');
77+
}
78+
79+
publicfunctiontestActionRegister_WrongTokenException()
80+
{
81+
Yii::$container->set('Github\Api\Repo',function ($container,$params,$config) {
82+
returnnew \yiiunit\extensions\githubbot\mocks\RepoMock($params[0]);
83+
});
84+
Yii::$container->set('Github\HttpClient\CachedHttpClient',function () {
85+
returnnewCachedHttpClientMock();
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 =newGithubControllerMock('github', Yii::$app);
103+
$this->setExpectedException('Github\Exception\RuntimeException','Bad credentials',401);
104+
$controller->runAction('register');
105+
}
106+
107+
publicfunctiontestActionRegister()
108+
{
109+
Yii::$container->set('Github\Api\Repo',function ($container,$params) {
110+
returnnew \yiiunit\extensions\githubbot\mocks\RepoMock($params[0]);
111+
});
112+
Yii::$container->set('Github\HttpClient\CachedHttpClient',function () {
113+
returnnewCachedHttpClientMock();
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 =newGithubControllerMock('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+
}

‎tests/GithubTest.php‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespaceyiiunit\extensions\githubbot;
4+
5+
useapp\components\Github;
6+
useYii;
7+
8+
/**
9+
* Class GithubTest
10+
* @package yiiunit\extensions\githubbot
11+
*/
12+
class GithubTestextends TestCase
13+
{
14+
publicfunctiontestClient_RequiredTokenException()
15+
{
16+
$this->setExpectedException('\Exception','Config param "github_token" is not configured!');
17+
(newGithub())->client();
18+
}
19+
20+
publicfunctiontestClient_RequiredUserException()
21+
{
22+
$this->mockApplication([
23+
'params' => [
24+
'github_token' =>'test-token'
25+
]
26+
]);
27+
$this->setExpectedException('\Exception','Config param "github_username" is not configured!');
28+
(newGithub())->client();
29+
}
30+
31+
publicfunctiontestClient_Authentication()
32+
{
33+
$this->mockApplication([
34+
'params' => [
35+
'github_token' =>'test-token',
36+
'github_username' =>'test-username',
37+
]
38+
]);
39+
$client = (newGithub())->client();
40+
$this->assertInstanceOf('Github\Client',$client);
41+
$this->assertInstanceOf('Github\HttpClient\HttpClient',$this->getInvisibleProperty('httpClient',$client));
42+
}
43+
44+
publicfunctiontestVerifyRequest_RequiredHookSecretException()
45+
{
46+
$this->setExpectedException('\yii\base\Exception','Config param "hook_secret" is not configured!');
47+
(newGithub())->verifyRequest('');
48+
}
49+
50+
publicfunctiontestVerifyRequest_MissingSignatureException()
51+
{
52+
$this->mockWebApplication([
53+
'params' => [
54+
'hook_secret' =>'test-secret',
55+
],
56+
]);
57+
$this->setExpectedException('\yii\web\BadRequestHttpException','X-Hub-Signature header is missing.');
58+
(newGithub())->verifyRequest('');
59+
}
60+
61+
publicfunctiontestVerifyRequest_UnknownSignatureException()
62+
{
63+
$this->mockWebApplication([
64+
'params' => [
65+
'hook_secret' =>'test-secret',
66+
],
67+
]);
68+
Yii::$app->request->getHeaders()->set('X-Hub-Signature',"unknown=dummy");
69+
$this->setExpectedException('\yii\web\BadRequestHttpException','Unknown algorithm in X-Hub-Signature header.');
70+
(newGithub())->verifyRequest('');
71+
}
72+
73+
/**
74+
* @dataProvider signatureDataProvider
75+
*/
76+
publicfunctiontestVerifyRequest_ValidationException($signature)
77+
{
78+
$this->mockWebApplication([
79+
'params' => [
80+
'hook_secret' =>'test-secret',
81+
],
82+
]);
83+
Yii::$app->request->getHeaders()->set('X-Hub-Signature',"$signature=hash");
84+
$this->setExpectedException('\yii\web\BadRequestHttpException','Unable to validate submitted data.');
85+
(newGithub())->verifyRequest('');
86+
}
87+
88+
publicfunctionsignatureDataProvider()
89+
{
90+
return [['sha1'], ['sha256'], ['sha384'], ['sha512']];
91+
}
92+
93+
}

‎tests/TestCase.php‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespaceyiiunit\extensions\githubbot;
4+
5+
useYii;
6+
useyii\di\Container;
7+
useyii\helpers\ArrayHelper;
8+
9+
/**
10+
* This is the base class for all yii framework unit tests.
11+
*/
12+
abstractclass TestCaseextends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* Clean up after test.
16+
* By default the application created with [[mockApplication]] will be destroyed.
17+
*/
18+
protectedfunctiontearDown()
19+
{
20+
parent::tearDown();
21+
$this->destroyApplication();
22+
}
23+
24+
/**
25+
* Destroys application in Yii::$app by setting it to null.
26+
*/
27+
protectedfunctiondestroyApplication()
28+
{
29+
Yii::$app =null;
30+
Yii::$container =newContainer();
31+
}
32+
33+
/**
34+
* Populates Yii::$app with a new application
35+
* The application will be destroyed on tearDown() automatically.
36+
* @param array $config The application configuration, if needed
37+
* @param string $appClass name of the application class to create
38+
*/
39+
protectedfunctionmockApplication($config = [],$appClass ='\yii\console\Application')
40+
{
41+
returnnew$appClass(ArrayHelper::merge([
42+
'id' =>'testapp',
43+
'basePath' =>dirname(__DIR__),
44+
'vendorPath' =>dirname(__DIR__) .'/vendor',
45+
],$config));
46+
}
47+
48+
protectedfunctionmockWebApplication($config = [],$appClass ='\yii\web\Application')
49+
{
50+
returnnew$appClass(ArrayHelper::merge([
51+
'id' =>'testapp',
52+
'basePath' =>dirname(__DIR__),
53+
'vendorPath' =>dirname(__DIR__) .'/vendor',
54+
'components' => [
55+
'request' => [
56+
'cookieValidationKey' =>'wefJDF8sfdsfSDefwqdxj9oq',
57+
'scriptFile' =>__DIR__ .'/index.php',
58+
'scriptUrl' =>'/index.php',
59+
],
60+
]
61+
],$config));
62+
}
63+
64+
/**
65+
* Returns invisible property
66+
* @param string $property
67+
* @param string|object $mixed
68+
* @return mixed
69+
*/
70+
protectedfunctiongetInvisibleProperty($propertyName,$mixed)
71+
{
72+
$class =new \ReflectionClass($mixed);
73+
$property =$class->getProperty($propertyName);
74+
$property->setAccessible(true);
75+
$value =$property->getValue($mixed);
76+
$property->setAccessible(false);
77+
return$value;
78+
}
79+
}

‎tests/bootstrap.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
// ensure we get report on all possible php errors
4+
error_reporting(-1);
5+
6+
define('YII_ENABLE_ERROR_HANDLER',false);
7+
define('YII_ENV_TEST',true);
8+
9+
$_SERVER['SCRIPT_NAME'] ='/' .__DIR__;
10+
$_SERVER['SCRIPT_FILENAME'] =__FILE__;
11+
12+
$vendorRoot =__DIR__ .'/../vendor';
13+
require_once($vendorRoot .'/autoload.php');
14+
require_once($vendorRoot .'/yiisoft/yii2/Yii.php');
15+
16+
Yii::setAlias('@yiiunit/extensions/githubbot',__DIR__);
17+
Yii::setAlias('@yiiunit',__DIR__ .'/../tests');
18+
Yii::setAlias('@app',dirname(__DIR__));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp