Skip to content

Commit

Permalink
MNT Test that we can't filter/sort eagerloaded data by relation (#10978)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Oct 17, 2023
1 parent 87958e7 commit cbd358a
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions tests/php/ORM/EagerLoadedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,13 @@ public function testCanSortBy()
$this->assertFalse($subteam->canSortBy('SomethingElse'));
}

public function testCannotSortByRelation()
{
$list = $this->getListWithRecords(TeamComment::class);
$this->assertFalse($list->canSortBy('Team'));
$this->assertFalse($list->canSortBy('Team.Title'));
}

public function testArrayAccess()
{
$list = $this->getListWithRecords(Team::class)->sort('Title');
Expand Down Expand Up @@ -1112,6 +1119,14 @@ public function testSortMixedCase()
);
}

public function testSortByRelation()
{
$list = $this->getListWithRecords(TeamComment::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot sort by relations on EagerLoadedList');
$list = $list->sort('Team.Title', 'ASC');
}

/**
* @dataProvider provideSortInvalidParameters
*/
Expand Down Expand Up @@ -1324,6 +1339,26 @@ public function testCanFilterBy()
$this->assertTrue($subteam->canFilterBy("SubclassDatabaseField"));
}

public function testCannotFilterByRelation()
{
$list = $this->getListWithRecords(Team::class);

$this->assertFalse($list->canFilterBy('Captain.ShirtNumber'));
$this->assertFalse($list->canFilterBy('SomethingElse.ShirtNumber'));
$this->assertFalse($list->canFilterBy('Captain.SomethingElse'));
$this->assertFalse($list->canFilterBy('Captain.FavouriteTeam.Captain.ShirtNumber'));

// Has many
$this->assertFalse($list->canFilterBy('Fans.Name'));
$this->assertFalse($list->canFilterBy('SomethingElse.Name'));
$this->assertFalse($list->canFilterBy('Fans.SomethingElse'));

// Many many
$this->assertFalse($list->canFilterBy('Players.FirstName'));
$this->assertFalse($list->canFilterBy('SomethingElse.FirstName'));
$this->assertFalse($list->canFilterBy('Players.SomethingElse'));
}

public function testAddfilter()
{
$list = $this->getListWithRecords(TeamComment::class);
Expand Down Expand Up @@ -1441,6 +1476,60 @@ public function testFilterAndExcludeById()
$this->assertEquals(2, count($list->exclude('ID', $id) ?? []));
}

public function testFilterAnyByRelation()
{
$list = $this->getListWithRecords(Player::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Can't filter by column 'Teams.Title'");
$list = $list->filterAny(['Teams.Title' => 'Team']);
}

public function testFilterAggregate()
{
$list = $this->getListWithRecords(Team::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Can't filter by column 'Players.Count()'");
$list->filter(['Players.Count()' => 2]);
}

public function testFilterAnyAggregate()
{
$list = $this->getListWithRecords(Team::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Can't filter by column 'Players.Count()'");
$list->filterAny(['Players.Count()' => 2]);
}

public function provideCantFilterByRelation()
{
return [
'many_many' => [
'Players.FirstName',
],
'has_many' => [
'Comments.Name',
],
'has_one' => [
'FavouriteTeam.Title',
],
'non-existent relation' => [
'MascotAnimal.Name',
]
];
}

/**
* @dataProvider provideCantFilterByRelation
*/
public function testCantFilterByRelation(string $column)
{
// Many to many
$list = $this->getListWithRecords(Team::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Can't filter by column '$column'");
$list->filter($column, ['Captain', 'Captain 2']);
}

/**
* @dataProvider provideFilterByNull
*/
Expand Down

0 comments on commit cbd358a

Please sign in to comment.