Skip to content
Merged
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: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# 3.x branch
## 3.0 branch
### 3.0.4
* the `Utility::__call()` method now correctly handles properties with an initial capital letter. This means that
`BackupExport()` and `BackupImport()` magically implement the `connection()` method, which allows you to set up the
connection even at runtime;
* for testing with real drivers, a configuration for _Docker_ is proposed and used (see the `README.md` file);
* by default, the GitHub's actions now use `mariadb` (instead of `mysql`). Improved the entire configuration;
* fixed https://github.com/mirko-pagliai/cakephp-database-backup/security/dependabot/2;
Expand Down
2 changes: 0 additions & 2 deletions src/Utility/BackupExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
* Utility to export databases.
*
* @method \DatabaseBackup\Utility\BackupExport compression(\DatabaseBackup\Compression|string|null $compression)
* @method \DatabaseBackup\Utility\BackupExport filename(string $filename)
* @method \DatabaseBackup\Utility\BackupExport timeout(int $timeout)
*/
class BackupExport extends Utility
{
Expand Down
3 changes: 0 additions & 3 deletions src/Utility/BackupImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

/**
* Utility to import databases.
*
* @method \DatabaseBackup\Utility\BackupImport filename(string $filename)
* @method \DatabaseBackup\Utility\BackupImport timeout(int $timeout)
*/
class BackupImport extends Utility
{
Expand Down
7 changes: 7 additions & 0 deletions src/Utility/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
* Abstract utility.
*
* Provides methods and properties common to utility classes.
*
* @method $this connection(ConnectionInterface|string $connection)
* @method $this filename(string $filename)
* @method $this timeout(int $timeout)
*/
abstract class Utility
{
Expand Down Expand Up @@ -103,6 +107,9 @@ public function __construct(readonly protected OperationType $OperationType, Con
*/
public function __call(string $name, array $arguments)
{
if (property_exists($this, ucfirst($name))) {
$name = ucfirst($name);
}
if (!property_exists($this, $name)) {
throw new BadMethodCallException(sprintf('Method `%s::%s()` does not exist', $this::class, $name));
}
Expand Down
48 changes: 40 additions & 8 deletions tests/TestCase/Utility/UtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ class UtilityTest extends TestCase
{
protected Utility $Utility;

/**
* @inheritDoc
*/
public static function setUpBeforeClass(): void
{
ConnectionManager::setConfig('test', new FakeConnection());
ConnectionManager::setConfig('test_another_connection', new FakeConnection(['name' => 'test_another_connection']));
}

/**
* @inheritDoc
*/
public static function tearDownAfterClass(): void
{
ConnectionManager::drop('test');
ConnectionManager::drop('test_another_connection');
}

/**
* @inheritDoc
*/
Expand All @@ -53,23 +71,20 @@ public function setUp(): void
* @link \DatabaseBackup\Utility\Utility::$connection
*/
#[Test]
#[TestWith([new FakeConnection()])]
#[TestWith(['test'])]
public function testConnectionProperty(mixed $connection): void
#[TestWith([new FakeConnection(), 'test'])]
#[TestWith(['test', 'test'])]
#[TestWith(['test_another_connection', 'test_another_connection'])]
public function testConnectionProperty(string|ConnectionInterface $connection, string $expectedNameConnection): void
{
ConnectionManager::setConfig('test', new FakeConnection());

//Default value, without calling the setter
$this->assertSame('test', $this->Utility->Connection->config()['name']);

$this->Utility->Connection = $connection;

$result = $this->Utility->Connection;

ConnectionManager::drop('test');

$this->assertInstanceOf(ConnectionInterface::class, $result);
$this->assertSame('test', $result->config()['name']);
$this->assertSame($expectedNameConnection, $result->config()['name']);
}

/**
Expand Down Expand Up @@ -138,7 +153,24 @@ public function testTimeoutPropertyWithInvalidValue(): void
$this->Utility->timeout = -1;
}


/**
* @link \DatabaseBackup\Utility\Utility::__call()
*/
#[Test]
public function testCallMagicMethod(): void
{
$this->Utility->connection('test_another_connection');

$result = $this->Utility->Connection;

$this->assertInstanceOf(ConnectionInterface::class, $result);
$this->assertSame('test_another_connection', $result->config()['name']);
}

/**
* Tests for `__call()` magic method, with a non-existing method.
*
* @link \DatabaseBackup\Utility\Utility::__call()
*/
#[Test]
Expand Down