Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Orcutt committed Dec 11, 2019
2 parents ee9a941 + a0d5119 commit de567ff
Show file tree
Hide file tree
Showing 30 changed files with 960 additions and 4 deletions.
23 changes: 21 additions & 2 deletions src/Zynga/Framework/Factory/V2/Base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use Zynga\Framework\Factory\V2\Interfaces\DriverInterface;
abstract class Base implements FactoryInterface {

private static Map<string, FactoryTemplateInterface> $_template = Map {};
private static Map<string, string> $_mockOverrides = Map {};

abstract public static function getClassRoot(): string;

Expand Down Expand Up @@ -87,8 +88,14 @@ abstract class Base implements FactoryInterface {
// --
$driver = $template->factory($name);

$name = 'Mock';

if (self::$_mockOverrides->containsKey($name)) {
$name = self::$_mockOverrides[$name];
} else if (strpos($name, 'Mock\\') !== false ||
strpos($name, 'Mock_') !== false) {
// No-op since Factory is pointing to a Mock Namespace
} else {
$name = 'Mock';
}
}

$driver = $template->factory($name);
Expand Down Expand Up @@ -168,7 +175,19 @@ abstract class Base implements FactoryInterface {

public static function disableMockDrivers(): bool {
$template = self::getFactoryTemplate();
self::clearOverridenMockDrivers();
return $template->disableMockDrivers();
}

public static function overrideMockDriver(
string $actualClass,
string $mockClass,
): void {
self::enableMockDrivers();
self::$_mockOverrides[$actualClass] = $mockClass;
}

public static function clearOverridenMockDrivers(): void {
self::$_mockOverrides = Map {};
}
}
43 changes: 43 additions & 0 deletions src/Zynga/Framework/Factory/V2/BaseTest.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class BaseTest extends TestCase {
return true;
}

public function tearDown(): void {
TestFactory::clearOverridenMockDrivers();
parent::tearDown();
}

public function test_construct(): void {
$foo = new FactoryTemplate("Zynga\Factory\Test");
$this->assertTrue($foo instanceof FactoryTemplate);
Expand Down Expand Up @@ -131,6 +136,44 @@ class BaseTest extends TestCase {
$this->assertTrue(TestFactory::addClassRoot('\\SomeOther\ClassRoot'));
}

public function testOverridingMockDrivers(): void {
TestFactory::enableMockDrivers();
TestFactory::clearOverridenMockDrivers();

TestFactory::overrideMockDriver('Mock', 'MockOverride');
$driver = TestFactory::factory(TestDriverInterface::class, 'Mock');
$config = $driver->getConfig();

$expected = 'This-is-overridden-'.DevelopmentMode::getModeAsString();
if ($config instanceof TestDriverConfigInterface) {
$this->assertEquals($expected, $config->getExampleConfigValue());
} else {
$this->fail('config should be TestDriverConfigInterface');
}
}

public function testMockDriversClassPathsAreNotOverriden(): void {
TestFactory::enableMockDrivers();
TestFactory::clearOverridenMockDrivers();

$driver = TestFactory::factory(TestDriverInterface::class, 'Mock\Reader');
$driver2 =
TestFactory::factory(TestDriverInterface::class, 'Mock_Reader');

$expected = 'This-is-'.DevelopmentMode::getModeAsString().'-Reader';

$config = $driver->getConfig();
$config2 = $driver2->getConfig();

if ($config instanceof TestDriverConfigInterface &&
$config2 instanceof TestDriverConfigInterface) {
$this->assertEquals($expected, $config->getExampleConfigValue());
$this->assertEquals($expected, $config2->getExampleConfigValue());
} else {
$this->fail('config should be TestDriverConfigInterface');
}
}

public function testFactoryLoadWithWrongInterface(): void {

// --
Expand Down
12 changes: 12 additions & 0 deletions src/Zynga/Framework/Factory/V2/Test/Config/Mock/Reader/Base.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?hh // strict

namespace Zynga\Framework\Factory\V2\Test\Config\Mock\Reader;

use Zynga\Framework\Factory\V2\Config\Base as ConfigBase;
use Zynga\Framework\Factory\V2\Test\Interfaces\ConfigInterface;

abstract class Base extends ConfigBase implements ConfigInterface {
public function getDriver(): string {
return 'Mock';
}
}
11 changes: 11 additions & 0 deletions src/Zynga/Framework/Factory/V2/Test/Config/Mock/Reader/Dev.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?hh // strict

namespace Zynga\Framework\Factory\V2\Test\Config\Mock\Reader;

use Zynga\Framework\Factory\V2\Test\Config\Mock\Base as ConfigBase;

class Dev extends ConfigBase {
public function getExampleConfigValue(): string {
return 'This-is-Dev-Reader';
}
}
14 changes: 14 additions & 0 deletions src/Zynga/Framework/Factory/V2/Test/Config/Mock/Reader/DevTest.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?hh //strict

namespace Zynga\Framework\Factory\V2\Test\Config\Mock\Reader;

use Zynga\Framework\Testing\TestCase\V2\Base as TestCase;

use Zynga\Framework\Factory\V2\Test\Config\Mock\Reader\Dev as TestConfig;

class DevTest extends TestCase {
public function test_configValues(): void {
$obj = new TestConfig();
$this->assertEquals('This-is-Dev-Reader', $obj->getExampleConfigValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?hh // strict

namespace Zynga\Framework\Factory\V2\Test\Config\Mock\Reader;

use Zynga\Framework\Factory\V2\Test\Config\Mock\Base as ConfigBase;

class Production extends ConfigBase {
public function getExampleConfigValue(): string {
return 'This-is-Production-Reader';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?hh //strict

namespace Zynga\Framework\Factory\V2\Test\Config\Mock\Reader;

use Zynga\Framework\Testing\TestCase\V2\Base as TestCase;

use
Zynga\Framework\Factory\V2\Test\Config\Mock\Reader\Production as TestConfig
;

class ProductionTest extends TestCase {
public function test_configValues(): void {
$obj = new TestConfig();
$this->assertEquals(
'This-is-Production-Reader',
$obj->getExampleConfigValue(),
);
}
}
11 changes: 11 additions & 0 deletions src/Zynga/Framework/Factory/V2/Test/Config/Mock/Reader/Staging.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?hh // strict

namespace Zynga\Framework\Factory\V2\Test\Config\Mock\Reader;

use Zynga\Framework\Factory\V2\Test\Config\Mock\Base as ConfigBase;

class Staging extends ConfigBase {
public function getExampleConfigValue(): string {
return 'This-is-Staging-Reader';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?hh //strict

namespace Zynga\Framework\Factory\V2\Test\Config\Mock\Reader;

use Zynga\Framework\Testing\TestCase\V2\Base as TestCase;

use Zynga\Framework\Factory\V2\Test\Config\Mock\Reader\Staging as TestConfig;

class StagingTest extends TestCase {
public function test_configValues(): void {
$obj = new TestConfig();
$this->assertEquals(
'This-is-Staging-Reader',
$obj->getExampleConfigValue(),
);
}
}
12 changes: 12 additions & 0 deletions src/Zynga/Framework/Factory/V2/Test/Config/MockOverride/Base.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?hh // strict

namespace Zynga\Framework\Factory\V2\Test\Config\MockOverride;

use Zynga\Framework\Factory\V2\Config\Base as ConfigBase;
use Zynga\Framework\Factory\V2\Test\Interfaces\ConfigInterface;

abstract class Base extends ConfigBase implements ConfigInterface {
public function getDriver(): string {
return 'Mock';
}
}
11 changes: 11 additions & 0 deletions src/Zynga/Framework/Factory/V2/Test/Config/MockOverride/Dev.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?hh // strict

namespace Zynga\Framework\Factory\V2\Test\Config\MockOverride;

use Zynga\Framework\Factory\V2\Test\Config\MockOverride\Base as ConfigBase;

class Dev extends ConfigBase {
public function getExampleConfigValue(): string {
return 'This-is-overridden-Dev';
}
}
17 changes: 17 additions & 0 deletions src/Zynga/Framework/Factory/V2/Test/Config/MockOverride/DevTest.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?hh //strict

namespace Zynga\Framework\Factory\V2\Test\Config\MockOverride;

use Zynga\Framework\Testing\TestCase\V2\Base as TestCase;

use Zynga\Framework\Factory\V2\Test\Config\MockOverride\Dev as TestConfig;

class DevTest extends TestCase {
public function test_configValues(): void {
$obj = new TestConfig();
$this->assertEquals(
'This-is-overridden-Dev',
$obj->getExampleConfigValue(),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?hh // strict

namespace Zynga\Framework\Factory\V2\Test\Config\MockOverride;

use Zynga\Framework\Factory\V2\Test\Config\MockOverride\Base as ConfigBase;

class Production extends ConfigBase {
public function getExampleConfigValue(): string {
return 'This-is-overridden-Production';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?hh //strict

namespace Zynga\Framework\Factory\V2\Test\Config\MockOverride;

use Zynga\Framework\Testing\TestCase\V2\Base as TestCase;

use
Zynga\Framework\Factory\V2\Test\Config\MockOverride\Production as TestConfig
;

class ProductionTest extends TestCase {
public function test_configValues(): void {
$obj = new TestConfig();
$this->assertEquals(
'This-is-overridden-Production',
$obj->getExampleConfigValue(),
);
}
}
11 changes: 11 additions & 0 deletions src/Zynga/Framework/Factory/V2/Test/Config/MockOverride/Staging.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?hh // strict

namespace Zynga\Framework\Factory\V2\Test\Config\MockOverride;

use Zynga\Framework\Factory\V2\Test\Config\MockOverride\Base as ConfigBase;

class Staging extends ConfigBase {
public function getExampleConfigValue(): string {
return 'This-is-overridden-Staging';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?hh //strict

namespace Zynga\Framework\Factory\V2\Test\Config\MockOverride;

use Zynga\Framework\Testing\TestCase\V2\Base as TestCase;

use
Zynga\Framework\Factory\V2\Test\Config\MockOverride\Staging as TestConfig
;

class StagingTest extends TestCase {
public function test_configValues(): void {
$obj = new TestConfig();
$this->assertEquals(
'This-is-overridden-Staging',
$obj->getExampleConfigValue(),
);
}
}
2 changes: 2 additions & 0 deletions src/Zynga/Framework/PgData/V1/Interfaces/PgModelInterface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ interface PgModelInterface {
public function getResultSetCacheName(): string;
public function getReadDatabaseName(): string;
public function getWriteDatabaseName(): string;
public function lockRowCache(PgRowInterface $row): bool;
public function reader(): ReaderInterface;
public function stats(): StatsInterface;
public function unlockRowCache(PgRowInterface $row): bool;
public function writer(): WriterInterface;
public function getByPk<TModelClass as PgRowInterface>(
classname<TModelClass> $model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ use
;
use Zynga\Framework\Type\V1\Interfaces\TypeInterface;

interface PgModelInterface extends PgModelInterfaceBase {}
interface PgModelInterface extends PgModelInterfaceBase {
public function getShardId(): TypeInterface;

}
26 changes: 26 additions & 0 deletions src/Zynga/Framework/PgData/V1/PgModelFactory.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?hh //strict

namespace Zynga\Framework\PgData\V1;

use Zynga\Framework\PgData\V1\Interfaces\PgModelInterface;

abstract class PgModelFactory {
private static ?PgModelInterface $_mock;

public function getModel(): PgModelInterface {
if (self::$_mock !== null) {
return self::$_mock;
}
return $this->getRealModel();
}
public static function enableMock(PgModelInterface $mock): void {
self::$_mock = $mock;
}

public static function disableMock(): void {
self::$_mock = null;
}

protected abstract function getRealModel(): PgModelInterface;

}
30 changes: 30 additions & 0 deletions src/Zynga/Framework/PgData/V1/PgShardedModelFactory.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?hh //strict

namespace Zynga\Framework\PgData\V1;

use Zynga\Framework\PgData\V1\Interfaces\Sharded\PgModelInterface;
use Zynga\Framework\Type\V1\Interfaces\TypeInterface;

abstract class PgShardedModelFactory {
private static ?PgModelInterface $_mock;

public function __construct(private TypeInterface $_shardKey) {}

public function getModel(): PgModelInterface {
if (self::$_mock !== null) {
return self::$_mock;
}
return $this->getRealModel();
}

public static function enableMock(PgModelInterface $mock): void {
self::$_mock = $mock;
}

public static function disableMock(): void {
self::$_mock = null;
}

protected abstract function getRealModel(): PgModelInterface;

}
Loading

0 comments on commit de567ff

Please sign in to comment.