Skip to content

Commit 765c793

Browse files
committed
Adding FindOne
1 parent f4f592b commit 765c793

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

src/Manager/InvoiceManager.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public function __construct(InvoiceRepository $repository)
2727
/**
2828
* @param int $id
2929
*
30-
* @return Status
30+
* @return Invoice|null
3131
*/
32-
public function findOne(int $id): Status
32+
public function findOne(int $id):? Invoice
3333
{
34-
$row = $this->repository->findOne($id);
35-
return StatusHydrator::hydrate($row);
34+
$entity = $this->repository->findOne($id);
35+
return $entity;
3636
}
3737

3838
/**

src/Repository/Type/InvoiceRepository.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use App\DB\QueryBuilder;
77
use App\Entity\Type\Invoice;
88
use App\Entity\Type\Status;
9+
use App\Hydration\CustomerHydrator;
10+
use App\Hydration\InvoiceHydrator;
911
use App\Repository\AbstractRepository;
1012

1113
class InvoiceRepository extends AbstractRepository
@@ -19,11 +21,24 @@ public function __construct(Connection $connection)
1921

2022
/**
2123
* @param int $id
22-
* @return mixed
24+
* @return Invoice|null
2325
*/
24-
public function findOne(int $id)
26+
public function findOne(int $id):?Invoice
2527
{
28+
$entity = null;
29+
$sql = QueryBuilder::findOneBy('invoice');
30+
$dbCon = $this->connection->open();
31+
32+
$statement = $dbCon->prepare($sql);
33+
$statement->execute([
34+
'id' => $id
35+
]);
36+
$row = $statement->fetch();
2637

38+
if($row) {
39+
$entity = InvoiceHydrator::hydrate($row);
40+
}
41+
return $entity;
2742
}
2843

2944
/**
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use App\DB\Connection;
4+
use App\Entity\Type\Invoice;
5+
use App\Manager\InvoiceManager;
6+
use App\Repository\Type\InvoiceRepository;
7+
use Codeception\Test\Unit;
8+
9+
class DBInvoiceEntityTest extends Unit
10+
{
11+
/**
12+
* @var Code
13+
*/
14+
protected $tester;
15+
16+
/**
17+
* @group db-invoice
18+
* @group db-invoice-entity-find-one-by-id
19+
*/
20+
public function testSaveCustomerAndFindByID()
21+
{
22+
$entity = new Invoice();
23+
$entity->setVAT(3)
24+
->setTotal(3)
25+
->setReference('FooBar');
26+
$manager = $this->getManager();
27+
$savedEntity = $manager->save($entity);
28+
29+
$foundEntity = $manager->findOne($savedEntity->getId());
30+
31+
$this->assertSame($foundEntity->getVat(), $savedEntity->getVat());
32+
$this->assertSame($foundEntity->getTotal(), $savedEntity->getTotal());
33+
$this->assertSame($foundEntity->getReference(), $savedEntity->getReference());
34+
}
35+
36+
/**
37+
* @return InvoiceManager
38+
*/
39+
protected function getManager(): InvoiceManager
40+
{
41+
$connection = new Connection();
42+
$repository = new InvoiceRepository($connection);
43+
return new InvoiceManager($repository);
44+
}
45+
46+
/**
47+
* @group db-invoice
48+
* @group db-invoice-entity-find-one-by-id-not-exists
49+
*/
50+
public function testFindCustomerThatDoesNotExist()
51+
{
52+
$entity = new Invoice();
53+
$entity->setId(5001);
54+
$manager = $this->getManager();
55+
56+
$foundEntity = $manager->findOne($entity->getId());
57+
$this->assertNull($foundEntity);
58+
59+
}
60+
61+
62+
protected function _before()
63+
{
64+
}
65+
66+
protected function _after()
67+
{
68+
}
69+
}

0 commit comments

Comments
 (0)