Skip to content

Commit

Permalink
Merge pull request #129 from bizley/no-yii-autoloader
Browse files Browse the repository at this point in the history
No autoloader required
  • Loading branch information
Bizley authored Feb 15, 2021
2 parents e405ae9 + fe26e44 commit 39208d7
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/phpstan.sh export-ignore
/phpstan.neon export-ignore
/phpunit.xml.dist export-ignore
/phpunit-no-yii-autoload.xml.dist export-ignore
/infection.json.dist export-ignore
/yii2-migration.png export-ignore
2 changes: 1 addition & 1 deletion .github/workflows/build-mysql5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ jobs:
run: printf "<?php\n\n\$config['mysql']['dsn']='mysql:host=127.0.0.1;port=${{ job.services.mysql.ports['3306'] }};dbname=migration';\n" >> tests/config.local.php;

- name: PHPUnit tests
run: vendor/bin/phpunit --exclude-group pgsql,sqlite
run: vendor/bin/phpunit --exclude-group pgsql,sqlite,autoloader
2 changes: 1 addition & 1 deletion .github/workflows/build-mysql8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ jobs:
run: printf "<?php\n\n\$config['mysql']['dsn']='mysql:host=127.0.0.1;port=${{ job.services.mysql.ports['3306'] }};dbname=migration';\n" >> tests/config.local.php;

- name: PHPUnit tests
run: vendor/bin/phpunit --exclude-group pgsql,sqlite
run: vendor/bin/phpunit --exclude-group pgsql,sqlite,autoloader
2 changes: 1 addition & 1 deletion .github/workflows/build-pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ jobs:
run: printf "<?php\n\n\$config['pgsql']['dsn']='pgsql:host=127.0.0.1;port=${{ job.services.postgres.ports['5432'] }};dbname=postgres';\n" >> tests/config.local.php;

- name: PHPUnit tests
run: vendor/bin/phpunit --exclude-group mysql,sqlite
run: vendor/bin/phpunit --exclude-group mysql,sqlite,autoloader
5 changes: 4 additions & 1 deletion .github/workflows/build-sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ jobs:
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader

- name: PHPUnit tests
run: vendor/bin/phpunit --exclude-group mysql,pgsql
run: vendor/bin/phpunit --exclude-group mysql,pgsql,autoloader

- name: PHPUnit autoloader test
run: vendor/bin/phpunit --configuration phpunit-no-yii-autoload.xml.dist --group autoloader
13 changes: 13 additions & 0 deletions phpunit-no-yii-autoload.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap-no-autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Yii 2 Migration Test Suite (No Yii Autoloader)">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
File renamed without changes.
4 changes: 4 additions & 0 deletions src/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public function extract(string $migration, array $migrationPaths): void
*/
private function setDummyMigrationClass(): void
{
// attempt to register Yii's autoloader in case it's not been done already
// registering it second time should be skipped anyway
spl_autoload_register(['Yii', 'autoload'], true, true);

Yii::$classMap['yii\db\Migration'] = Yii::getAlias('@bizley/migration/dummy/Migration.php');
}

Expand Down
14 changes: 14 additions & 0 deletions tests/bootstrap-no-autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

error_reporting(-1);
define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true);

$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__;

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/stubs/Yii.php';

Yii::setAlias('@bizley/migration', __DIR__ . '/../src/');
Yii::setAlias('@bizley/tests', __DIR__);
38 changes: 38 additions & 0 deletions tests/functional/NoYiiAutoloaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace bizley\tests\functional;

use bizley\tests\stubs\MigrationControllerStub;
use Yii;
use yii\base\Exception;
use yii\console\ExitCode;

/**
* This test should be run separate with phpunit-no-yii-autoload.xml config (bootstrap-no-autoload.php bootstrap)
* to make sure no standard Yii autoloader is registered first.
* @group autoloader
*/
class NoYiiAutoloaderTest extends DbLoaderTestCase
{
/** @var string */
public static $schema = 'sqlite';

/**
* @test
* @throws Exception
*/
public function shouldProvideOwnYiiAutoloader(): void
{
$controller = new MigrationControllerStub('migration', Yii::$app);
$controller->migrationPath = '@bizley/tests/migrations';
MigrationControllerStub::$stdout = '';
MigrationControllerStub::$content = '';
MigrationControllerStub::$confirmControl = true;

$this->addBase();

self::assertEquals(ExitCode::OK, $controller->runAction('update', ['updater_base']));
}
}
11 changes: 11 additions & 0 deletions tests/stubs/Yii.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require __DIR__ . '/../../vendor/yiisoft/yii2/BaseYii.php';

class Yii extends \yii\BaseYii
{
}

// no autoloader
Yii::$classMap = require __DIR__ . '/../../vendor/yiisoft/yii2/classes.php';
Yii::$container = new yii\di\Container();

0 comments on commit 39208d7

Please sign in to comment.