Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to laravel 11 #1050

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ci.phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="Phei9thoong3ai8aqu4ieHie6kee1zee"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="testing"/>
<env name="IN_CI" value="1"/>
<env name="LIMIT_UUID_LENGTH_32" value="1"/>
<env name="MAIL_MAILER" value="log"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="TENANCY_DATABASE_AUTO_DELETE_USER" value="1"/>
<env name="TENANCY_DATABASE_AUTO_DELETE" value="1"/>
<env name="TENANCY_DEFAULT_HOSTNAME" value="local.testing"/>
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
},
"require": {
"php": "^8.0",
"doctrine/dbal": "~2.5|~3.0",
"ramsey/uuid": "^4.0",
"laravel/framework": "^9.0|^10.0"
"laravel/framework": "^11.0"
},
"require-dev": {
"fakerphp/faker": "^1.12",
"laravel/laravel": "^9.0|^10.0",
"laravel/laravel": "^11.0",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^9.0",
"phpunit/phpunit": "^10.0",
"symfony/dom-crawler": "~3.1"
},
"autoload": {
Expand Down
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="Phei9thoong3ai8aqu4ieHie6kee1zee"/>
<env name="DB_DATABASE" value="tenancy"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_PASSWORD" value=""/>
<env name="DB_USERNAME" value="root"/>
<env name="MAIL_MAILER" value="log"/>
<env name="LIMIT_UUID_LENGTH_32" value="1"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="TENANCY_DATABASE_AUTO_DELETE" value="1"/>
<env name="TENANCY_DATABASE_AUTO_DELETE_USER" value="1"/>
<env name="TENANCY_DEFAULT_HOSTNAME" value="local.testing"/>
Expand Down
4 changes: 4 additions & 0 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public function identifyHostname()

return $hostname;
});

if ($this->app->resolved(CurrentHostname::class)) {
$this->app->make(CurrentHostname::class);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Webserver/Vhost/ApacheGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(Directory $directory)
public function media(Website $website)
{
return $this->directory->setWebsite($website)->isLocal() && $this->directory->exists('media') ?
$this->directory->path('media', true) :
$this->directory->localPath('media') :
null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Webserver/Vhost/NginxGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(Directory $directory)
public function media(Website $website)
{
return $this->directory->setWebsite($website)->isLocal() && $this->directory->exists('media') ?
$this->directory->path('media', true) :
$this->directory->localPath('media') :
null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/Filesystem/LoadsTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LoadsTranslations extends AbstractTenantDirectoryListener
public function load(WebsiteEvent $event)
{
if ($this->directory()->isLocal()) {
$this->readLanguageFiles($this->directory()->path($this->path, true));
$this->readLanguageFiles($this->directory()->localPath($this->path));
} else {
throw new FilesystemException("$this->path is not available locally, cannot include");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/Filesystem/LoadsViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function load(WebsiteEvent $event)
if ($this->directory()->isLocal()) {
/** @var Factory views */
$this->views = app(Factory::class);
$this->viewsPath = $this->directory()->path($this->path, true);
$this->viewsPath = $this->directory()->localPath($this->path);

$namespace = $this->config->get('tenancy.folders.views.namespace');

Expand Down
37 changes: 33 additions & 4 deletions src/Website/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ public function setWebsite(Website $website): Directory

/**
* @param string $path
* @param bool $local
* @return string
*/
public function path(string $path = null, $local = false): string
public function path($path = null): string
{
$prefix = "{$this->getWebsite()->uuid}/";

Expand All @@ -90,7 +89,18 @@ public function path(string $path = null, $local = false): string
$path = "$prefix$path";
}

if ($local && $this->isLocal()) {
return $path;
}

/**
* @param string $path
* @return string
*/
public function localPath(string $path = null): string
{
$path = $this->path($path);

if ($this->isLocal()) {
$config = $this->filesystem->getConfig();
$path = sprintf(
"%s/%s",
Expand All @@ -102,6 +112,25 @@ public function path(string $path = null, $local = false): string
return $path;
}

public function putFile($path, $file = null, $options = [])
{
return $this->filesystem->putFile(
$this->path($path),
$file,
$options
);
}

public function putFileAs($path, $file, $name = null, $options = [])
{
return $this->filesystem->putFileAs(
$this->path($path),
$file,
$name,
$options
);
}

/**
* Get the contents of a file.
*
Expand Down Expand Up @@ -367,7 +396,7 @@ public function isLocal(): bool
public function __call($name, $arguments)
{
if ($this->isLocal() && method_exists($this->local, $name)) {
$arguments[0] = $this->path($arguments[0], true);
$arguments[0] = $this->localPath($arguments[0]);

return call_user_func_array([$this->local, $name], $arguments);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/Commands/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Hyn\Tenancy\Tests\Commands;

use App\Console\Kernel;
use Illuminate\Foundation\Console\Kernel;
use Hyn\Tenancy\Tests\Test;
use Illuminate\Contracts\Foundation\Application;

Expand Down
12 changes: 6 additions & 6 deletions tests/unit-tests/Commands/SeedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function runs_seed_on_one_tenant()

$this->connection->set($this->website);

$this->assertFalse($this->connection->get()->getDoctrineSchemaManager()->tablesExist('users'));
$this->assertTrue($this->connection->get()->getDoctrineSchemaManager()->tablesExist('samples'));
$this->assertFalse($this->connection->get()->getSchemaBuilder()->hasTable('users'));
$this->assertTrue($this->connection->get()->getSchemaBuilder()->hasTable('samples'));

$this->assertGreaterThan(
0,
Expand Down Expand Up @@ -87,8 +87,8 @@ public function runs_configured_seed()

$this->connection->set($this->website);

$this->assertFalse($this->connection->get()->getDoctrineSchemaManager()->tablesExist('users'));
$this->assertTrue($this->connection->get()->getDoctrineSchemaManager()->tablesExist('samples'));
$this->assertFalse($this->connection->get()->getSchemaBuilder()->hasTable('users'));
$this->assertTrue($this->connection->get()->getSchemaBuilder()->hasTable('samples'));
}

/**
Expand All @@ -98,14 +98,14 @@ public function runs_seed_on_tenants()
{
$this->connection->set($this->website);

$this->assertFalse($this->connection->get()->getDoctrineSchemaManager()->tablesExist('samples'));
$this->assertFalse($this->connection->get()->getSchemaBuilder()->hasTable('samples'));

$this->migrateAndTest('migrate');

$this->seedAndTest(function (Website $website) {
$this->connection->set($website);

$this->assertTrue($this->connection->get()->getDoctrineSchemaManager()->tablesExist('samples'));
$this->assertTrue($this->connection->get()->getSchemaBuilder()->hasTable('samples'));

$this->assertEquals(
2,
Expand Down
9 changes: 8 additions & 1 deletion tests/unit-tests/Database/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

namespace Hyn\Tenancy\Tests\Database;

use Doctrine\DBAL\Driver\PDOException;
use Hyn\Tenancy\Commands\UpdateKeyCommand;
use Hyn\Tenancy\Contracts\CurrentHostname;
use Hyn\Tenancy\Environment;
Expand All @@ -23,6 +22,7 @@
use Hyn\Tenancy\Tests\Test;
use Illuminate\Database\Connection as DatabaseConnection;
use Illuminate\Support\Str;
use PDOException;

class ConnectionTest extends Test
{
Expand Down Expand Up @@ -135,16 +135,23 @@ public function can_rotate_tenant_key()

// Re-establish connection and expect 1045 error code (Access denied for user)
app(Environment::class)->tenant($this->website);

$hasCaughtException = false;
try {
$this->connection->get()->reconnect();
$this->connection->get()->getPdo();
} catch (PDOException $e) {
$this->assertTrue($e->getCode() === 1045 || $e->getCode() === 7, 'Access should be denied for tenant database user: [code: '.$e->getCode().'] '. $e->getMessage());
$hasCaughtException = true;
}

$this->assertTrue($hasCaughtException, 'Access should be denied for tenant database user.');

$this->artisan(UpdateKeyCommand::class);

// Re-establish connection after updating tenant users password
app(Environment::class)->tenant($this->website);
$this->connection->get()->reconnect();
$this->connection->get()->getPdo();
}
}
7 changes: 3 additions & 4 deletions tests/unit-tests/Database/MultiDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Hyn\Tenancy\Database\Connection;
use Hyn\Tenancy\Tests\Test;
use Illuminate\Contracts\Foundation\Application;
use PDOException;

class MultiDatabaseTest extends Test
{
Expand Down Expand Up @@ -50,9 +51,7 @@ public function allow_writing_to_secondary_database()
// make sure the Website model still uses the regular system name.
$this->assertEquals(app(Connection::class)->systemName(), $this->website->getConnectionName());

$this->assertTrue(in_array(
$this->website->uuid,
$this->getConnection('secondary')->getDoctrineSchemaManager()->listDatabases()
));
$this->expectException(PDOException::class);
$this->getConnection('secondary')->getSchemaBuilder()->createDatabase($this->website->uuid);
}
}
2 changes: 1 addition & 1 deletion tests/unit-tests/Providers/RouteProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function overrides_global_route()
{
$this->overrideGlobalRoute();

$this->assertEquals(2, $this->app['router']->getRoutes()->count());
$this->assertGreaterThan(1, $this->app['router']->getRoutes()->count());
}

/**
Expand Down