Skip to content

Commit e7a4699

Browse files
committedOct 13, 2019
Add tests for widget and module
1 parent 8de2653 commit e7a4699

10 files changed

+233
-22
lines changed
 

‎phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<directory suffix=".php">./src</directory>
2020
<exclude>
2121
<directory suffix=".php">./src/migrations</directory>
22+
<directory suffix=".php">./src/widgets/hitCounter/views</directory>
2223
</exclude>
2324
</whitelist>
2425
</filter>

‎src/Module.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public static function selfInstance()
6868
public function getDefaultModels()
6969
{
7070
return [
71-
'HitCounter' => \coderius\hitCounter\models\HitCounterModel::class,
71+
'HitCounterModel' => \coderius\hitCounter\models\HitCounterModel::class,
7272
];
7373
}
7474

75-
public function model(Model $name)
75+
public function model(string $name)
7676
{
7777
$models = $this->getDefaultModels();
7878
if (array_key_exists($name, $models)) {

‎src/widgets/hitCounter/HitCounterWidget.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class HitCounterWidget extends Widget
3838
*
3939
* @var array
4040
*/
41-
41+
public $counterId;
42+
4243
public $counterOptions = [];
4344

4445
public $linkUrl;
@@ -88,7 +89,10 @@ public function init()
8889
{
8990
parent::init();
9091

91-
92+
if(null === $this->counterId){
93+
$this->counterId = $this->getId();
94+
}
95+
9296
// Set defaults in src img (with params to be transferred to the controller)
9397
$defCOpts = [
9498
'type' => self::COUNTER_VIEW_INVISIBLE,
@@ -144,7 +148,7 @@ protected function makeCounter()
144148

145149
//Render view file wich relevant counter type
146150
$output .= $this->render($type . "-counter.php", [
147-
'counterId' => $this->getId(),
151+
'counterId' => $this->counterId,
148152
'imgSrc' => $this->imgSrc,
149153
'clientImgOptions' => $clientImgOptions, //Style etc.
150154
'counterImgSrcQuery' => $this->counterOptionsToQueryStr()
@@ -153,6 +157,7 @@ protected function makeCounter()
153157
$output .= $this->buildNoScriptHtml();
154158

155159
//If isset wrapper link url, counter code put inside <a></a> tag
160+
//This may be necessary if the counter is visible and clickable so that you can go to the statistics page.
156161
$output = $this->linkUrl ? Html::a($output, $this->linkUrl, $this->clientLinkOptions) : $output;
157162

158163
//Print html comments to counter output

‎tests/TestCase.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,36 @@ protected function mockWebApplication($config = [], $appClass = '\yii\web\Applic
4242
'@npm' => '@vendor/npm',
4343
],
4444
'components' => [
45+
'db' => [
46+
'class' => 'yii\db\Connection',
47+
'dsn' => 'sqlite::memory:',
48+
],
4549
'request' => [
46-
'cookieValidationKey' => 'wefJDF8svfdgergnml,mn23456gvc',
47-
'scriptFile' => __DIR__ . '/index.php',
48-
'scriptUrl' => '/index.php',
50+
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
51+
'hostInfo' => 'http://domain.com',
52+
'scriptUrl' => 'index.php',
53+
],
54+
'user' => [
55+
'class' => 'yii\web\User',
56+
'identityClass' => '\tests\unit\overrides\User',
4957
],
50-
]
58+
'urlManager' => [
59+
'class' => 'yii\web\UrlManager',
60+
'enablePrettyUrl' => true,
61+
'showScriptName' => false,
62+
'enableStrictParsing' => true,
63+
'rules' => [],
64+
],
65+
],
66+
'modules' => [
67+
'hitCounter' => [
68+
'class' => 'coderius\hitCounter\Module',
69+
'userIdentityClass' => '',
70+
71+
],
72+
73+
],
74+
5175
], $config));
5276
}
5377
/**

‎tests/functional/HitCounterWidgetTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace tests\functional;
44

5+
use Yii;
56
use coderius\hitCounter\widgets\hitCounter\HitCounterWidget;
67

78
class HitCounterWidgetTest extends \tests\TestCase
@@ -18,10 +19,23 @@ public function setUp(): void
1819
public function testRenderWidget()
1920
{
2021
$expected = file_get_contents(__DIR__ . '/_data/test-counter-html.bin');
21-
$out = \coderius\hitCounter\widgets\hitCounter\HitCounterWidget::widget([]);
22+
$out = \coderius\hitCounter\widgets\hitCounter\HitCounterWidget::widget([
23+
'counterId' => 'test-counter'
24+
]);
25+
$this->assertEqualsWithoutLE($expected, $out);
26+
}
27+
28+
public function testRenderWidgetAndWrapByLink()
29+
{
30+
$url = 'http://localhost';
31+
$expected = file_get_contents(__DIR__ . '/_data/test-counter-html-wrappedby-link.bin');
32+
33+
$out = \coderius\hitCounter\widgets\hitCounter\HitCounterWidget::widget([
34+
'linkUrl' => $url,
35+
'counterId' => 'test-counter'
36+
]);
2237
$this->assertEqualsWithoutLE($expected, $out);
2338
}
2439

25-
2640

2741
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- Hit counter--><a href="http://localhost" target="_blank" style="position:absolute; left:-9999px;">
2+
<script language="javascript" type="text/javascript">console.time('hitCounter-' + 'test-counter')</script>
3+
4+
<script language="javascript" type="text/javascript"><!--
5+
Cd=document;Cn=navigator;Cn_c=Cn.connection||Cn.mozConnection||Cn.webkitConnection;Cr="&"+Math.random();Cd.cookie="dtct=abc";Cp="&hl="+history.length;Cp+="&t="+(new Date()).getTimezoneOffset();if(Cd.cookie)Cp+="&c=1";if(self!=top)Cp+="&f=1";if(Cn)Cp+="&lg="+Cn.language;if(Cn)Cp+="&cnt="+Cn_c.effectiveType;if(Cn)Cp+="&ram="+Cn.deviceMemory;if('ontouchstart' in window || Cn.msMaxTouchPoints)Cp+="&td=1";if(Intl)Cp+="&tz="+Intl.DateTimeFormat().resolvedOptions().timeZone;//--></script><script language="javascript1.1" type="text/javascript"><!--
6+
if(Cn.javaEnabled())Cp+="&j=1";
7+
//--></script><script language="javascript1.2" type="text/javascript"><!--
8+
if(typeof(screen)!='undefined')Cp+="&w="+screen.width+"&h="+
9+
screen.height+"&d="+(screen.colorDepth?screen.colorDepth:screen.pixelDepth);
10+
//--></script><script language="javascript" type="text/javascript"><!--
11+
Cd.write("<img src='http://domain.com/hitCounter/hit-counter/index?i=test-counter"+Cp+Cr+
12+
"&r="+escape(Cd.referrer)+"&u="+escape(window.location.href)+"&type=invisible&period=1'"+" alt=\"Hit counter\" style=\"width:1px; height:1px\"/>");
13+
//--></script>
14+
15+
<script language="javascript" type="text/javascript">console.timeEnd('hitCounter-' + 'test-counter');</script>
16+
<noscript><img src="http://domain.com/hitCounter/hit-counter/index" alt="Hit counter" style="width:1px; height:1px"></noscript></a><!-- / Hit counter -->
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!-- Hit counter-->
2-
<script language="javascript" type="text/javascript">console.time('hitCounter-' + 'w0')</script>
2+
<script language="javascript" type="text/javascript">console.time('hitCounter-' + 'test-counter')</script>
33

44
<script language="javascript" type="text/javascript"><!--
55
Cd=document;Cn=navigator;Cn_c=Cn.connection||Cn.mozConnection||Cn.webkitConnection;Cr="&"+Math.random();Cd.cookie="dtct=abc";Cp="&hl="+history.length;Cp+="&t="+(new Date()).getTimezoneOffset();if(Cd.cookie)Cp+="&c=1";if(self!=top)Cp+="&f=1";if(Cn)Cp+="&lg="+Cn.language;if(Cn)Cp+="&cnt="+Cn_c.effectiveType;if(Cn)Cp+="&ram="+Cn.deviceMemory;if('ontouchstart' in window || Cn.msMaxTouchPoints)Cp+="&td=1";if(Intl)Cp+="&tz="+Intl.DateTimeFormat().resolvedOptions().timeZone;//--></script><script language="javascript1.1" type="text/javascript"><!--
@@ -8,9 +8,9 @@ if(Cn.javaEnabled())Cp+="&j=1";
88
if(typeof(screen)!='undefined')Cp+="&w="+screen.width+"&h="+
99
screen.height+"&d="+(screen.colorDepth?screen.colorDepth:screen.pixelDepth);
1010
//--></script><script language="javascript" type="text/javascript"><!--
11-
Cd.write("<img src='/index.php?r=hitCounter%2Fhit-counter%2Findex?i=w0"+Cp+Cr+
11+
Cd.write("<img src='http://domain.com/hitCounter/hit-counter/index?i=test-counter"+Cp+Cr+
1212
"&r="+escape(Cd.referrer)+"&u="+escape(window.location.href)+"&type=invisible&period=1'"+" alt=\"Hit counter\" style=\"width:1px; height:1px;position:absolute; left:-9999px;\"/>");
1313
//--></script>
1414

15-
<script language="javascript" type="text/javascript">console.timeEnd('hitCounter-' + 'w0');</script>
16-
<noscript><img src="/index.php?r=hitCounter%2Fhit-counter%2Findex" alt="Hit counter" style="width:1px; height:1px;position:absolute; left:-9999px;"></noscript><!-- / Hit counter -->
15+
<script language="javascript" type="text/javascript">console.timeEnd('hitCounter-' + 'test-counter');</script>
16+
<noscript><img src="http://domain.com/hitCounter/hit-counter/index" alt="Hit counter" style="width:1px; height:1px;position:absolute; left:-9999px;"></noscript><!-- / Hit counter -->

‎tests/unit/HitCounterModuleTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace tests\unit;
4+
5+
use coderius\hitCounter\Module;
6+
use coderius\hitCounter\models\HitCounterModel;
7+
use yii\base\Model;
8+
use Yii;
9+
10+
class HitCounterModuleTest extends \tests\TestCase
11+
{
12+
13+
public function testGetModule()
14+
{
15+
$module = \Yii::$app->getModule('hitCounter');
16+
$this->assertInstanceOf(Module::class, $module);
17+
}
18+
19+
public function testCreateSelfInstanse()
20+
{
21+
$module = Module::selfInstance();
22+
$this->assertInstanceOf(Module::class, $module);
23+
}
24+
25+
public function testGetDefaultModels()
26+
{
27+
$models = Module::selfInstance()->getDefaultModels();
28+
$this->assertNotEmpty($models);
29+
$firstModel = array_shift($models);
30+
$this->assertInstanceOf(Model::class, new $firstModel);
31+
}
32+
33+
public function testGetModel()
34+
{
35+
$module = Module::selfInstance();
36+
$model = $module->model('HitCounterModel');
37+
$this->assertEquals(HitCounterModel::class, $model);
38+
39+
$model = $module->model('HitCounterModelNotReal');
40+
$this->assertFalse($model);
41+
}
42+
43+
// public function testAddUrlManagerRules()
44+
// {
45+
// // $r = ['counter' => 'hit-counter/index'];
46+
// $this->mockWebApplication(['bootstrap' => ['coderius\hitCounter\config\Bootstrap']]);
47+
// Module::selfInstance()->addUrlManagerRules(Yii::$app);
48+
// $rules = \yii\helpers\Url::to(['hit-counter/index']);
49+
// var_dump($rules);
50+
// }
51+
52+
}

‎tests/unit/HitCounterWidgetTest.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,54 @@
33
namespace tests\unit;
44

55
use coderius\hitCounter\widgets\hitCounter\HitCounterWidget;
6+
use coderius\hitCounter\config\Enum;
7+
use yii\base\InvalidParamException;
8+
use Yii;
69

710
class HitCounterWidgetTest extends \tests\TestCase
811
{
912

13+
public function testInitWidgetWithCounterOptions()
14+
{
15+
$config = ['class' => HitCounterWidget::class];
16+
$widget = Yii::createObject($config);
17+
$defaultCounterOptions = [
18+
'type' => HitCounterWidget::COUNTER_VIEW_INVISIBLE,
19+
'period' => Enum::PERIOD_DAY
20+
];
21+
22+
$counterOptions = $widget->counterOptions;
23+
24+
$this->assertNotNull($counterOptions);
25+
26+
sort($defaultCounterOptions);
27+
sort($counterOptions);
1028

11-
public function testSomething()
29+
$this->assertEquals(json_encode($defaultCounterOptions), json_encode($counterOptions));
30+
}
31+
32+
public function testInitWidgetUnknownCounterViewType()
1233
{
13-
// Optional: Test anything here, if you want.
14-
$this->assertTrue(true, 'This should already work.');
34+
$this->expectException(InvalidParamException::class);
35+
$config = [
36+
'class' => HitCounterWidget::class,
37+
'counterOptions' => [
38+
'type' => 'wrong_type'
39+
]
40+
];
41+
$widget = Yii::createObject($config);
42+
}
1543

16-
// Stop here and mark this test as incomplete.
17-
$this->markTestIncomplete(
18-
'This test has not been implemented yet.'
19-
);
44+
public function testInitWidgetUnknownCounterViewPeriod()
45+
{
46+
$this->expectException(InvalidParamException::class);
47+
$config = [
48+
'class' => HitCounterWidget::class,
49+
'counterOptions' => [
50+
'period' => 'wrong_period'
51+
]
52+
];
53+
$widget = Yii::createObject($config);
2054
}
2155

2256
}

‎tests/unit/overrides/User.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace tests\unit\overrides;
4+
5+
use yii\db\ActiveRecord;
6+
use yii\web\IdentityInterface;
7+
/**
8+
* @property int $id
9+
* @property string $username
10+
* @property string $email
11+
*/
12+
class User extends ActiveRecord implements IdentityInterface
13+
{
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public static function tableName()
18+
{
19+
return 'user';
20+
}
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
[['username', 'email'], 'required'],
28+
];
29+
}
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public static function findIdentity($id)
34+
{
35+
return static::findOne($id);
36+
}
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public static function findIdentityByAccessToken($token, $type = null)
41+
{
42+
// TODO: Implement findIdentityByAccessToken() method.
43+
}
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function getId()
48+
{
49+
return $this->getPrimaryKey();
50+
}
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function getAuthKey()
55+
{
56+
// TODO: Implement getAuthKey() method.
57+
}
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function validateAuthKey($authKey)
62+
{
63+
// TODO: Implement validateAuthKey() method.
64+
}
65+
}

0 commit comments

Comments
 (0)
Please sign in to comment.