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

Make Abstract{Migration,Seed}::getAdapter return non-null #2261

Merged
merged 3 commits into from
Jan 23, 2024
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
6 changes: 5 additions & 1 deletion src/Phinx/Migration/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ public function setAdapter(AdapterInterface $adapter): MigrationInterface
/**
* @inheritDoc
*/
public function getAdapter(): ?AdapterInterface
public function getAdapter(): AdapterInterface
{
if (!isset($this->adapter)) {
throw new RuntimeException('Cannot access `adapter` it has not been set');
}

return $this->adapter;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Phinx/Seed/AbstractSeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Table;
use RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -98,6 +99,10 @@ public function setAdapter(AdapterInterface $adapter): SeedInterface
*/
public function getAdapter(): AdapterInterface
{
if (!isset($this->adapter)) {
throw new RuntimeException('Cannot access `adapter` it has not been set');
}

return $this->adapter;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Phinx/Migration/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function testAdapterMethods()
->getMock();

// test methods
$this->assertNull($migrationStub->getAdapter());
$this->expectException(RuntimeException::class);
$migrationStub->getAdapter();
$migrationStub->setAdapter($adapterStub);
$this->assertInstanceOf(
'Phinx\Db\Adapter\AdapterInterface',
Expand Down
30 changes: 30 additions & 0 deletions tests/Phinx/Seed/AbstractSeedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace Test\Phinx\Seed;

use PHPUnit\Framework\TestCase;
use RuntimeException;

class AbstractSeedTest extends TestCase
{
public function testAdapterMethods()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Seed\AbstractSeed', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
->setConstructorArgs([[]])
->getMock();

// test methods
$this->expectException(RuntimeException::class);
$migrationStub->getAdapter();
$migrationStub->setAdapter($adapterStub);
$this->assertInstanceOf(
'Phinx\Db\Adapter\AdapterInterface',
$migrationStub->getAdapter()
);
}
}
Loading