Skip to content

Commit 39208d7

Browse files
author
Bizley
authored
Merge pull request #129 from bizley/no-yii-autoloader
No autoloader required
2 parents e405ae9 + fe26e44 commit 39208d7

File tree

11 files changed

+88
-4
lines changed

11 files changed

+88
-4
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
/phpstan.sh export-ignore
44
/phpstan.neon export-ignore
55
/phpunit.xml.dist export-ignore
6+
/phpunit-no-yii-autoload.xml.dist export-ignore
67
/infection.json.dist export-ignore
78
/yii2-migration.png export-ignore

.github/workflows/build-mysql5.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ jobs:
6363
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;
6464

6565
- name: PHPUnit tests
66-
run: vendor/bin/phpunit --exclude-group pgsql,sqlite
66+
run: vendor/bin/phpunit --exclude-group pgsql,sqlite,autoloader

.github/workflows/build-mysql8.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ jobs:
6363
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;
6464

6565
- name: PHPUnit tests
66-
run: vendor/bin/phpunit --exclude-group pgsql,sqlite
66+
run: vendor/bin/phpunit --exclude-group pgsql,sqlite,autoloader

.github/workflows/build-pgsql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ jobs:
5858
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;
5959

6060
- name: PHPUnit tests
61-
run: vendor/bin/phpunit --exclude-group mysql,sqlite
61+
run: vendor/bin/phpunit --exclude-group mysql,sqlite,autoloader

.github/workflows/build-sqlite.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@ jobs:
4444
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader
4545

4646
- name: PHPUnit tests
47-
run: vendor/bin/phpunit --exclude-group mysql,pgsql
47+
run: vendor/bin/phpunit --exclude-group mysql,pgsql,autoloader
48+
49+
- name: PHPUnit autoloader test
50+
run: vendor/bin/phpunit --configuration phpunit-no-yii-autoload.xml.dist --group autoloader

phpunit-no-yii-autoload.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="./tests/bootstrap-no-autoload.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false">
8+
<testsuites>
9+
<testsuite name="Yii 2 Migration Test Suite (No Yii Autoloader)">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>
File renamed without changes.

src/Extractor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public function extract(string $migration, array $migrationPaths): void
8080
*/
8181
private function setDummyMigrationClass(): void
8282
{
83+
// attempt to register Yii's autoloader in case it's not been done already
84+
// registering it second time should be skipped anyway
85+
spl_autoload_register(['Yii', 'autoload'], true, true);
86+
8387
Yii::$classMap['yii\db\Migration'] = Yii::getAlias('@bizley/migration/dummy/Migration.php');
8488
}
8589

tests/bootstrap-no-autoload.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
error_reporting(-1);
4+
define('YII_ENABLE_ERROR_HANDLER', false);
5+
define('YII_DEBUG', true);
6+
7+
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
8+
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
9+
10+
require_once __DIR__ . '/../vendor/autoload.php';
11+
require_once __DIR__ . '/stubs/Yii.php';
12+
13+
Yii::setAlias('@bizley/migration', __DIR__ . '/../src/');
14+
Yii::setAlias('@bizley/tests', __DIR__);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace bizley\tests\functional;
6+
7+
use bizley\tests\stubs\MigrationControllerStub;
8+
use Yii;
9+
use yii\base\Exception;
10+
use yii\console\ExitCode;
11+
12+
/**
13+
* This test should be run separate with phpunit-no-yii-autoload.xml config (bootstrap-no-autoload.php bootstrap)
14+
* to make sure no standard Yii autoloader is registered first.
15+
* @group autoloader
16+
*/
17+
class NoYiiAutoloaderTest extends DbLoaderTestCase
18+
{
19+
/** @var string */
20+
public static $schema = 'sqlite';
21+
22+
/**
23+
* @test
24+
* @throws Exception
25+
*/
26+
public function shouldProvideOwnYiiAutoloader(): void
27+
{
28+
$controller = new MigrationControllerStub('migration', Yii::$app);
29+
$controller->migrationPath = '@bizley/tests/migrations';
30+
MigrationControllerStub::$stdout = '';
31+
MigrationControllerStub::$content = '';
32+
MigrationControllerStub::$confirmControl = true;
33+
34+
$this->addBase();
35+
36+
self::assertEquals(ExitCode::OK, $controller->runAction('update', ['updater_base']));
37+
}
38+
}

0 commit comments

Comments
 (0)