Skip to content

Commit 0caaed3

Browse files
author
simialbi
committed
added test mode in connection
added tests
1 parent 6c18fd1 commit 0caaed3

File tree

8 files changed

+270
-5
lines changed

8 files changed

+270
-5
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
insert_final_newline = true
9+
indent_style = space
10+
indent_size = 4
11+
trim_trailing_whitespace = true
12+
13+
[*.md]
14+
trim_trailing_whitespace = false

.travis.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.0
6+
- 7.1
7+
- 7.2
8+
- 7.3
9+
10+
sudo: false
11+
12+
# cache vendor dirs
13+
cache:
14+
directories:
15+
- $HOME/.composer/cache
16+
17+
install:
18+
- travis_retry composer self-update && composer --version
19+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
20+
- travis_retry composer install --prefer-dist --no-interaction
21+
22+
before_script:
23+
- |
24+
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
25+
PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
26+
fi
27+
script:
28+
- ./vendor/bin/phpunit --verbose $PHPUNIT_FLAGS
29+
30+
after_script:
31+
- |
32+
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
33+
travis_retry wget https://scrutinizer-ci.com/ocular.phar
34+
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
35+
fi

src/Connection.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class Connection extends Component
6161
* @var boolean Whether to user pluralisation or not
6262
*/
6363
public $usePluralisation = true;
64+
/**
65+
* @var boolean Whether we are in test mode or not (prevent execution)
66+
*/
67+
public $isTestMode = false;
68+
6469
/**
6570
* @var string|Closure authorization config
6671
*/
@@ -265,7 +270,7 @@ public function getHandler()
265270
* @param string|array $url the URL for request, not including proto and site
266271
* @param array $data the request data
267272
*
268-
* @return Response|false
273+
* @return mixed|false
269274
* @throws \yii\httpclient\Exception
270275
*/
271276
protected function request($method, $url, $data = [])
@@ -280,14 +285,19 @@ protected function request($method, $url, $data = [])
280285

281286
Yii::beginProfile($profile, __METHOD__);
282287
/* @var $request \yii\httpclient\Request */
288+
289+
Yii::debug($this->handler->baseUrl . '/' . $url, __METHOD__ . '-url');
290+
Yii::debug($data, __METHOD__ . '-data');
291+
Yii::debug($headers, __METHOD__ . '-headers');
292+
283293
$request = call_user_func([$this->handler, $method], $url, $data, $headers);
284-
$this->_response = $request->send();
294+
$this->_response = $this->isTestMode ? [] : $request->send();
285295
Yii::endProfile($profile, __METHOD__);
286296

287-
if (!$this->_response->isOk) {
297+
if (!$this->isTestMode && !$this->_response->isOk) {
288298
return false;
289299
}
290300

291-
return $this->_response->data;
301+
return $this->isTestMode ? [] : $this->_response->data;
292302
}
293303
}

tests/TestCase.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
class TestCase extends \PHPUnit\Framework\TestCase
1414
{
15+
private $_index = 0;
16+
1517
/**
1618
* {@inheritdoc}
1719
*/
@@ -44,7 +46,17 @@ protected function mockWebApplication($config = [], $appClass = '\yii\web\Applic
4446
],
4547
'rest' => [
4648
'class' => 'simialbi\yii2\rest\Connection',
47-
'baseUrl' => 'https:s//api.site.com/'
49+
'baseUrl' => 'https://api.site.com/',
50+
'isTestMode' => true
51+
],
52+
'log' => [
53+
'traceLevel' => 3,
54+
'targets' => [
55+
[
56+
'class' => 'yiiunit\extensions\rest\log\ArrayTarget'
57+
]
58+
],
59+
'flushInterval' => 0
4860
]
4961
]
5062
], $config));
@@ -58,4 +70,38 @@ protected function destroyApplication()
5870
Yii::$app = null;
5971
Yii::$container = new Container();
6072
}
73+
74+
/**
75+
* Parse log from index and returns data
76+
* @return array
77+
*/
78+
protected function parseLogs()
79+
{
80+
$url = '';
81+
$data = [];
82+
$headers = [];
83+
84+
$profile = false;
85+
for (; $this->_index <= count(Yii::$app->log->logger->messages); $this->_index++) {
86+
$message = Yii::$app->log->logger->messages[$this->_index];
87+
if ($message[2] === 'simialbi\yii2\rest\Connection::request-url') {
88+
$url = $message[0];
89+
} elseif ($message[2] === 'simialbi\yii2\rest\Connection::request-data') {
90+
$data = $message[0];
91+
} elseif ($message[2] === 'simialbi\yii2\rest\Connection::request-headers') {
92+
$data = $message[0];
93+
} elseif ($message[2] === 'simialbi\yii2\rest\Connection::request') {
94+
if ($profile) {
95+
break;
96+
}
97+
$profile = true;
98+
}
99+
}
100+
101+
return [
102+
'url' => $url,
103+
'data' => $data,
104+
'headers' => $headers
105+
];
106+
}
61107
}

tests/UrlTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* @package yii2-rest-client
4+
* @author Simon Karlen <[email protected]>
5+
* @copyright Copyright © 2019 Simon Karlen
6+
*/
7+
8+
namespace yiiunit\extensions\rest;
9+
10+
use yiiunit\extensions\rest\models\RestModel;
11+
12+
class UrlTest extends TestCase
13+
{
14+
protected function setUp()
15+
{
16+
parent::setUp();
17+
18+
$this->mockWebApplication();
19+
}
20+
21+
public function testGetOne()
22+
{
23+
RestModel::findOne(1);
24+
25+
$logEntry = $this->parseLogs();
26+
27+
$this->assertEquals('https://api.site.com/rest-models/1', $logEntry['url']);
28+
}
29+
30+
public function testGetAnotherOne()
31+
{
32+
RestModel::find()->where(['id' => 1])->one();
33+
34+
$logEntry = $this->parseLogs();
35+
36+
$this->assertEquals('https://api.site.com/rest-models/1', $logEntry['url']);
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* @package yii2-rest-client
4+
* @author Simon Karlen <[email protected]>
5+
* @copyright Copyright © 2019 Simon Karlen
6+
*/
7+
8+
namespace yiiunit\extensions\rest;
9+
10+
11+
use yiiunit\extensions\rest\models\RestModel;
12+
13+
class UrlWithoutPluralisationTest extends TestCase
14+
{
15+
protected function setUp()
16+
{
17+
parent::setUp();
18+
19+
$this->mockWebApplication([
20+
'components' => [
21+
'rest' => [
22+
'usePluralisation' => false
23+
]
24+
]
25+
]);
26+
}
27+
28+
public function testGetOne()
29+
{
30+
RestModel::findOne(1);
31+
32+
$logEntry = $this->parseLogs();
33+
34+
$this->assertEquals('https://api.site.com/rest-model/1', $logEntry['url']);
35+
}
36+
37+
public function testGetAnotherOne()
38+
{
39+
RestModel::find()->where(['id' => 1])->one();
40+
41+
$logEntry = $this->parseLogs();
42+
43+
$this->assertEquals('https://api.site.com/rest-model/1', $logEntry['url']);
44+
}
45+
}

tests/log/ArrayTarget.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @package yii2-rest-client
4+
* @author Simon Karlen <[email protected]>
5+
* @copyright Copyright © 2019 Simon Karlen
6+
*/
7+
8+
namespace yiiunit\extensions\rest\log;
9+
10+
use yii\helpers\ArrayHelper;
11+
use yii\log\Target;
12+
13+
/**
14+
* Class ArrayTarget
15+
* @package yiiunit\extensions\rest\log
16+
*
17+
* @property-read array $cache
18+
*/
19+
class ArrayTarget extends Target
20+
{
21+
/**
22+
* @var array Stores log data
23+
*/
24+
private $_cache = [];
25+
26+
/**
27+
* Exports log [[messages]] to a specific destination.
28+
* Child classes must implement this method.
29+
*/
30+
public function export()
31+
{
32+
$this->_cache = ArrayHelper::merge($this->_cache, $this->messages);
33+
}
34+
35+
/**
36+
* Getter for cache variable
37+
* @return array
38+
*/
39+
public function getCache()
40+
{
41+
return $this->_cache;
42+
}
43+
}

tests/models/RestModel.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* @package yii2-rest-client
4+
* @author Simon Karlen <[email protected]>
5+
* @copyright Copyright © 2019 Simon Karlen
6+
*/
7+
8+
namespace yiiunit\extensions\rest\models;
9+
10+
11+
use simialbi\yii2\rest\ActiveRecord;
12+
13+
/**
14+
* Class RestModel
15+
* @package yiiunit\extensions\rest\models
16+
*
17+
* @property integer $id
18+
* @property string $name
19+
* @property string $description
20+
* @property integer $created_at
21+
* @property integer $updated_at
22+
* @property string $created_by
23+
* @property string $updated_by
24+
*/
25+
class RestModel extends ActiveRecord
26+
{
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public static function primaryKey()
31+
{
32+
return ['id'];
33+
}
34+
}

0 commit comments

Comments
 (0)