Skip to content

Commit

Permalink
tests(app): fixing phpunit tests failures caused be refactoring revis…
Browse files Browse the repository at this point in the history
…ions, event and facade
  • Loading branch information
JoelAlphonso committed Nov 11, 2022
1 parent 19cd39a commit e62ebff
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class FormSidebarWidget extends AdminWidget implements
*/
private $requiredGlobalAclPermissions = [];

private RevisionsManager $revisionManager;
private ?RevisionsManager $revisionManager;

/**
* @param array|ArrayInterface $data Class data.
Expand Down Expand Up @@ -549,7 +549,7 @@ public function isObjRevisionable()
$this->isObjRevisionable = false;
} else {
$obj = $this->form()->obj();
if (!$obj->id()) {
if (!$obj->id() || $this->revisionManager === null) {
$this->isObjRevisionable = false;
return $this->isObjRevisionable;
}
Expand Down Expand Up @@ -903,6 +903,6 @@ protected function setDependencies(Container $container)
{
parent::setDependencies($container);

$this->revisionManager = $container['revisions/manager'];
$this->revisionManager = ($container['revisions/manager'] ?? null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Charcoal\Tests\Admin\Action;

use Charcoal\App\Facade\Facade;
use PDO;
use ReflectionClass;

Expand Down
8 changes: 8 additions & 0 deletions packages/admin/tests/Charcoal/Admin/ContainerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Charcoal\Tests\Admin;

use Charcoal\App\Facade\Facade;
use PDO;

// From Mockery
Expand Down Expand Up @@ -92,6 +93,9 @@ public function registerDebug(Container $container)
*/
public function registerBaseServices(Container $container)
{
Facade::clearResolvedFacadeInstances();
Facade::setFacadeResolver($container);

$this->registerDebug($container);
$this->registerConfig($container);
$this->registerDatabase($container);
Expand Down Expand Up @@ -390,6 +394,7 @@ public function registerCache(Container $container)
public function registerEvent(Container $container)
{
$container['event/dispatcher'] = new EventDispatcher();
$container['app/event/dispatcher'] = $container['event/dispatcher'];
}

/**
Expand Down Expand Up @@ -554,6 +559,9 @@ public function registerEmailFactory(Container $container)
*/
public function registerActionDependencies(Container $container)
{
Facade::clearResolvedFacadeInstances();
Facade::setFacadeResolver($container);

$this->registerDebug($container);
$this->registerLogger($container);
$this->registerDatabase($container);
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/Charcoal/App/Facade/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
*/
abstract class Facade
{
protected static ContainerInterface $resolver;
protected static \ArrayAccess $resolver;
/**
* @var array<string, object>
*/
protected static array $resolvedInstances = [];

public static function setFacadeResolver(ContainerInterface $resolver)
public static function setFacadeResolver(\ArrayAccess $resolver)
{
static::$resolver = $resolver;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cms/tests/Charcoal/Cms/ContainerIntegrationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Charcoal\App\AppContainer as Container;

// From 'charcoal-cms/tests'
use Charcoal\App\Facade\Facade;
use Charcoal\Tests\Cms\ContainerProvider;

/**
Expand Down Expand Up @@ -109,5 +110,8 @@ private function setupContainer()

$this->container = $container;
$this->containerProvider = $provider;

Facade::clearResolvedFacadeInstances();
Facade::setFacadeResolver($container);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Charcoal\Tests;

// From Pimple
use Charcoal\App\Facade\Facade;
use Pimple\Container;

// From 'charcoal-core/tests'
Expand Down Expand Up @@ -68,5 +69,8 @@ private function setupContainer()

$this->container = $container;
$this->containerProvider = $provider;

Facade::clearResolvedFacadeInstances();
Facade::setFacadeResolver($container);
}
}
2 changes: 2 additions & 0 deletions packages/core/tests/Charcoal/CoreContainerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Charcoal\Tests;

use Charcoal\Event\ServiceProvider\EventServiceProvider;
use PDO;

// From PSR-3
Expand Down Expand Up @@ -52,6 +53,7 @@ public function registerBaseServices(Container $container)
$this->registerSource($container);
$this->registerLogger($container);
$this->registerCache($container);
$container->register(new EventServiceProvider());
}

/**
Expand Down
55 changes: 13 additions & 42 deletions packages/core/tests/Charcoal/Mock/BadStorableMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,19 @@
/**
*
*/
class BadStorableMock extends StorableMock
class BadStorableMock extends GenericModel
{
const FAIL_AFTER = false;
const FAIL_BEFORE = true;
private bool $failAfter = false;
private bool $failBefore = false;

/**
* Whether to fail before or after an event.
*
* @var boolean
*/
private $fail = self::FAIL_BEFORE;

/**
* Create new storable mock.
*
* @param boolean $fail TRUE to fail on pre-event, FALSE to fail on post-event.
*/
public function __construct($fail = self::FAIL_BEFORE)
public function failBefore()
{
$this->fail = (bool)$fail;

parent::__construct();
$this->failBefore = true;
}

/**
* Create new storable mock to fail on before events.
*
* @return static
*/
public static function createToFailBefore()
{
return new self(self::FAIL_BEFORE);
}

/**
* Create new storable mock to fail on after events.
*
* @return static
*/
public static function createToFailAfter()
public function failAfter()
{
return new self(self::FAIL_AFTER);
$this->failAfter = true;
}

/**
Expand All @@ -61,7 +32,7 @@ public static function createToFailAfter()
*/
protected function preSave()
{
return $this->fail;
return $this->failBefore;
}

/**
Expand All @@ -72,7 +43,7 @@ protected function preSave()
*/
protected function postSave()
{
return !$this->fail;
return $this->failAfter;
}

/**
Expand All @@ -84,7 +55,7 @@ protected function postSave()
*/
protected function preUpdate(array $keys = null)
{
return $this->fail;
return $this->failBefore;
}

/**
Expand All @@ -96,7 +67,7 @@ protected function preUpdate(array $keys = null)
*/
protected function postUpdate(array $keys = null)
{
return !$this->fail;
return $this->failAfter;
}

/**
Expand All @@ -107,7 +78,7 @@ protected function postUpdate(array $keys = null)
*/
protected function preDelete()
{
return $this->fail;
return $this->failBefore;
}

/**
Expand All @@ -118,6 +89,6 @@ protected function preDelete()
*/
protected function postDelete()
{
return !$this->fail;
return $this->failAfter;
}
}
60 changes: 36 additions & 24 deletions packages/core/tests/Charcoal/Source/StorableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Charcoal\Tests\Source;

use Charcoal\Model\Service\MetadataLoader;
use Charcoal\Model\Service\ModelLoaderBuilder;
use Charcoal\Tests\Mock\GenericModel;
use InvalidArgumentException;
use RuntimeException;

Expand All @@ -27,12 +30,13 @@
*/
class StorableTraitTest extends AbstractTestCase
{
use \Charcoal\Tests\CoreContainerIntegrationTrait;
use ReflectionsTrait;

/**
* The tested class.
*
* @var StorableMock
* @var GenericModel
*/
public $obj;

Expand All @@ -43,7 +47,17 @@ class StorableTraitTest extends AbstractTestCase
*/
protected function setUp(): void
{
$this->obj = new StorableMock();
$container = $this->getContainer();

$this->factory = $container['model/factory'];
$this->obj = $this->factory->get(GenericModel::class);

$source = $this->obj->source();
if (!$source->tableExists()) {
$source->createTable();
}

// $this->obj = new StorableMock();
}

/**
Expand Down Expand Up @@ -210,7 +224,8 @@ public function testSourceFactory()
public function testMissingSourceFactory()
{
$this->expectException(RuntimeException::class);
$this->callMethod($this->obj, 'sourceFactory');
$obj = new StorableMock();
$this->callMethod($obj, 'sourceFactory');
}

/**
Expand All @@ -233,24 +248,19 @@ public function testSource()
{
$obj = $this->obj;

/** 1. Default state is NULL */
$this->assertNull($this->getPropertyValue($obj, 'source'));

/** 2. Create repository if state is NULL */
/** 1. Create repository if state is NULL */
$src1 = $obj->source();
$this->assertInstanceOf(SourceInterface::class, $src1);
$this->assertSame($src1, $this->getPropertyValue($obj, 'source'));

/** 3. Mutated state */
/** 2. Mutated state */
$src2 = $this->createSource();
$that = $obj->setSource($src2);
$this->assertSame($src2, $obj->source());
$this->assertSame($src2, $this->getPropertyValue($obj, 'source'));

/** 4. Storable can create a repository */
/** 3. Storable can create a repository */
$this->assertInstanceOf(SourceInterface::class, $this->callMethod($obj, 'createSource'));

/** 5. Chainable */
/** 4. Chainable */
$this->assertSame($that, $obj);
}

Expand Down Expand Up @@ -278,13 +288,13 @@ public function testSave()
$this->assertTrue($obj->save());

/** 2. Fail Early */
$obj = BadStorableMock::createToFailBefore();
$obj->setSource($src);
$obj = $this->factory->create(BadStorableMock::class);
$obj->failBefore();
$this->assertFalse($obj->save());

/** 3. Fail Early */
$obj = BadStorableMock::createToFailAfter();
$obj->setSource($src);
$obj = $this->factory->create(BadStorableMock::class);
$obj->failAfter();
$this->assertFalse($obj->save());
}

Expand Down Expand Up @@ -312,13 +322,13 @@ public function testUpdate()
$this->assertTrue($obj->update());

/** 2. Fail Early */
$obj = BadStorableMock::createToFailBefore();
$obj->setSource($src);
$obj = $this->factory->create(BadStorableMock::class);
$obj->failBefore();
$this->assertFalse($obj->update());

/** 3. Fail Early */
$obj = BadStorableMock::createToFailAfter();
$obj->setSource($src);
$obj = $this->factory->create(BadStorableMock::class);
$obj->failAfter();
$this->assertFalse($obj->update());
}

Expand Down Expand Up @@ -346,13 +356,15 @@ public function testDelete()
$this->assertTrue($obj->delete());

/** 2. Fail Early */
$obj = BadStorableMock::createToFailBefore();
$obj->setSource($src);
$obj = $this->factory->create(BadStorableMock::class);
$obj->setId('123');
$obj->failBefore();
$this->assertFalse($obj->delete());

/** 3. Fail Early */
$obj = BadStorableMock::createToFailAfter();
$obj->setSource($src);
$obj = $this->factory->create(BadStorableMock::class);
$obj->setId('123');
$obj->failAfter();
$this->assertFalse($obj->delete());
}
}
4 changes: 4 additions & 0 deletions packages/email/tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Charcoal\App\AppConfig;
use Charcoal\App\AppContainer;
use Charcoal\App\Facade\Facade;
use Charcoal\Config\GenericConfig;

if (($_ENV['TEST_MODE'] ?? '') === 'PACKAGE') {
Expand Down Expand Up @@ -39,3 +40,6 @@
$GLOBALS['container'] = new AppContainer([
'config' => $config
]);

Facade::clearResolvedFacadeInstances();
Facade::setFacadeResolver($GLOBALS['container']);
Loading

0 comments on commit e62ebff

Please sign in to comment.