Skip to content

Commit cff647b

Browse files
committed
Add basic tests for connection enums in models
1 parent 2a147a8 commit cff647b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
use InvalidArgumentException;
5757
use LogicException;
5858
use Mockery as m;
59+
use PHPUnit\Framework\Attributes\TestWith;
5960
use PHPUnit\Framework\TestCase;
6061
use ReflectionClass;
6162
use stdClass;
@@ -1317,6 +1318,23 @@ public function testConnectionManagement()
13171318
$this->assertSame('bar', $model->getConnection());
13181319
}
13191320

1321+
#[TestWith(['Foo'])]
1322+
#[TestWith([ConnectionName::Foo])]
1323+
#[TestWith([ConnectionNameBacked::Foo])]
1324+
public function testConnectionEnums(string|\UnitEnum $connectionName)
1325+
{
1326+
EloquentModelStub::setConnectionResolver($resolver = m::mock(ConnectionResolverInterface::class));
1327+
$model = new EloquentModelStub;
1328+
1329+
$retval = $model->setConnection($connectionName);
1330+
$this->assertEquals($retval, $model);
1331+
$this->assertSame('Foo', $model->getConnectionName());
1332+
1333+
$resolver->shouldReceive('connection')->once()->with('Foo')->andReturn('bar');
1334+
1335+
$this->assertSame('bar', $model->getConnection());
1336+
}
1337+
13201338
public function testToArray()
13211339
{
13221340
$model = new EloquentModelStub;
@@ -4432,3 +4450,13 @@ public function __toString()
44324450
return $this->cast;
44334451
}
44344452
}
4453+
4454+
enum ConnectionName {
4455+
case Foo;
4456+
case Bar;
4457+
}
4458+
4459+
enum ConnectionNameBacked : string {
4460+
case Foo = 'Foo';
4461+
case Bar = 'Bar';
4462+
}

0 commit comments

Comments
 (0)