-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(orm): handle delete with
EntityResult
- Loading branch information
Showing
2 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/collection package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Doctrine\ORM\Result; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Zenstruck\Collection\Doctrine\ORM\EntityResultQueryBuilder; | ||
use Zenstruck\Collection\Tests\Doctrine\Fixture\Entity; | ||
use Zenstruck\Collection\Tests\Doctrine\HasDatabase; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class SingleScalarTest extends TestCase | ||
{ | ||
use HasDatabase; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_get_single_scalar_value_on_filter(): void | ||
{ | ||
$this->persistEntities(4); | ||
|
||
$result = EntityResultQueryBuilder::forEntity($this->em, Entity::class, 'e') | ||
->select('SUM(e.id)') | ||
->result() | ||
; | ||
|
||
$this->assertSame(10, $result->asScalar()->first()); | ||
$this->assertSame(10, $result->asInt()->first()); | ||
$this->assertSame(10.0, $result->asFloat()->first()); | ||
$this->assertSame('10', $result->asString()->first()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_get_single_scalar_value_on_delete(): void | ||
{ | ||
$this->persistEntities(4); | ||
|
||
$result = EntityResultQueryBuilder::forEntity($this->em, Entity::class, 'e') | ||
->delete() | ||
->where('e.id > 1') | ||
->result() | ||
; | ||
|
||
$this->assertSame(3, $result->asScalar()->first()); | ||
$this->assertSame(0, $result->asInt()->first()); | ||
$this->assertSame(0.0, $result->asFloat()->first()); | ||
$this->assertSame('0', $result->asString()->first()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_get_single_scalar_value_on_update(): void | ||
{ | ||
$this->persistEntities(4); | ||
|
||
$result = EntityResultQueryBuilder::forEntity($this->em, Entity::class, 'e') | ||
->update() | ||
->set('e.value', ':value') | ||
->setParameter('value', 'foo') | ||
->where('e.id > 1') | ||
->result() | ||
; | ||
|
||
$this->assertSame(3, $result->asScalar()->first()); | ||
$this->assertSame(3, $result->asInt()->first()); | ||
$this->assertSame(3.0, $result->asFloat()->first()); | ||
$this->assertSame('3', $result->asString()->first()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function not_a_collection_eager(): void | ||
{ | ||
$result = EntityResultQueryBuilder::forEntity($this->em, Entity::class, 'e') | ||
->delete() | ||
->where('e.id > 1') | ||
->result() | ||
; | ||
|
||
$this->expectException(\LogicException::class); | ||
|
||
$result->eager()->all(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function not_a_collection_iterate(): void | ||
{ | ||
$result = EntityResultQueryBuilder::forEntity($this->em, Entity::class, 'e') | ||
->delete() | ||
->where('e.id > 1') | ||
->result() | ||
; | ||
|
||
$this->expectException(\LogicException::class); | ||
|
||
foreach ($result as $item) { | ||
} | ||
} | ||
} |