Skip to content

Commit

Permalink
Merge pull request #4 from challgren/add-tests
Browse files Browse the repository at this point in the history
Improve testing and coverage
  • Loading branch information
akutaktau authored Jan 2, 2019
2 parents e4642b9 + 53b3e77 commit 4b4f503
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 19 deletions.
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Created by .ignore support plugin (hsz.mobi)
### Windows template
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Windows shortcuts
*.lnk
### user template

# IntelliJ project files
.idea
*.iml
out
gen

### CakePHP template
# CakePHP 3

/vendor/*
/config/app.php

/tmp

### macOS template
# General
.DS_Store
.AppleDouble
.LSOverride

# Thumbnails
._*
/composer.lock
/phpunit.xml
/phpunit.phar
59 changes: 59 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
language: php

sudo: false

php:
- 5.6
- 7.0
- 7.3

env:
matrix:
- DB=mysql db_dsn='mysql://[email protected]/cakephp_test'
global:
- DEFAULT=1

matrix:
fast_finish: true

include:
- php: 7.1
env: DB=pgsql db_dsn='postgres://[email protected]/cakephp_test'

- php: 7.2
env: DB=sqlite db_dsn='sqlite:///:memory:'

- php: 5.6
env: PREFER_LOWEST=1

- php: 7.2
env: CHECKS=1 DEFAULT=0

- php: 7.2
env: CODECOVERAGE=1 DEFAULT=0

before_script:
- if [[ $PREFER_LOWEST != 1 ]]; then composer install --prefer-source --no-interaction ; fi
- if [[ $PREFER_LOWEST == 1 ]]; then composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable ; fi

- if [[ $CHECKS != 1 ]]; then composer require phpunit/phpunit:"^5.7.14|^6.0"; fi

- sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi"
- sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi"

- phpenv rehash
- set +H
- cp phpunit.xml.dist phpunit.xml

script:
- if [[ $DEFAULT == 1 ]]; then vendor/bin/phpunit; fi

- if [[ $CHECKS == 1 ]]; then composer phpstan-setup && composer phpstan ; fi
- if [[ $CHECKS == 1 ]]; then composer cs-check ; fi

- if [[ $CODECOVERAGE == 1 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml || true; fi
- if [[ $CODECOVERAGE == 1 ]]; then wget -O codecov.sh https://codecov.io/bash; fi
- if [[ $CODECOVERAGE == 1 ]]; then bash codecov.sh; fi

notifications:
email: false
25 changes: 21 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,37 @@
"type": "cakephp-plugin",
"license": "MIT",
"require": {
"php": ">5.6",
"cakephp/cakephp": "^3.6"
},
"require-dev": {
"phpunit/phpunit": "^5.7|^6.0"
"squizlabs/php_codesniffer": "^3.4",
"cakephp/cakephp-codesniffer": "^3.0"
},
"autoload": {
"psr-4": {
"CakeImpersonate\\": "src"
"CakeImpersonate\\": "src/",
"CakeImpersonate\\Test\\Fixture\\": "tests/Fixture/"
}
},
"autoload-dev": {
"psr-4": {
"CakeImpersonate\\Test\\": "tests",
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
"CakeImpersonate\\Test\\": "tests/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/",
"Cake\\PHPStan\\": "vendor/cakephp/cakephp/tests/PHPStan/",
"App\\": "tests/test_app/"
}
},
"scripts": {
"phpstan": "phpstan analyse -c tests/phpstan.neon -l 3 src/",
"phpstan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.10.1 && mv composer.backup composer.json",
"test": "php phpunit.phar",
"test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit-6.5.13.phar && mv phpunit-6.5.13.phar phpunit.phar || true",
"test-coverage": "php phpunit.phar --log-junit webroot/coverage/unitreport.xml --coverage-html webroot/coverage --coverage-clover webroot/coverage/coverage.xml",
"cs-check": "phpcs -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP --extensions=php --ignore=/tests/test_files/ src/ tests/ config/",
"cs-fix": "phpcbf -v --standard=vendor/cakephp/cakephp-codesniffer/CakePHP --extensions=php --ignore=/tests/test_files/ src/ tests/ config/"
},
"config": {
"process-timeout": 600
}
}
11 changes: 8 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
<!-- E_ALL => 32767 -->
<!-- E_ALL & ~E_USER_DEPRECATED => 16383 -->
<ini name="error_reporting" value="16383"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Cakeimpersonate Test Suite">
<directory>./tests/TestCase</directory>
<directory>tests/</directory>
</testsuite>
</testsuites>

Expand All @@ -31,8 +34,10 @@

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
<directory suffix=".php">src/</directory>
</whitelist>
<blacklist>
<directory suffix=".ctp">src/Template/</directory>
</blacklist>
</filter>

</phpunit>
2 changes: 1 addition & 1 deletion src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CakeImpersonate\Controller;

use App\Controller\AppController as BaseController;
use Cake\Controller\Controller as BaseController;

class AppController extends BaseController
{
Expand Down
15 changes: 15 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: challgren
* Date: 2019-01-02
* Time: 05:52
*/

namespace CakeImpersonate;

use Cake\Core\BasePlugin;

class Plugin extends BasePlugin
{
}
47 changes: 47 additions & 0 deletions tests/Fixture/UsersFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Created by PhpStorm.
* User: challgren
* Date: 2019-01-02
* Time: 05:57
*/

namespace CakeImpersonate\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class UsersFixture extends TestFixture
{
/**
* Fields
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'null' => true],
'password' => ['type' => 'string', 'null' => true],
'active' => ['type' => 'boolean', 'null' => true],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
];
/**
* Records
*
* @var array
*/
public $records = [
[
'id' => 1,
'name' => 'test-user',
'password' => '12345678',
'active' => true,
],
[
'id' => 2,
'name' => 'tester',
'password' => '12345678',
'active' => false,
],
];
}
66 changes: 55 additions & 11 deletions tests/TestCase/Controller/Component/ImpersonateComponentTest.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
<?php
namespace CakeImpersonate\Test\TestCase\Controller\Component;
namespace App\Test\TestCase\Controller\Component;

use Cake\Controller\ComponentRegistry;
use App\Controller\ImpersonateTestController;
use Cake\Core\Configure;
use Cake\Http\ServerRequest;
use Cake\TestSuite\TestCase;
use Impersonate\Controller\Component\ImpersonateComponent;

/**
* CakeImpersonate\Controller\Component\ImpersonateComponent Test Case
* App\Controller\Component\ImpersonateComponent Test Case
*/
class ImpersonateComponentTest extends TestCase
{

/**
* Test subject
*
* @var \CakeImpersonate\Controller\Component\ImpersonateComponent
* @var \App\Controller\ImpersonateTestController
*/
public $Impersonate;

public $fixtures = [
'plugin.CakeImpersonate.Users'
];

public $Auth = [
'User' => [
'id' => 1,
'name' => 'test-user',
'password' => '12345678',
'active' => true
]
];

/**
* setUp method
*
Expand All @@ -26,8 +40,13 @@ class ImpersonateComponentTest extends TestCase
public function setUp()
{
parent::setUp();
$registry = new ComponentRegistry();
$this->Impersonate = new ImpersonateComponent($registry);
Configure::write('App.fullBaseUrl', 'http://localhost');

$request = new ServerRequest('/my_controller/foo');
$request = $request->withParam('controller', 'MyController')
->withParam('action', 'foo');
$this->Impersonate = new ImpersonateTestController($request);
$this->Impersonate->startupProcess();
}

/**
Expand All @@ -38,17 +57,42 @@ public function setUp()
public function tearDown()
{
unset($this->Impersonate);
unset($this->Auth);

parent::tearDown();
}

/**
* Test initial setup
*
* @return void
*/
public function testInitialization()
public function testIsImpersonate()
{
$this->markTestIncomplete('Not implemented yet.');
$this->assertFalse($this->Impersonate->Impersonate->isImpersonate());

$this->Impersonate->getRequest()->getSession()->write('OriginalAuth', $this->Auth);
$this->assertTrue($this->Impersonate->Impersonate->isImpersonate());
}

/**
* @return void
*/
public function testLogout()
{
$this->assertTrue($this->Impersonate->Impersonate->logout());

$this->Impersonate->getRequest()->getSession()->write('OriginalAuth', $this->Auth);
$this->assertTrue($this->Impersonate->Impersonate->logout());
$this->assertEquals($this->Auth, $this->Impersonate->getRequest()->getSession()->read('Auth'));
}

/**
* @return void
*/
public function testLogin()
{
$this->Impersonate->getRequest()->getSession()->write('Auth', $this->Auth);
$this->assertTrue($this->Impersonate->Impersonate->login(2));

$this->assertEquals($this->Auth, $this->Impersonate->getRequest()->getSession()->read('OriginalAuth'));
}
}
9 changes: 9 additions & 0 deletions tests/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
autoload_files:
- %rootDir%/../../../tests/bootstrap.php
excludes_analyse:
- %rootDir%/../../../src/TestSuite/*

services:
-
class: Cake\PHPStan\AssociationTableMixinClassReflectionExtension
30 changes: 30 additions & 0 deletions tests/test_app/Controller/ImpersonateTestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Created by PhpStorm.
* User: challgren
* Date: 2019-01-02
* Time: 05:28
*/

namespace App\Controller;

use Cake\Controller\Controller;

/**
* Use Controller instead of AppController to avoid conflicts
*
* @property \CakeImpersonate\Controller\Component\ImpersonateComponent $Impersonate
*/
class ImpersonateTestController extends Controller
{

/**
* {@inheritdoc}
*/
public function initialize()
{
$this->loadComponent('Auth');
$this->loadComponent('CakeImpersonate.Impersonate');
parent::initialize();
}
}
Loading

0 comments on commit 4b4f503

Please sign in to comment.