Skip to content

Commit

Permalink
Phpstan installed, but won't run in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Pablo Ramirez committed Oct 20, 2020
1 parent 6fa8ad7 commit a7e10e1
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 22 deletions.
1 change: 1 addition & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:

- name: Install dependencies
run: |
composer remove --dev phpstan/phpstan
if [[ ${{ matrix.composer-type }} == 'lowest' ]]; then
composer update --prefer-dist --no-progress --no-suggest --prefer-stable --prefer-lowest
elif [[ ${{ matrix.composer-type }} == 'stable' ]]; then
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"cakephp/cakephp": "^3.6"
},
"require-dev": {
"phpstan/phpstan": "^0.12.48@dev",
"phpunit/phpunit": "^6.1"
},
"autoload": {
Expand All @@ -36,7 +37,8 @@
"scripts": {
"run-tests-mysql": "bash run_tests.sh Mysql",
"run-tests-pgsql": "bash run_tests.sh Postgres",
"run-tests-sqlite": "bash run_tests.sh Sqlite"
"run-tests-sqlite": "bash run_tests.sh Sqlite",
"run-phpstan": "./vendor/bin/phpstan analyse"
},
"config": {
"sort-packages": true
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: max
checkMissingIterableValueType: false
bootstrapFiles:
- tests/bootstrap.php
paths:
- src/
- tests/TestApp
19 changes: 8 additions & 11 deletions src/FixtureInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
namespace CakephpTestSuiteLight;

use Cake\ORM\TableRegistry;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestSuite;

Expand Down Expand Up @@ -70,13 +69,11 @@ public function startTest(Test $test)
$this->_fixtureManager->truncateDirtyTables();
}

// Load CakePHP fixtures if defined
if (!empty($test->fixtures)) {
parent::startTest($test);
}
// Load CakePHP fixtures
parent::startTest($test);

// Run the seeds of your DB
$this->rollbackAndMigrateIfRequired();
// $this->rollbackAndMigrateIfRequired();
}

/**
Expand All @@ -101,7 +98,7 @@ public function endTest(Test $test, $time)
*/
public function endTestSuite(TestSuite $suite)
{
$this->statisticTool->storeTestSuiteStatistics($suite);
$this->statisticTool->storeTestSuiteStatistics();
}

/**
Expand All @@ -112,7 +109,7 @@ public function endTestSuite(TestSuite $suite)
*/
public function skipTablesTruncation(Test $test): bool
{
return $test->skipTablesTruncation ?? false;
return isset($test->skipTablesTruncation) ? $test->skipTablesTruncation : false;
}

/**
Expand All @@ -124,8 +121,8 @@ public function skipTablesTruncation(Test $test): bool
* in its dependency. Let's think about a way to have this
* done.
*/
public function rollbackAndMigrateIfRequired()
{
// public function rollbackAndMigrateIfRequired()
// {
// $configs = Configure::read('TestFixtureMarkedNonMigrated', []);
//
// if (!empty($configs)) {
Expand All @@ -138,5 +135,5 @@ public function rollbackAndMigrateIfRequired()
// $migrations->migrate($config);
// }
// }
}
// }
}
7 changes: 6 additions & 1 deletion src/FixtureManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public function initDb(): FixtureManager
return $this;
}

/**
* @return void
*/
public function aliasConnections()
{
$this->_aliasConnections();
Expand All @@ -80,12 +83,13 @@ public function getSniffer(string $connectionName): BaseTableSniffer
} catch (\RuntimeException $e) {
throw new \PHPUnit\Framework\Exception("The DB driver $driver is not being supported");
}
/** @var BaseTableSniffer $snifferName */

return new $snifferName($connection);
}

/**
* Scan all Test connections and truncate the dirty tables
* @return void
*/
public function truncateDirtyTables()
{
Expand Down Expand Up @@ -150,6 +154,7 @@ private function getDefaultTableSniffers()
/**
* Get the appropriate truncator and drop all tables
* @param string $connectionName
* @return void
*/
public function dropTables(string $connectionName)
{
Expand Down
14 changes: 11 additions & 3 deletions src/Sniffer/BaseTableSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use Cake\Database\Exception;
use Cake\Datasource\ConnectionInterface;
use Cake\Utility\Hash;

abstract class BaseTableSniffer
{
Expand All @@ -36,7 +35,7 @@ abstract public function getDirtyTables(): array;
/**
* Truncate all the tables provided
* @param array $tables
* @return bool
* @return void
*/
abstract public function truncateTables(array $tables);

Expand Down Expand Up @@ -82,21 +81,30 @@ public function setConnection(ConnectionInterface $connection)
* Execute a query returning a list of table
* In case where the query fails because the database queried does
* not exist, an exception is thrown.
*
* @param string $query
*
* @return array
*/
protected function fetchQuery(string $query): array
{
try {
$tables = $this->getConnection()->execute($query)->fetchAll();
if ($tables === false) {
throw new \Exception("Failing query: $query");
}
} catch (\Exception $e) {
$name = $this->getConnection()->configName();
$db = $this->getConnection()->config()['database'];
var_dump($e->getMessage());
throw new Exception("Error in the connection '$name'. Is the database '$db' created and accessible?");
}

return Hash::extract($tables, '{n}.0');
foreach ($tables as $i => $val) {
$tables[$i] = $val[0];
}

return $tables;
}

/**
Expand Down
39 changes: 33 additions & 6 deletions src/StatisticTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use Cake\Datasource\ConnectionManager;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestSuite;

class StatisticTool
{
Expand Down Expand Up @@ -44,6 +43,12 @@ class StatisticTool
*/
public $time;

/**
* StatisticTool constructor.
*
* @param FixtureManager $manager
* @param bool $isActivated
*/
public function __construct(
FixtureManager $manager,
$isActivated = false
Expand All @@ -54,6 +59,9 @@ public function __construct(
$this->setFileName();
}

/**
* @return bool
*/
public function isNotActivated(): bool
{
return $this->isActivated !== true;
Expand All @@ -62,29 +70,40 @@ public function isNotActivated(): bool
/**
* @param Test $test
* @param float $time
* @return void
*/
public function collectTestStatistics(Test $test, float $time)
{
if ($this->isNotActivated()) {
return;
}

$dirytTables = $this->castDirtyTables();
$dirtyTables = $this->castDirtyTables();
$testName = method_exists($test, 'getName') ? $test->getName() : 'Test name undefined';

$this->statistics[] = [
round($time * 1000) / 1000, // Time in seconds
get_class($test), // Test Class name
$test->getName(), // Test method name
count($dirytTables), // Number of dirty tables
implode(', ', $dirytTables), // Dirty tables
$testName, // Test method name
count($dirtyTables), // Number of dirty tables
implode(', ', $dirtyTables), // Dirty tables
];
}

public function storeTestSuiteStatistics(TestSuite $suite)
/**
* Write the collected data in a csv data
* @return void
*/
public function storeTestSuiteStatistics()
{
$this->writeStatsInCsv();
}

/**
* Extract all dirty tables prefixed with their DB
* Sort them
* @return array
*/
private function castDirtyTables(): array
{
$tables = [];
Expand All @@ -101,6 +120,10 @@ private function castDirtyTables(): array
return $tables;
}

/**
* Write Stats in a CSV file
* @return void
*/
public function writeStatsInCsv()
{
if ($this->isNotActivated()) {
Expand All @@ -109,6 +132,10 @@ public function writeStatsInCsv()

$statFile = fopen($this->getFileName(), 'w');

if (!$statFile) {
return;
}

fputcsv($statFile, [
'Time (seconds)',
'Test Class',
Expand Down
72 changes: 72 additions & 0 deletions tests/TestCase/FixtureInjectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);

/**
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson
* @link https://webrider.de/
* @since 1.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace TestCase;

use CakephpTestSuiteLight\FixtureInjector;
use CakephpTestSuiteLight\FixtureManager;
use PHPUnit\Framework\TestCase;

class FixtureInjectorTest extends TestCase
{
/**
* @var FixtureInjector
*/
public $FixtureInjector;

public function setUp()
{
$this->FixtureInjector = new FixtureInjector(new FixtureManager(), true);
}

/**
* Start test should work for PHPUnit TestCase
*/
public function testStartTestWithPhpunitTestCase()
{
$test = $this->createMock(TestCase::class);
$this->FixtureInjector->startTest($test);
$this->assertTrue(true);
}

/**
* Start test should work for CakePHP TestCase
*/
public function testStartTestWithCakeTestCase()
{
$test = $this->createMock(\Cake\TestSuite\TestCase::class);
$this->FixtureInjector->startTest($test);
$this->assertTrue(true);
}

/**
* End test should work for PHPUnit TestCase
*/
public function testEndTestWithPhpunitTestCase()
{
$test = $this->createMock(TestCase::class);
$this->FixtureInjector->endTest($test, 0);
$this->assertTrue(true);
}

/**
* End test should work for CakePHP TestCase
*/
public function testEndTestWithCakeTestCase()
{
$test = $this->createMock(\Cake\TestSuite\TestCase::class);
$this->FixtureInjector->endTest($test, 0);
$this->assertTrue(true);
}
}

0 comments on commit a7e10e1

Please sign in to comment.