Skip to content

Instantly share code, notes, and snippets.

@Danack
Created August 2, 2019 19:16
Show Gist options
  • Save Danack/4c5cd824c8de0e97c35ebaa709fd873f to your computer and use it in GitHub Desktop.
Save Danack/4c5cd824c8de0e97c35ebaa709fd873f to your computer and use it in GitHub Desktop.
testing dbs.
<?php
declare(strict_types = 1);
namespace Osf\Repo\ProjectRepo;
use Doctrine\ORM\EntityManager;
use Osf\Model\BankDetails;
use Osf\Model\ContactInformation;
use Osf\Model\Project;
class DoctrineProjectRepo implements ProjectRepo
{
/** @var EntityManager */
private $em;
/**
* DoctrineBookListRepo constructor.
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* @return Project[]
*/
public function getAllProjects()
{
$repo = $this->em->getRepository(Project::class);
/** @var Project[] $result */
$result = $repo->findAll();
return $result;
}
/**
* Get a project by name or null if no project can't be found
*
* @param string $project_name
* @return Project|null
*/
public function getProjectByName(string $project_name): ?Project
{
$repo = $this->em->getRepository(Project::class);
/** @var \Osf\Model\Project|null $result */
$result = $repo->findOneBy(['name' => $project_name]);
return $result;
}
public function createProject(string $project_name) : Project
{
$project = Project::fromPartial($project_name);
$this->em->persist($project);
$this->em->flush();
return $project;
}
public function getBankDetailsForProject(Project $project): ?BankDetails
{
$repo = $this->em->getRepository(BankDetails::class);
/** @var \Osf\Model\BankDetails|null $result */
$result = $repo->findOneBy(['project' => $project]);
return $result;
}
public function getContactInformationForProject(Project $project): ?ContactInformation
{
$repo = $this->em->getRepository(ContactInformation::class);
/** @var \Osf\Model\ContactInformation|null $result */
$result = $repo->findOneBy(['project' => $project]);
return $result;
}
public function setProjectContactInformation(
Project $project,
string $name,
string $phone,
string $email,
string $website,
array $addressLines
): ContactInformation {
$contactInformation = new ContactInformation(
$name,
$phone,
$email,
$website,
$addressLines,
$project
);
$this->em->persist($contactInformation);
$this->em->flush($contactInformation);
return $contactInformation;
}
public function setProjectBankDetails(
Project $project,
string $name,
string $sort_code,
string $account_number
): BankDetails {
$bankDetails = new BankDetails(
$name,
$sort_code,
$account_number,
$project
);
$this->em->persist($bankDetails);
$this->em->flush($bankDetails);
return $bankDetails;
}
}
/**
* @coversNothing
*/
class DoctrineProjectRepoTest extends BaseTestCase
{
use Testing;
/**
* @covers \Osf\Repo\ProjectRepo\DoctrineProjectRepo
*/
public function testWorks()
{
$projectRepo = $this->injector->make(DoctrineProjectRepo::class);
$allProjectsBefore = $projectRepo->getAllProjects();
$projectName = 'DoctrineProjectRepoTest_' . time();
$projectCreated = $projectRepo->createProject($projectName);
$this->assertSame($projectName, $projectCreated->getName());
$projectFetchedByName = $projectRepo->getProjectByName($projectName);
$this->assertEquals(
$projectCreated,
$projectFetchedByName
);
$allProjectsAfter = $projectRepo->getAllProjects();
$countDifference = count($allProjectsAfter) - count($allProjectsBefore);
$this->assertSame(
1,
$countDifference,
'Adding one project did not increase the number of projects by one.'
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment