Skip to content

Commit

Permalink
Merge pull request #1 from monster-hunter/master
Browse files Browse the repository at this point in the history
phpunit
  • Loading branch information
zacksleo authored Aug 9, 2017
2 parents 1af99ee + 86549ac commit 1b505c7
Show file tree
Hide file tree
Showing 17 changed files with 579 additions and 9 deletions.
10 changes: 10 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
imports:
- php

tools:
external_code_coverage:
timeout: 1800 # Timeout in seconds.
# disable copy paste detector and similarity analyzer as they have no real value
# and a huge bunch of false-positives
php_sim: false
php_cpd: false
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: psr2
34 changes: 34 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
language: php

matrix:
fast_finish: true

php:
- 5.6
#- 7.1

# cache vendor dirs
cache:
directories:
- $HOME/.composer/cache
- vendor

services:
- mysql

install:
- travis_retry composer self-update
- composer config -g github-oauth.github.com dcb6b0049723eb6f56039b2d6389ac76eb29e352
- travis_retry composer install --prefer-dist --no-interaction
- travis_retry mysql -e 'CREATE DATABASE test;'

before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source --dev

script:
- ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
2 changes: 1 addition & 1 deletion Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Module extends \yii\base\Module

/**
*
* @var string source language for translation
* @var string source language for translation
*/
public $sourceLanguage = 'en-US';

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[![StyleCI](https://styleci.io/repos/99540308/shield?branch=master)](https://styleci.io/repos/99540308)
[![Build Status](https://travis-ci.org/monster-hunter/yii2-settings.svg?branch=master)](https://travis-ci.org/monster-hunter/yii2-settings)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/monster-hunter/yii2-settings/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/monster-hunter/yii2-settings/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/monster-hunter/yii2-settings/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/monster-hunter/yii2-settings/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/monster-hunter/yii2-settings/badges/build.png?b=master)](https://scrutinizer-ci.com/g/monster-hunter/yii2-settings/build-status/master)

Yii2 Settings
=============
Yii2 Database settings
Expand Down
2 changes: 0 additions & 2 deletions components/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,7 @@ public function getRawConfig()
{
if ($this->_data === null) {
if ($this->cache instanceof Cache) {

$data = $this->cache->get($this->cacheKey);

if ($data === false) {
$data = $this->model->getSettings();
$this->cache->set($this->cacheKey, $data);
Expand Down
11 changes: 10 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@
"yiisoft/yii2": ">=2.0.6",
"pheme/yii2-toggle-column": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4": {
"pheme\\settings\\": ""
}
}
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
}
4 changes: 2 additions & 2 deletions models/BaseSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use yii\base\InvalidParamException;
use yii\behaviors\TimestampBehavior;


/**
* This is the model class for table "settings".
*
Expand Down Expand Up @@ -188,7 +187,8 @@ public function deleteAllSettings()
/**
* @inheritdoc
*/
public function findSetting($key, $section = null) {
public function findSetting($key, $section = null)
{
if (is_null($section)) {
$pieces = explode('.', $key, 2);
if (count($pieces) > 1) {
Expand Down
3 changes: 1 addition & 2 deletions models/SettingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,4 @@ public function deactivateSetting($section, $key);
* @return SettingInterface single setting
*/
public function findSetting($section, $key);

}
}
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./models</directory>
<directory suffix=".php">./components</directory>
</whitelist>
</filter>
</phpunit>
83 changes: 83 additions & 0 deletions tests/BaseSettingModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Created by PhpStorm.
* User: zjw
* Date: 2017/8/7
* Time: 下午3:56
*/

namespace pheme\settings\tests;

use Yii;

class BaseSettingModelTest extends TestCase
{
public function setUp()
{
parent::setUp();
$this->setting = Yii::$app->settings;
$this->model->value = "i am testSet value";
$this->model->section = "testSetKey";
$this->model->type = 'string';
$this->model->modified = time();
$this->model->active = "1";
$this->model->key = 'testSetKey';
$this->model->save();
}

public function testSave()
{
$this->model->type = "double";
$this->assertFalse($this->model->save());
}

public function testGetSettings()
{
$res = $this->model->getSettings()['testSetKey']['testSetKey'];
$this->assertTrue($res[0] == 'i am testSet value');
$this->assertTrue($res[1] == 'string');
}

public function testSetSetting()
{
$res = $this->model->setSetting("testSetKey", "testSetKey", "aa", "string");
$this->assertTrue($res);
$res = $this->model->setSetting("testSetKey", "testSetKey1", "bb", "string");
$this->assertTrue($res);
}

public function testDeactivateSetting()
{
$res = $this->model->deactivateSetting("testSetKey", "testSetKey");
$this->assertTrue($res);
$res = $this->model->deactivateSetting("testSetKey", "testSetKey");
$this->assertFalse($res);
}

public function testActivateSetting()
{
$res1 = $this->model->deactivateSetting("testSetKey", "testSetKey");
$res2 = $this->model->activateSetting("testSetKey", "testSetKey");
$this->assertTrue($res1 && $res2);
$res3 = $this->model->activateSetting("testSetKey", "testSetKey");
$this->assertFalse($res3);
}

public function testDeleteSetting()
{
$res = $this->model->deleteSetting("testSetKey", "testSetKey");
$this->assertTrue($res == 1);
}

public function testDeleteAllSettings()
{
$res = $this->model->deleteAllSettings();
$this->assertTrue($res == 1);
}

public function testFindSetting()
{
$res = $this->model->findSetting("testSetKey", "testSetKey");
$this->assertTrue($res->id > 0);
}
}
100 changes: 100 additions & 0 deletions tests/ComponentSettingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Created by PhpStorm.
* User: zjw
* Date: 2017/8/7
* Time: 下午6:00
*/

namespace pheme\settings\tests;

use pheme\settings\components\Settings;
use Yii;

class ComponentSettingTest extends TestCase
{
/**
* @var \pheme\settings\components\Settings
*/
public $setting;

public function setUp()
{
parent::setUp();
$this->setting = Yii::$app->settings;
$this->setting->init();
$this->model->value = "i am testSet value";
$this->model->section = "testSetKey";
$this->model->type = 'string';
$this->model->modified = time();
$this->model->active = "0";
$this->model->key = 'testSetKey';
$this->model->save();
}

public function testSet()
{
$res = $this->setting->set('testSetKey', "i am testSet value", 'testSetKey');
$this->assertTrue($res, '通过组件来修改testSetKey的section');
}

public function testGet()
{
$this->setting->activate("testSetKey", "testSetKey");
$res = $this->setting->get("testSetKey", "testSetKey");
$this->assertTrue($res == "i am testSet value");
$res1 = $this->setting->get("testSetKey.testSetKey");
$this->assertTrue($res1 == "i am testSet value");
}

public function testHas()
{
$this->setting->activate("testSetKey", "testSetKey");
$res = $this->setting->has("testSetKey", "testSetKey");
$this->assertTrue($res);
}

public function testDelete()
{
$res = $this->setting->delete("testSetKey", "testSetKey");
$this->assertTrue($res == 1);
}

public function testDeleteAll()
{
$res = $this->setting->deleteAll();
$this->assertTrue($res > 0);
}

public function testActivate()
{
$res = $this->setting->activate("testSetKey", "testSetKey");
$this->assertTrue($res);
}

public function testDeActivate()
{
$this->setting->activate("testSetKey", "testSetKey");
$res = $this->setting->deactivate("testSetKey", "testSetKey");
$this->assertTrue($res);
}

public function testGetRawConfig()
{
$this->setting->activate("testSetKey", "testSetKey");
$this->setting->get('testSetKey', "testSetKey");
$res = $this->setting->getRawConfig();
$this->assertTrue($res['testSetKey']['testSetKey'][0] == $this->model->value);
}

public function testClearCache()
{
$res = $this->setting->clearCache();
$this->assertTrue($res);
}

public function testGetModelClass()
{
$this->assertTrue($this->setting->modelClass == (new Settings())->modelClass);
}
}
Loading

0 comments on commit 1b505c7

Please sign in to comment.