Skip to content

Commit a8a4b1f

Browse files
committed
Create test for HitCounterService and fix some vars type to text
1 parent 6a4fd73 commit a8a4b1f

File tree

8 files changed

+127
-51
lines changed

8 files changed

+127
-51
lines changed

migrations/m190926_110717_hit_counter__table.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public function safeUp()
2020
$this->createTable('{{%hit_counter}}', [
2121
'id' => $this->primaryKey()->unsigned(),
2222
'counter_id' => $this->string()->notNull(),
23-
'cookie_mark'=> $this->char(32),
23+
'cookie_mark'=> $this->char(32)->null(),
2424
'js_cookei_enabled' => $this->boolean()->notNull()->defaultValue(0),
2525
'js_java_enabled' => $this->boolean()->notNull()->defaultValue(0),
2626
'js_timezone_offset' => $this->integer()->null(),
27-
'js_timezone' => $this->string(),
27+
'js_timezone' => $this->string()->null(),
2828
'js_connection' => $this->string()->null(),
29-
'js_current_url' => $this->string()->notNull(),
30-
'js_referer_url' => $this->string(),
29+
'js_current_url' => $this->text()->null(),
30+
'js_referer_url' => $this->text()->null(),
3131
'js_screen_width' => $this->integer()->null(),
3232
'js_screen_height' => $this->integer()->null(),
3333
'js_color_depth' => $this->integer()->null(),
@@ -36,21 +36,21 @@ public function safeUp()
3636
'js_is_toutch_device' => $this->boolean()->notNull()->defaultValue(0),
3737
'js_processor_ram' => $this->integer()->null(),
3838

39-
'serv_ip' => $this->char(20)->notNull(),
40-
'serv_user_agent' => $this->string(),
41-
'serv_referer_url' => $this->string(),
42-
'serv_server_name' => $this->string(),
39+
'serv_ip' => $this->char(20)->null(),
40+
'serv_user_agent' => $this->text()->null(),
41+
'serv_referer_url' => $this->text()->null(),
42+
'serv_server_name' => $this->string()->null(),
4343
'serv_auth_user_id' => $this->integer()->unsigned()->null(),
4444
'serv_port' => $this->integer()->unsigned()->null(),
4545
'serv_cookies' => 'JSON',
4646

47-
'serv_os' => $this->string(),
48-
'serv_client' => $this->string(),
49-
'serv_device' => $this->string(),
50-
'serv_brand' => $this->string(),
51-
'serv_model' => $this->string(),
52-
'serv_bot' => $this->string(),
53-
'serv_host_by_ip' => $this->string(),
47+
'serv_os' => $this->string()->null(),
48+
'serv_client' => $this->string()->null(),
49+
'serv_device' => $this->string()->null(),
50+
'serv_brand' => $this->string()->null(),
51+
'serv_model' => $this->string()->null(),
52+
'serv_bot' => $this->string()->null(),
53+
'serv_host_by_ip' => $this->string()->null(),
5454
'serv_is_proxy_or_vpn' => $this->boolean()->notNull()->defaultValue(0),
5555

5656
'created_at' => $this->dateTime()->notNull(),

models/HitCounterModel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ class HitCounterModel extends Model{
6060
public function rules()
6161
{
6262
return [
63-
[['counter_id', 'js_current_url', 'serv_ip'], 'required'],
63+
[['counter_id'], 'required'],
6464
[['js_cookei_enabled', 'js_java_enabled', 'js_timezone_offset', 'js_screen_width', 'js_screen_height', 'js_color_depth', 'js_history_length', 'js_is_toutch_device', 'js_processor_ram', 'serv_auth_user_id', 'serv_is_proxy_or_vpn', 'serv_port'], 'integer'],
65-
[['serv_cookies'], 'string'],
66-
[['counter_id', 'js_timezone', 'js_connection','js_current_url', 'js_referer_url', 'js_browser_language', 'serv_user_agent', 'serv_referer_url', 'serv_server_name', 'serv_os', 'serv_client', 'serv_device', 'serv_brand', 'serv_model', 'serv_bot', 'serv_host_by_ip'], 'string', 'max' => 255],
65+
[['serv_cookies','js_current_url', 'js_referer_url','serv_referer_url','serv_user_agent'], 'string'],
66+
[['counter_id', 'js_timezone', 'js_connection', 'js_browser_language', 'serv_server_name', 'serv_os', 'serv_client', 'serv_device', 'serv_brand', 'serv_model', 'serv_bot', 'serv_host_by_ip'], 'string', 'max' => 255],
6767
[['cookie_mark'], 'string', 'max' => 32],
6868
[['serv_ip'], 'string', 'max' => 20],
6969
[['serv_auth_user_id'], 'exist', 'skipOnError' => true, 'targetClass' => Module::getInstance()->userIdentityClass, 'targetAttribute' => ['serv_auth_user_id' => 'id']],
70-
[['cookie_mark', 'js_timezone_offset','js_timezone', 'js_connection','js_referer_url','js_screen_width','js_screen_height','js_color_depth','js_browser_language','js_history_length','js_processor_ram','serv_user_agent','serv_referer_url','serv_server_name','serv_auth_user_id','serv_port','serv_cookies','serv_os','serv_client','serv_device','serv_brand','serv_model','serv_bot','serv_host_by_ip'], 'default', 'value' => null],
70+
[['cookie_mark', 'js_current_url', 'serv_ip', 'js_timezone_offset','js_timezone', 'js_connection','js_referer_url','js_screen_width','js_screen_height','js_color_depth','js_browser_language','js_history_length','js_processor_ram','serv_user_agent','serv_referer_url','serv_server_name','serv_auth_user_id','serv_port','serv_cookies','serv_os','serv_client','serv_device','serv_brand','serv_model','serv_bot','serv_host_by_ip'], 'default', 'value' => null],
7171
];
7272
}
7373

services/HitCounterService.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use coderius\hitCounter\entities\HitCounter;
2020
use yii\helpers\ArrayHelper;
2121
use coderius\hitCounter\dto\HitDTO;
22+
use yii\web\BadRequestHttpException;
2223

2324
class HitCounterService extends Component{
2425

@@ -57,28 +58,33 @@ private function detectJsVisitInfo(Request $request)
5758
$array = $request->get();
5859
$data = [];
5960

60-
$data['counter_id'] = $array['i'];
61-
$data['js_cookei_enabled'] = ArrayHelper::getValue($array, 'c', 0);
62-
$data['js_java_enabled'] = ArrayHelper::getValue($array, 'j', 0);
63-
$data['js_timezone_offset'] = $array['t'];
64-
$data['js_timezone'] = $array['tz'];
65-
$data['js_connection'] = $array['cnt'];
66-
$data['js_current_url'] = $array['u'];
67-
$data['js_referer_url'] = $array['r'];
68-
$data['js_screen_width'] = $array['w'];
69-
$data['js_screen_height'] = $array['h'];
70-
$data['js_color_depth'] = $array['d'];
71-
$data['js_browser_language']= $array['lg'];
72-
$data['js_history_length'] = $array['hl'];
73-
$data['js_is_toutch_device']= ArrayHelper::getValue($array, 'td', 0);
74-
$data['js_processor_ram'] = $array['ram'];
61+
if(isset($array['i'])){
62+
$data['counter_id'] = $array['i'];
63+
$data['js_cookei_enabled'] = ArrayHelper::getValue($array, 'c', 0);
64+
$data['js_java_enabled'] = ArrayHelper::getValue($array, 'j', 0);
65+
$data['js_timezone_offset'] = ArrayHelper::getValue($array, 't');
66+
$data['js_timezone'] = ArrayHelper::getValue($array, 'tz');
67+
$data['js_connection'] = ArrayHelper::getValue($array, 'cnt');
68+
$data['js_current_url'] = ArrayHelper::getValue($array, 'u');
69+
$data['js_referer_url'] = ArrayHelper::getValue($array, 'r');
70+
$data['js_screen_width'] = ArrayHelper::getValue($array, 'w');
71+
$data['js_screen_height'] = ArrayHelper::getValue($array, 'h');
72+
$data['js_color_depth'] = ArrayHelper::getValue($array, 'd');
73+
$data['js_browser_language']= ArrayHelper::getValue($array, 'lg');
74+
$data['js_history_length'] = ArrayHelper::getValue($array, 'hl');
75+
$data['js_is_toutch_device']= ArrayHelper::getValue($array, 'td', 0);
76+
$data['js_processor_ram'] = ArrayHelper::getValue($array, 'ram');
77+
78+
return $data;
79+
}else{
80+
throw new BadRequestHttpException('required get param "i" cannot be empty.');
81+
}
7582

76-
return $data;
7783
}
7884

7985
private function detectServerVisitInfo(Request $request)
8086
{
81-
$request = Yii::$app->request;
87+
// $request = Yii::$app->request;
8288
$data = [];
8389

8490
//Common user data
@@ -110,7 +116,7 @@ private function detectServerVisitInfo(Request $request)
110116

111117
}
112118

113-
public static function pastCookieMark()
119+
private static function pastCookieMark()
114120
{
115121
$request = Yii::$app->request;
116122
$response = Yii::$app->response;
@@ -135,7 +141,7 @@ public static function pastCookieMark()
135141

136142
public static function defaultNameCookieMark()
137143
{
138-
return Inflector::camel2id(StringHelper::basename(get_called_class()));
144+
return Inflector::camel2id(StringHelper::basename(__CLASS__));
139145
}
140146

141147
}

tests/functional/HitCounterWidgetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function setUp(): void
1717

1818
public function testRenderWidget()
1919
{
20-
$expected = file_get_contents(__DIR__ . '/data/test-counter-html.bin');
20+
$expected = file_get_contents(__DIR__ . '/_data/test-counter-html.bin');
2121
$out = \coderius\hitCounter\widgets\hitCounter\HitCounterWidget::widget([]);
2222
$this->assertEqualsWithoutLE($expected, $out);
2323
}

tests/unit/HitCounterEntityTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function setUp(): void
1515
'components' => [
1616
'db' => [
1717
'class' => 'yii\db\Connection',
18-
'dsn' => 'mysql:host=172.27.0.3;port=3306;dbname=test_db',
18+
'dsn' => 'mysql:host=172.27.0.3;port=3306;dbname=coderius',//test_db
1919
'username' => 'root',
2020
'password' => 'root',
2121
'charset' => 'utf8',

tests/unit/HitCounterModelTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public function testValidRequiredTrue()
4343
{
4444
$this->model->load([
4545
'counter_id' => 'w0',
46-
'js_current_url' => 'http://localhost',
47-
'serv_ip' => "172.27.0.1",
48-
'created_at' => "2019-10-05 20:44:32"
46+
// 'js_current_url' => 'http://localhost',
47+
// 'serv_ip' => "172.27.0.1",
48+
// 'created_at' => "2019-10-05 20:44:32"
4949
], '');
5050

5151
$this->assertTrue($this->model->validate());

tests/unit/HitCounterServiceTest.php

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,91 @@
55

66
namespace tests\unit;
77

8-
use coderius\hitCounter\widgets\hitCounter\HitCounterWidget;
8+
use coderius\hitCounter\services\HitCounterService;
9+
use coderius\hitCounter\models\HitCounterModel;
10+
use coderius\hitCounter\entities\HitCounter;
11+
use coderius\hitCounter\Module;
12+
use yii\web\Request;
13+
use yii\di\Container;
14+
use Yii;
915

1016
class HitCounterServiceTest extends \tests\TestCase
1117
{
1218

19+
private $service;
20+
// private $request;
1321

14-
public function test()
22+
protected function setUp(): void
23+
{
24+
parent::setUp();
25+
26+
Yii::configure(\Yii::$app, [
27+
'components' => [
28+
'user' => [
29+
'class' => 'yii\web\User',
30+
'identityClass' => '\tests\unit\user\User',
31+
],
32+
'db' => [
33+
'class' => 'yii\db\Connection',
34+
'dsn' => 'mysql:host=172.27.0.3;port=3306;dbname=coderius',//test_db
35+
'username' => 'root',
36+
'password' => 'root',
37+
'charset' => 'utf8',
38+
],
39+
],
40+
]);
41+
Module::setInstance(new Module('hitCounter'));
42+
43+
// $this->request = $this->createMock(Request::class);
44+
45+
$dd = $this->createMock('\coderius\hitCounter\components\deviceDetect\DeviceDetector');
46+
$hcr = $this->createMock('coderius\hitCounter\repositories\HitCounterRepository');
47+
$hcr->method('save');
48+
49+
$this->service = new HitCounterService($dd, $hcr);
50+
}
51+
52+
public function testDefaultNameCookieMarkExpectation()
1553
{
16-
// Optional: Test anything here, if you want.
17-
$this->assertTrue(true, 'This should already work.');
54+
$cookieNameExpected = 'hit-counter-service';
55+
$cookieNameActual = HitCounterService::defaultNameCookieMark();
1856

19-
// Stop here and mark this test as incomplete.
20-
$this->markTestIncomplete(
21-
'This test has not been implemented yet.'
22-
);
57+
$this->assertEquals($cookieNameExpected, $cookieNameActual);
2358
}
2459

60+
public function testLoadModel()
61+
{
62+
$request = \Yii::$app->request;
63+
$values = ['i' => 'w0'];
64+
$request->setQueryParams($values);
65+
$model = $this->service->loadModel($request);
66+
67+
$this->assertInstanceOf(HitCounterModel::class, $model);
68+
$this->assertEquals($values['i'], $model->counter_id);
69+
70+
}
71+
72+
public function testCreate()
73+
{
74+
$model = new HitCounterModel();
75+
$model->setAttributes(['counter_id' => 'w0']);
76+
$hit = $this->service->create($model);
77+
$this->assertInstanceOf(HitCounter::class, $hit);
78+
$this->assertEquals('w0', $hit->counter_id);
79+
$this->assertNull($hit->js_is_toutch_device);
80+
$this->assertNotNull($hit->created_at);
81+
}
82+
83+
84+
// public function test()
85+
// {
86+
// // Optional: Test anything here, if you want.
87+
// $this->assertTrue(true, 'This should already work.');
88+
89+
// // Stop here and mark this test as incomplete.
90+
// $this->markTestIncomplete(
91+
// 'This test has not been implemented yet.'
92+
// );
93+
// }
94+
2595
}

0 commit comments

Comments
 (0)