Skip to content

Commit

Permalink
Merge pull request #12 from wayofdev/feat/save-entity
Browse files Browse the repository at this point in the history
  • Loading branch information
lotyp authored May 3, 2024
2 parents b339fd5 + 2dfc383 commit b892b74
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Exceptions/ActiveRecordException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Cycle\ActiveRecord\Exceptions;

use Throwable;

interface ActiveRecordException extends Throwable
{
}
11 changes: 11 additions & 0 deletions src/Exceptions/ConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Cycle\ActiveRecord\Exceptions;

use RuntimeException;

class ConfigurationException extends RuntimeException implements ActiveRecordException
{
}
6 changes: 3 additions & 3 deletions src/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Cycle\ActiveRecord;

use Cycle\ActiveRecord\Exceptions\ConfigurationException;
use Cycle\ORM\EntityManager;
use Cycle\ORM\EntityManagerInterface;
use Cycle\ORM\ORMInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use RuntimeException;

class Facade
{
Expand All @@ -35,12 +35,12 @@ public static function setOrm(ORMInterface $orm): void
public static function getOrm(): ORMInterface
{
if (null === self::$container) {
throw new RuntimeException('The container is not configured.');
throw new ConfigurationException('Container has not been set. Please set the container first using setContainer() method.');
}

self::$orm ??= self::$container->get(ORMInterface::class);

Check warning on line 41 in src/Facade.php

View workflow job for this annotation

GitHub Actions / mutation-testing (ubuntu-latest, 8.2, locked)

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ if (null === self::$container) { throw new ConfigurationException('Container has not been set. Please set the container first using setContainer() method.'); } - self::$orm ??= self::$container->get(ORMInterface::class); + self::$orm = self::$container->get(ORMInterface::class); if (null === self::$orm) { throw new ConfigurationException('The ORM Carrier is not configured.'); }
if (null === self::$orm) {
throw new RuntimeException('The ORM Carrier is not configured.');
throw new ConfigurationException('The ORM Carrier is not configured.');
}

return self::$orm;
Expand Down
21 changes: 18 additions & 3 deletions tests/src/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Cycle\Tests;

use Cycle\ActiveRecord\Exceptions\ConfigurationException;
use Cycle\ActiveRecord\Facade;
use Cycle\ORM\EntityManager;
use Cycle\ORM\ORMInterface;
Expand All @@ -12,7 +13,6 @@
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use RuntimeException;

final class FacadeTest extends TestCase
{
Expand All @@ -37,6 +37,21 @@ protected function tearDown(): void
Facade::reset();
}

/**
* @test
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
#[Test]
public function it_fails_to_get_orm_from_facade_when_container_is_not_set(): void
{
$this->expectException(ConfigurationException::class);
$this->expectExceptionMessage('Container has not been set.');

Facade::getOrm();
}

/**
* @test
*
Expand Down Expand Up @@ -82,8 +97,8 @@ public function it_throws_exception_when_container_does_not_have_orm(): void

Facade::setContainer($container);

// Assert that a RuntimeException is thrown when the ORM is requested but not available
$this->expectException(RuntimeException::class);
// Assert that an exception is thrown when the ORM is requested but not available
$this->expectException(ConfigurationException::class);
$this->expectExceptionMessage('The ORM Carrier is not configured.');

Facade::getOrm();
Expand Down

0 comments on commit b892b74

Please sign in to comment.