Skip to content

Commit

Permalink
Tests (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
KartaviK authored and Horat1us committed Nov 29, 2018
1 parent 70520c3 commit e80d4ca
Show file tree
Hide file tree
Showing 12 changed files with 589 additions and 12 deletions.
36 changes: 36 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
language: php

services:
- postgresql
- mysql

php:
- 7.2
- 7.3

env:
- DB_TYPE=pgsql
DB_HOST=localhost
DB_NAME=userdevice
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=root
- DB_TYPE=mysql
DB_HOST=127.0.0.1
DB_NAME=userdevice
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=

before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source
- sh -c "if [ '$DB_TYPE' = 'pgsql' ]; then psql -c 'CREATE DATABASE userdevice;' -U postgres; fi"
- sh -c "if [ '$DB_TYPE' = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS userdevice;'; fi"

script:
- travis_retry composer lint
- travis_retry composer cover

after_success:
- bash <(curl -s https://codecov.io/bash)
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"require-dev": {
"phpunit/phpunit": "^7.4",
"squizlabs/php_codesniffer": "^3.3",
"vlucas/phpdotenv": "^2.5"
"vlucas/phpdotenv": "^2.5",
"yiisoft/yii2-phpunit": "dev-master"
},
"license": "MIT",
"authors": [
Expand All @@ -37,7 +38,8 @@
"scripts": {
"lint": "./vendor/bin/phpcs --standard=PSR2 ./src ./tests",
"phpcbf": "./vendor/bin/phpcbf --standard=PSR2 ./src ./tests",
"test": "./vendor/bin/phpunit"
"test": "./vendor/bin/phpunit",
"cover": "./vendor/bin/phpunit --coverage-clover=coverage.xml"
},
"autoload": {
"psr-4": {
Expand Down
44 changes: 42 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@
</blacklist>
</filter>
</phpunit>

4 changes: 2 additions & 2 deletions src/Migrations/M181121080429CreateUserDeviceTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public function safeUp(): void
$this->createTable('user_device', [
'id' => $this->primaryKey(),
'user_id' => $this->integer()->unsigned()->notNull(),
'user_agent' => $this->text()->notNull(),
'user_agent' => $this->text(256)->notNull(),
'ip' => $this->string(39)->notNull(),
'created_at' => $this->timestamp()->notNull()->defaultExpression('now()'),
'updated_at' => $this->timestamp()->notNull()->defaultExpression('now()'),
]);

$this->createIndex('user_device_unique', 'user_device', [
'user_id',
'user_agent',
$this->db->getDriverName() === 'mysql' ? 'user_agent(256)' : 'user_agent',
'ip',
]);
}
Expand Down
76 changes: 76 additions & 0 deletions tests/Mocks/UserMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Wearesho\Yii\UserDevice\Tests\Mocks;

use Wearesho\Yii\UserDevice\Behavior;
use yii\web;

/**
* Class UserMock
* @package Wearesho\Yii\UserDevice\Tests\Mocks
*/
class UserMock extends web\User implements web\IdentityInterface
{
public $identityClass = web\User::class;

public $id;

public function __construct(int $id, array $config = [])
{
parent::__construct($config);
$this->id = $id;
}

public function behaviors()
{
return [
'store-device' => [
'class' => Behavior::class,
'user' => $this,
'request' => \Yii::$app->request,
],
];
}

public static function findIdentity($id)
{
return $id;
}

/**
* @param mixed $token
* @param null $type
*
* @return void|web\IdentityInterface
* @throws \Exception
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new \Exception('Method not implemented!');
}

public function getId(): int
{
return $this->id;
}

/**
* @return string|void
* @throws \Exception
*/
public function getAuthKey()
{
throw new \Exception('Method not implemented!');
}

/**
* @param string $authKey
*
* @return bool|void
* @throws \Exception
*/
public function validateAuthKey($authKey)
{
throw new \Exception('Method not implemented!');
}
}
28 changes: 28 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Wearesho\Yii\UserDevice\Tests;

use yii\helpers\ArrayHelper;
use yii\phpunit\MigrateFixture;

/**
* Class TestCase
* @package Wearesho\Yii\UserDevice\Tests
* @internal
*/
class TestCase extends \yii\phpunit\TestCase
{
public function globalFixtures(): array
{
$fixtures = [
[
'class' => MigrateFixture::class,
'migrationNamespaces' => [
'Wearesho\\Yii\\UserDevice\\Migrations',
],
]
];

return ArrayHelper::merge(parent::globalFixtures(), $fixtures);
}
}
Loading

0 comments on commit e80d4ca

Please sign in to comment.