diff --git a/composer.json b/composer.json index be25807..9f7bda1 100644 --- a/composer.json +++ b/composer.json @@ -21,14 +21,14 @@ "ext-mbstring" : "*", "ext-json" : "*", "ext-mongodb" : ">=1.5.0", - "fatcode/enum": ">=1.0.0" + "fatcode/enum": ">=1.0.0", + "fatcode/hydration": ">=1.0.0" }, "require-dev": { "phpunit/phpunit": ">=8.0", "mockery/mockery": ">=1.2", "fzaninotto/faker": ">=1.8", - "squizlabs/php_codesniffer": ">=3.0", - "vimeo/psalm": ">=3.2" + "squizlabs/php_codesniffer": ">=3.0" }, "autoload": { "psr-4": { diff --git a/src/Driver/Command.php b/src/Driver/Command.php index 691a2b5..6029a6e 100644 --- a/src/Driver/Command.php +++ b/src/Driver/Command.php @@ -4,5 +4,5 @@ interface Command { - public function execute(callable $handler, Connection $connection): void; + public function execute(callable $handler, Connection $connection) : void; } diff --git a/src/Driver/Cursor.php b/src/Driver/Cursor.php index 2a80059..1fc1e8a 100644 --- a/src/Driver/Cursor.php +++ b/src/Driver/Cursor.php @@ -6,7 +6,7 @@ interface Cursor extends Iterator { - public function getConnection(): Connection; - public function close(): void; - public function isClosed(): bool; + public function getConnection() : Connection; + public function close() : void; + public function isClosed() : bool; } diff --git a/src/Driver/MongoDb/Command/Aggregate.php b/src/Driver/MongoDb/Command/Aggregate.php index 4586e43..f4dfe9b 100644 --- a/src/Driver/MongoDb/Command/Aggregate.php +++ b/src/Driver/MongoDb/Command/Aggregate.php @@ -23,12 +23,12 @@ public function __construct(string $collection, PipelineOperation ...$pipeline) $this->pipeline = $pipeline; } - public function setCollation(Collation $collation): void + public function setCollation(Collation $collation) : void { $this->collation = $collation; } - public function execute(callable $handler, Connection $connection): void + public function execute(callable $handler, Connection $connection) : void { $command = [ 'aggregate' => $this->collection, diff --git a/src/Driver/MongoDb/Command/Changeset.php b/src/Driver/MongoDb/Command/Changeset.php index dbe6e2c..b39e562 100644 --- a/src/Driver/MongoDb/Command/Changeset.php +++ b/src/Driver/MongoDb/Command/Changeset.php @@ -4,6 +4,7 @@ use FatCode\Storage\Driver\MongoDb\Command\Operation\UpdateOperation; use stdClass; + use function array_merge_recursive; class Changeset diff --git a/src/Driver/MongoDb/Command/Operation/AddToSet.php b/src/Driver/MongoDb/Command/Operation/AddToSet.php index 306d484..32f111a 100644 --- a/src/Driver/MongoDb/Command/Operation/AddToSet.php +++ b/src/Driver/MongoDb/Command/Operation/AddToSet.php @@ -17,7 +17,7 @@ public function __construct(string $field, ...$values) $this->values = $values; } - public function apply(): array + public function apply() : array { if (count($this->values) > 1) { diff --git a/src/Driver/MongoDb/Command/Operation/ConstrainMaximum.php b/src/Driver/MongoDb/Command/Operation/ConstrainMaximum.php index cd67fa0..4889517 100644 --- a/src/Driver/MongoDb/Command/Operation/ConstrainMaximum.php +++ b/src/Driver/MongoDb/Command/Operation/ConstrainMaximum.php @@ -17,7 +17,7 @@ public function __construct(string $field, $value) $this->value = $value; } - public function apply(): array + public function apply() : array { return [ '$min' => [ diff --git a/src/Driver/MongoDb/Command/Operation/ConstrainMinimum.php b/src/Driver/MongoDb/Command/Operation/ConstrainMinimum.php index fc24117..86daf69 100644 --- a/src/Driver/MongoDb/Command/Operation/ConstrainMinimum.php +++ b/src/Driver/MongoDb/Command/Operation/ConstrainMinimum.php @@ -17,7 +17,7 @@ public function __construct(string $field, $value) $this->value = $value; } - public function apply(): array + public function apply() : array { return [ '$max' => [ diff --git a/src/Driver/MongoDb/Command/Operation/FindOperation.php b/src/Driver/MongoDb/Command/Operation/FindOperation.php index d9cc399..d1c220f 100644 --- a/src/Driver/MongoDb/Command/Operation/FindOperation.php +++ b/src/Driver/MongoDb/Command/Operation/FindOperation.php @@ -4,5 +4,5 @@ interface FindOperation { - public function apply(): array; + public function apply() : array; } diff --git a/src/Driver/MongoDb/Command/Operation/Increment.php b/src/Driver/MongoDb/Command/Operation/Increment.php index 7874172..41c83c7 100644 --- a/src/Driver/MongoDb/Command/Operation/Increment.php +++ b/src/Driver/MongoDb/Command/Operation/Increment.php @@ -18,7 +18,7 @@ public function __construct(string $field, int $value) $this->value = $value; } - public function apply(): array + public function apply() : array { return [ '$inc' => [ diff --git a/src/Driver/MongoDb/Command/Operation/Join.php b/src/Driver/MongoDb/Command/Operation/Join.php index a052af5..e54f973 100644 --- a/src/Driver/MongoDb/Command/Operation/Join.php +++ b/src/Driver/MongoDb/Command/Operation/Join.php @@ -16,12 +16,12 @@ public function __construct(string $from, string $localKey, string $foreignKey, ]; } - public function addToPipeline(): array + public function addToPipeline() : array { return ['$lookup' => $this->join]; } - public function apply(): array + public function apply() : array { return []; } diff --git a/src/Driver/MongoDb/Command/Operation/Limit.php b/src/Driver/MongoDb/Command/Operation/Limit.php index a805818..59b773a 100644 --- a/src/Driver/MongoDb/Command/Operation/Limit.php +++ b/src/Driver/MongoDb/Command/Operation/Limit.php @@ -13,7 +13,7 @@ public function __construct(int $limit, int $offset = null) $this->offset = $offset; } - public function addToPipeline(): array + public function addToPipeline() : array { if ($this->offset !== null) { return ['$limit' => $this->limit, '$skip' => $this->offset]; @@ -22,7 +22,7 @@ public function addToPipeline(): array return ['$limit' => $this->limit]; } - public function apply(): array + public function apply() : array { if ($this->offset !== null) { return ['limit' => $this->limit, 'skip' => $this->offset]; diff --git a/src/Driver/MongoDb/Command/Operation/Match.php b/src/Driver/MongoDb/Command/Operation/Match.php index 61a813d..c54f2e3 100644 --- a/src/Driver/MongoDb/Command/Operation/Match.php +++ b/src/Driver/MongoDb/Command/Operation/Match.php @@ -11,7 +11,7 @@ public function __construct(array $query) $this->query = $query; } - public function addToPipeline(): array + public function addToPipeline() : array { return ['$match' => $this->query]; } diff --git a/src/Driver/MongoDb/Command/Operation/Multiply.php b/src/Driver/MongoDb/Command/Operation/Multiply.php index 2657749..fea5a57 100644 --- a/src/Driver/MongoDb/Command/Operation/Multiply.php +++ b/src/Driver/MongoDb/Command/Operation/Multiply.php @@ -2,9 +2,10 @@ namespace FatCode\Storage\Driver\MongoDb\Command\Operation; +use FatCode\Storage\Exception\DriverException; + use function is_float; use function is_int; -use FatCode\Storage\Exception\DriverException; /** * Delete operator deletes the specified field @@ -30,7 +31,7 @@ public function __construct(string $field, $value) $this->value = $value; } - public function apply(): array + public function apply() : array { return [ '$mul' => [ diff --git a/src/Driver/MongoDb/Command/Operation/PipelineOperation.php b/src/Driver/MongoDb/Command/Operation/PipelineOperation.php index cbc2c90..580c914 100644 --- a/src/Driver/MongoDb/Command/Operation/PipelineOperation.php +++ b/src/Driver/MongoDb/Command/Operation/PipelineOperation.php @@ -4,5 +4,5 @@ interface PipelineOperation { - public function addToPipeline(): array; + public function addToPipeline() : array; } diff --git a/src/Driver/MongoDb/Command/Operation/Pop.php b/src/Driver/MongoDb/Command/Operation/Pop.php index 7295224..829650b 100644 --- a/src/Driver/MongoDb/Command/Operation/Pop.php +++ b/src/Driver/MongoDb/Command/Operation/Pop.php @@ -14,7 +14,7 @@ public function __construct(string $field) $this->field = $field; } - public function apply(): array + public function apply() : array { return [ '$pop' => [ diff --git a/src/Driver/MongoDb/Command/Operation/Rename.php b/src/Driver/MongoDb/Command/Operation/Rename.php index 880a1c5..d01fbc0 100644 --- a/src/Driver/MongoDb/Command/Operation/Rename.php +++ b/src/Driver/MongoDb/Command/Operation/Rename.php @@ -16,7 +16,7 @@ public function __construct(string $oldName, string $newName) $this->value = $newName; } - public function apply(): array + public function apply() : array { return [ '$rename' => [ diff --git a/src/Driver/MongoDb/Command/Operation/Select.php b/src/Driver/MongoDb/Command/Operation/Select.php index d3e179a..d9194ca 100644 --- a/src/Driver/MongoDb/Command/Operation/Select.php +++ b/src/Driver/MongoDb/Command/Operation/Select.php @@ -13,12 +13,12 @@ public function __construct(string ...$fields) } } - public function addToPipeline(): array + public function addToPipeline() : array { return ['$project' => $this->select]; } - public function apply(): array + public function apply() : array { return ['projection' => $this->select]; } diff --git a/src/Driver/MongoDb/Command/Operation/Set.php b/src/Driver/MongoDb/Command/Operation/Set.php index 1571eb1..c302fbb 100644 --- a/src/Driver/MongoDb/Command/Operation/Set.php +++ b/src/Driver/MongoDb/Command/Operation/Set.php @@ -16,7 +16,7 @@ public function __construct(string $field, $value) $this->value = $value; } - public function apply(): array + public function apply() : array { return [ '$set' => [ diff --git a/src/Driver/MongoDb/Command/Operation/Sort.php b/src/Driver/MongoDb/Command/Operation/Sort.php index 86ea4a1..4c27f8f 100644 --- a/src/Driver/MongoDb/Command/Operation/Sort.php +++ b/src/Driver/MongoDb/Command/Operation/Sort.php @@ -28,12 +28,12 @@ public function __construct(string ...$fields) } } - public function addToPipeline(): array + public function addToPipeline() : array { return ['$sort' => $this->sort]; } - public function apply(): array + public function apply() : array { return ['sort' => $this->sort]; } diff --git a/src/Driver/MongoDb/Command/Operation/UnsetField.php b/src/Driver/MongoDb/Command/Operation/UnsetField.php index 931fb44..f07e409 100644 --- a/src/Driver/MongoDb/Command/Operation/UnsetField.php +++ b/src/Driver/MongoDb/Command/Operation/UnsetField.php @@ -14,7 +14,7 @@ public function __construct(string ...$fields) $this->fields = $fields; } - public function apply(): array + public function apply() : array { $delete = []; foreach ($this->fields as $field) { diff --git a/src/Driver/MongoDb/Command/Operation/Unshift.php b/src/Driver/MongoDb/Command/Operation/Unshift.php index 4bdae17..284ce7d 100644 --- a/src/Driver/MongoDb/Command/Operation/Unshift.php +++ b/src/Driver/MongoDb/Command/Operation/Unshift.php @@ -14,7 +14,7 @@ public function __construct(string $field) $this->field = $field; } - public function apply(): array + public function apply() : array { return [ '$pop' => [ diff --git a/src/Driver/MongoDb/Command/Operation/UpdateDocument.php b/src/Driver/MongoDb/Command/Operation/UpdateDocument.php index e79f87c..e729b3f 100644 --- a/src/Driver/MongoDb/Command/Operation/UpdateDocument.php +++ b/src/Driver/MongoDb/Command/Operation/UpdateDocument.php @@ -14,7 +14,7 @@ public function __construct(array $document) $this->document = $document; } - public function apply(): array + public function apply() : array { return $this->document; } diff --git a/src/Driver/MongoDb/Command/Operation/UpdateOperation.php b/src/Driver/MongoDb/Command/Operation/UpdateOperation.php index 476cf4b..46e746b 100644 --- a/src/Driver/MongoDb/Command/Operation/UpdateOperation.php +++ b/src/Driver/MongoDb/Command/Operation/UpdateOperation.php @@ -4,5 +4,5 @@ interface UpdateOperation { - public function apply(): array; + public function apply() : array; } diff --git a/src/Driver/MongoDb/MongoConnection.php b/src/Driver/MongoDb/MongoConnection.php index c431dde..9252962 100644 --- a/src/Driver/MongoDb/MongoConnection.php +++ b/src/Driver/MongoDb/MongoConnection.php @@ -95,12 +95,12 @@ function (array $command) use (&$nativeCursor) { } } - public function getOptions(): ConnectionOptions + public function getOptions() : ConnectionOptions { return $this->options; } - public function connect(): void + public function connect() : void { $this->handler = new MongoDB\Driver\Manager( 'mongodb://' . $this->host . '/' . $this->options->getDatabase(), @@ -141,7 +141,7 @@ public function listCollections() : array return $collections; } - public function isConnected(): bool + public function isConnected() : bool { return $this->handler !== null; } diff --git a/src/Driver/MongoDb/MongoConnectionOptions.php b/src/Driver/MongoDb/MongoConnectionOptions.php index f0e7e4b..7ea1339 100644 --- a/src/Driver/MongoDb/MongoConnectionOptions.php +++ b/src/Driver/MongoDb/MongoConnectionOptions.php @@ -59,34 +59,30 @@ final class MongoConnectionOptions implements ConnectionOptions /** @var resource */ private $sslContext; - /** @var string */ - private $name; - public function __construct(string $database, string $username = null, string $password = null) { $this->username = $username; $this->password = $password; $this->database = $database; $this->readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY); - $this->name = 'default mongo connection'; } - public function setReadConcern(MongoDB\Driver\ReadConcern $concern): void + public function setReadConcern(MongoDB\Driver\ReadConcern $concern) : void { $this->readConcern = $concern; } - public function setWriteConcern(MongoDB\Driver\WriteConcern $concern): void + public function setWriteConcern(MongoDB\Driver\WriteConcern $concern) : void { $this->writeConcern = $concern; } - public function setReadPreference(MongoDB\Driver\ReadPreference $preference): void + public function setReadPreference(MongoDB\Driver\ReadPreference $preference) : void { $this->readPreference = $preference; } - public function setAppName(string $appName): void + public function setAppName(string $appName) : void { $this->appName = $appName; } @@ -97,28 +93,28 @@ public function setAppName(string $appName): void * @param array $options * @see https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst#auth-related-options */ - public function setAuth(string $mechanism, array $options = []): void + public function setAuth(string $mechanism, array $options = []) : void { $this->authMechanism = $mechanism; $this->authOptions = $options; } - public function setConnectionTimeout(int $milliseconds): void + public function setConnectionTimeout(int $milliseconds) : void { $this->connectTimeout = $milliseconds; } - public function setSocketTimeout(int $milliseconds): void + public function setSocketTimeout(int $milliseconds) : void { $this->socketTimeout = $milliseconds; } - public function setReplicaSet(string $name): void + public function setReplicaSet(string $name) : void { $this->replicaSet = $name; } - public function useSSL(string $pemFile, string $pemPassword = null, array $context = null): void + public function useSSL(string $pemFile, string $pemPassword = null, array $context = null) : void { if ($context !== null) { $this->sslContext = stream_context_create([ @@ -130,7 +126,7 @@ public function useSSL(string $pemFile, string $pemPassword = null, array $conte $this->sslPemPassword = $pemPassword; } - public function getURIOptions(): array + public function getURIOptions() : array { $options = []; if ($this->username !== null) { @@ -178,7 +174,7 @@ public function getURIOptions(): array return $options; } - public function getDriverOptions(): array + public function getDriverOptions() : array { if (!$this->sslPemFile) { return []; @@ -198,7 +194,7 @@ public function getDriverOptions(): array return $options; } - public function getDatabase(): string + public function getDatabase() : string { return $this->database; } diff --git a/src/Driver/MongoDb/MongoCursor.php b/src/Driver/MongoDb/MongoCursor.php index 66099cc..2438102 100644 --- a/src/Driver/MongoDb/MongoCursor.php +++ b/src/Driver/MongoDb/MongoCursor.php @@ -52,7 +52,7 @@ public function __construct( $this->createIterator(); } - private function createIterator(): void + private function createIterator() : void { $this->cursorIterator = new IteratorIterator($this->baseCursor); $this->cursorIterator->rewind(); @@ -61,17 +61,17 @@ private function createIterator(): void } } - public function getId(): string + public function getId() : string { return (string) $this->baseCursor->getId(); } - public function getCommand(): Command + public function getCommand() : Command { return $this->command; } - public function getConnection(): Connection + public function getConnection() : Connection { return $this->connection; } @@ -103,28 +103,28 @@ public function current() return $this->current; } - public function next(): void + public function next() : void { $this->cursorIterator->next(); $this->current = $this->fetch(); } - public function key(): int + public function key() : int { return $this->cursorIterator->key(); } - public function valid(): bool + public function valid() : bool { return $this->cursorIterator->valid(); } - public function rewind(): void + public function rewind() : void { $this->cursorIterator->rewind(); } - public function toArray(): array + public function toArray() : array { $result = iterator_to_array($this); $this->close(); @@ -132,12 +132,12 @@ public function toArray(): array return $result; } - public function isClosed(): bool + public function isClosed() : bool { return $this->baseCursor === null; } - public function close(): void + public function close() : void { $this->current = null; if ($this->baseCursor) { diff --git a/src/Exception/HydrationException.php b/src/Exception/HydrationException.php deleted file mode 100644 index 9d758bc..0000000 --- a/src/Exception/HydrationException.php +++ /dev/null @@ -1,48 +0,0 @@ -getTargetClass()}," . - "expected non null value, got null. Did you forget to make this field nullable?"); - } - - public static function forHydrationError(object $object, string $message) : self - { - $class = get_class($object); - return new self("Could not hydrate instance of {$class}," . - "are you sure that name mapping is set correctly? Failure message: {$message}"); - } - - public static function forUnallowedNullable() : self - { - return new self("Cannot extract/hydrate null value, field is not nullable." . - "Please set value or make field nullable."); - } -} diff --git a/src/Exception/SchemaException.php b/src/Exception/SchemaException.php deleted file mode 100644 index 749c59c..0000000 --- a/src/Exception/SchemaException.php +++ /dev/null @@ -1,21 +0,0 @@ -getNamingStrategy(); - - foreach ($schema as $name => $type) { - $key = $namingStrategy->map($name); - $value = $this->readProperty($object, $name); - if ($type instanceof CompositeType) { - $keys = []; - foreach ($type->getKeys($name) as $key) { - $keys[] = $namingStrategy->map($key); - } - - $output += array_combine( - $keys, - $type->extract($value) - ); - continue; - } - $output[$key] = $type->extract($value); - } - - return $output; - } - - protected function readProperty(object $object, string $property) - { - static $reader; - if ($reader === null) { - $reader = function ($name) { - return $this->$name; - }; - } - - $get = Closure::bind($reader, $object, $object); - - return $get($property); - } -} diff --git a/src/Hydration/GenericHydrator.php b/src/Hydration/GenericHydrator.php deleted file mode 100644 index 7bd0635..0000000 --- a/src/Hydration/GenericHydrator.php +++ /dev/null @@ -1,88 +0,0 @@ -identityMap = $identityMap; - } - - private function hydrateObject(Schema $schema, array $input, object $object) : object - { - $id = null; - if ($schema->definesId()) { - $idField = $schema->getNamingStrategy()->map($schema->getIdName()); - if (isset($input[$idField])) { - $id = $input[$idField]; - } - } - - if ($this->identityMap && $id && $this->identityMap->has($id)) { - return $this->identityMap->get($id); - } - - $namingStrategy = $schema->getNamingStrategy(); - - /** @var Type $type */ - foreach ($schema as $property => $type) { - if ($type instanceof CompositeType) { - $values = []; - foreach ($type->getKeys($property) as $key) { - $mappedKey = $namingStrategy->map($key); - $values[] = $input[$mappedKey] ?? null; - } - if (in_array(null, $values, true)) { - if (!$type->isNullable()) { - throw HydrationException::forNullHydration($schema, $property); - } - $this->writeProperty($object, $property, null); - continue; - } - $this->writeProperty($object, $property, $type->hydrate($values)); - continue; - } - - $value = $input[$namingStrategy->map($property)] ?? null; - if ($value === null) { - if (!$type instanceof NullableType || !$type->isNullable()) { - throw HydrationException::forNullHydration($schema, $property); - } - continue; - } - - $this->writeProperty($object, $property, $type->hydrate($value)); - } - - if ($this->identityMap && $id) { - $this->identityMap->attach($object, $id); - } - - return $object; - } - - protected function writeProperty(object $object, string $property, $value): void - { - static $writer; - if ($writer === null) { - $writer = function ($name, $value) { - $this->$name = $value; - }; - } - - $set = Closure::bind($writer, $object, $object); - $set($property, $value); - } -} diff --git a/src/Hydration/Hydrator.php b/src/Hydration/Hydrator.php deleted file mode 100644 index 8a8d98c..0000000 --- a/src/Hydration/Hydrator.php +++ /dev/null @@ -1,8 +0,0 @@ - [$className]] - ); - } catch (Throwable $exception) { - throw HydrationException::forInstantiationError($className, $exception->getMessage()); - } - - return clone self::$instances[$className]; - } -} diff --git a/src/Hydration/NamingStrategy/DirectNaming.php b/src/Hydration/NamingStrategy/DirectNaming.php deleted file mode 100644 index dc9122c..0000000 --- a/src/Hydration/NamingStrategy/DirectNaming.php +++ /dev/null @@ -1,11 +0,0 @@ -map = $map; - } - - public function map(string $name) : string - { - return isset($this->map[$name]) ? $this->map[$name] : $name; - } -} diff --git a/src/Hydration/NamingStrategy/NamingStrategy.php b/src/Hydration/NamingStrategy/NamingStrategy.php deleted file mode 100644 index b9b9738..0000000 --- a/src/Hydration/NamingStrategy/NamingStrategy.php +++ /dev/null @@ -1,15 +0,0 @@ -camelCaseToUnderscore($name); - } - - private function camelCaseToUnderscore(string $input): string - { - return ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '_$0', $input)), '_'); - } -} diff --git a/src/Hydration/ObjectHydrator.php b/src/Hydration/ObjectHydrator.php deleted file mode 100644 index ca09da7..0000000 --- a/src/Hydration/ObjectHydrator.php +++ /dev/null @@ -1,76 +0,0 @@ -setIdentityMap($identityMap ?? new IdentityMap()); - } - - public function hydrate(array $hash, object $object) : object - { - $className = get_class($object); - return $this->hydrateObject($this->getSchema($className), $hash, $object); - } - - public function extract(object $object): array - { - $className = get_class($object); - return $this->extractObject($this->getSchema($className), $object); - } - - public function addSchemaLoader(SchemaLoader $loader) : void - { - if (in_array($loader, $this->schemaLoaders, true)) { - return; - } - $this->schemaLoaders[] = $loader; - } - - public function addSchema(Schema $schema) : void - { - $this->schemaRegistry[$schema->getTargetClass()] = $schema; - } - - public function hasSchema(string $class) : bool - { - return isset($this->schemaRegistry[$class]) || $this->loadSchema($class); - } - - public function getSchema(string $class) : Schema - { - if (!$this->hasSchema($class)) { - throw SchemaException::forUndefinedSchema($class); - } - - return $this->schemaRegistry[$class]; - } - - private function loadSchema(string $class) : bool - { - foreach ($this->schemaLoaders as $loader) { - $schema = $loader->load($class); - if ($schema !== null) { - $this->schemaRegistry[$class] = $schema; - return true; - } - } - - return false; - } -} diff --git a/src/Hydration/Schema.php b/src/Hydration/Schema.php deleted file mode 100644 index 7d7bc3b..0000000 --- a/src/Hydration/Schema.php +++ /dev/null @@ -1,87 +0,0 @@ -getProperties()); - } - - public function getNamingStrategy() : NamingStrategy - { - if (null === $this->_namingStrategy) { - $this->_namingStrategy = new DirectNaming(); - } - - return $this->_namingStrategy; - } - - public function definesId() : bool - { - $this->build(); - return null !== $this->_idProperty; - } - - public function getIdName() : string - { - if (!$this->definesId()) { - return ''; - } - return $this->_idProperty; - } - - public function getProperties() : array - { - $this->build(); - return $this->_properties; - } - - public function count() : int - { - return count($this->getProperties()); - } - - private function build() : void - { - if (!empty($this->_properties)) { - return; - } - - $properties = get_object_vars($this); - foreach ($properties as $name => $type) { - if (!$type instanceof Type) { - continue; - } - if ($type instanceof IdType) { - $this->_idProperty = $name; - } - $this->_properties[$name] = $type; - } - } - - public function getSource() : ?string - { - return null; - } - - abstract public function getTargetClass() : string; -} diff --git a/src/Hydration/SchemaLoader.php b/src/Hydration/SchemaLoader.php deleted file mode 100644 index 5d050a3..0000000 --- a/src/Hydration/SchemaLoader.php +++ /dev/null @@ -1,8 +0,0 @@ -serialization = $serialization ? $serialization->getValue() : SerializationMethod::NONE; - } - - public function hydrate($value) : array - { - switch ($this->serialization) { - case SerializationMethod::JSON: - return json_decode($value, true); - case SerializationMethod::SERIALIZE: - return unserialize($value, ['allowed_classes' => false]); - case SerializationMethod::NONE: - default: - return $value; - } - } - - public function extract($value) - { - switch ($this->serialization) { - case SerializationMethod::JSON: - return json_encode($value); - case SerializationMethod::SERIALIZE: - return serialize($value); - case SerializationMethod::NONE: - default: - return $value; - } - } -} diff --git a/src/Hydration/Type/BooleanType.php b/src/Hydration/Type/BooleanType.php deleted file mode 100644 index b3d25f4..0000000 --- a/src/Hydration/Type/BooleanType.php +++ /dev/null @@ -1,16 +0,0 @@ -defaultTimezone = $defaultTimezone ?? new DateTimeZone('UTC'); - } - - public function hydrate($value): DateTimeInterface - { - $time = new DateTimeImmutable("@{$value[0]}"); - return $time->setTimezone(isset($value[1]) ? new DateTimeZone($value[1]) : $this->defaultTimezone); - } - - /** - * @param DateTimeInterface|null $object - * @return array - */ - public function extract($object): array - { - if ($object === null) { - return [null, $this->defaultTimezone]; - } - - return [ - $object->getTimestamp(), - $object->getTimezone()->getName() - ]; - } - - public function getKeys(string $prefix): array - { - return [ - $prefix . self::DATE_PART, - $prefix . self::TIMEZONE_PART, - ]; - } -} diff --git a/src/Hydration/Type/DateType.php b/src/Hydration/Type/DateType.php deleted file mode 100644 index 7169bdb..0000000 --- a/src/Hydration/Type/DateType.php +++ /dev/null @@ -1,35 +0,0 @@ -format = $format; - } - - public function hydrate($value): DateTimeInterface - { - $date = DateTime::createFromFormat($this->format, (string) $value); - $date->setTime(0, 0, 0); - - return $date; - } - - public function extract($value): ?string - { - if ($value instanceof DateTimeInterface) { - return $value->format($this->format); - } - - return null; - } -} diff --git a/src/Hydration/Type/DecimalType.php b/src/Hydration/Type/DecimalType.php deleted file mode 100644 index ea14cad..0000000 --- a/src/Hydration/Type/DecimalType.php +++ /dev/null @@ -1,57 +0,0 @@ -scale = $scale; - $this->precision = $precision; - $this->validate(); - } - - public function hydrate($value): string - { - return $this->formatNumber((string) $value); - } - - public function extract($value): string - { - return $this->formatNumber((string) $value); - } - - private function formatNumber(string $number): string - { - $parts = explode('.', $number); - $decimals = $this->precision - $this->scale; - - if (strlen($parts[0]) > $decimals) { - $parts[0] = str_repeat('9', $decimals); - } - - $number = $parts[0] . '.' . ($parts[1] ?? ''); - - return bcadd($number, '0', $this->scale); - } - - private function validate(): void - { - if ($this->scale > $this->precision) { - throw TypeException::forValidationFailure( - $this, - 'Attribute `scale` must be lower than `precision`.' - ); - } - } -} diff --git a/src/Hydration/Type/EmbedType.php b/src/Hydration/Type/EmbedType.php deleted file mode 100644 index 85764b2..0000000 --- a/src/Hydration/Type/EmbedType.php +++ /dev/null @@ -1,39 +0,0 @@ -schema = $schema; - } - - public function hydrate($value) : object - { - $object = Instantiator::instantiate($this->schema->getTargetClass()); - return $this->hydrateObject($this->schema, $value, $object); - } - - public function extract($value) : ?array - { - if ($value === null) { - if ($this->nullable) { - return null; - } - throw HydrationException::forUnallowedNullable(); - } - - return $this->extractObject($this->schema, $value); - } -} diff --git a/src/Hydration/Type/FloatType.php b/src/Hydration/Type/FloatType.php deleted file mode 100644 index f6b9daf..0000000 --- a/src/Hydration/Type/FloatType.php +++ /dev/null @@ -1,18 +0,0 @@ -nullable = true; - - return $this; - } - - public function isNullable(): bool - { - return $this->nullable; - } -} diff --git a/src/Hydration/Type/NullableType.php b/src/Hydration/Type/NullableType.php deleted file mode 100644 index 32ba083..0000000 --- a/src/Hydration/Type/NullableType.php +++ /dev/null @@ -1,9 +0,0 @@ -objects = []; } - public function attach(object $object, $id): void + public function attach(object $object, $id) : void { $this->objects[(string) $id] = $object; } - public function detach($id): void + public function detach($id) : void { if ($this->has($id)) { unset($this->objects[(string)$id]); } } - public function clear(): void + public function clear() : void { $this->objects = []; } - public function isEmpty(): bool + public function isEmpty() : bool { return empty($this->objects); } - public function has($id): bool + public function has($id) : bool { return isset($this->objects[(string) $id]); } - public function get($id): object + public function get($id) : object { if (!$this->has($id)) { throw IdentityMapException::forMissingObject($id); diff --git a/src/Repository.php b/src/Repository.php index ccfe9cd..028f0a6 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -8,25 +8,25 @@ interface Repository * @param mixed $id * @return object */ - public function get($id): object; + public function get($id) : object; /** * @param object ...$entity * @return object */ - public function create(object ...$entity): object; + public function create(object ...$entity) : object; /** * @param object ...$entity * @return object */ - public function remove(object ...$entity): object; + public function remove(object ...$entity) : object; /** * @param object ...$entity * @return object */ - public function update(object ...$entity): object; + public function update(object ...$entity) : object; /** * @return string diff --git a/tests/Fixtures/UserSchema.php b/tests/Fixtures/UserSchema.php deleted file mode 100644 index 528032e..0000000 --- a/tests/Fixtures/UserSchema.php +++ /dev/null @@ -1,71 +0,0 @@ -id = Type::id(); - $this->creationTime = Type::dateTime(); - $this->name = Type::embed(new class extends Schema { - protected $firstName; - protected $lastName; - - public function __construct() - { - $this->firstName = Type::string(); - $this->lastName = Type::string(); - } - - public function getTargetClass(): string - { - return UserName::class; - } - }); - $this->age = Type::integer(); - $this->favouriteNumber = Type::decimal()->nullable(); - $this->language = Type::string()->nullable(); - $this->email = Type::string()->nullable(); - $this->wallet = Type::embed(new class extends Schema { - protected $currency; - protected $amount; - public function __construct() - { - $this->currency = Type::string(); - $this->amount = Type::decimal(); - } - - public function getTargetClass() : string - { - return UserWallet::class; - } - }); - $this->eyeColor = Type::string()->nullable(); - $this->notListed = Type::string(); - } - - public function getTargetClass() : string - { - return User::class; - } - - public function getSource() : ?string - { - return 'users'; - } -} diff --git a/tests/Hydration/InstantiatorTest.php b/tests/Hydration/InstantiatorTest.php deleted file mode 100644 index f2ffb4e..0000000 --- a/tests/Hydration/InstantiatorTest.php +++ /dev/null @@ -1,32 +0,0 @@ -expectException(HydrationException::class); - Instantiator::instantiate('____NonExistingClass_____'); - } - - public function testFailInstantiationOnBuiltInClass() : void - { - $this->expectException(HydrationException::class); - Instantiator::instantiate(SimpleXMLElement::class); - } -} diff --git a/tests/Hydration/NamingStrategy/.phpunit.result.cache b/tests/Hydration/NamingStrategy/.phpunit.result.cache deleted file mode 100644 index d4230da..0000000 --- a/tests/Hydration/NamingStrategy/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":641:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:7:{s:63:"FatCode\Tests\Storage\Hydration\Property\MapNamingTest::testMap";d:0.007;s:67:"FatCode\Tests\Storage\Hydration\Property\MapNamingTest::testHydrate";d:0.008;s:67:"FatCode\Tests\Storage\Hydration\Property\MapNamingTest::testExtract";d:0.002;s:70:"FatCode\Tests\Storage\Hydration\Property\DirectNamingTest::testExtract";d:0.006;s:70:"FatCode\Tests\Storage\Hydration\Property\DirectNamingTest::testHydrate";d:0.002;s:74:"FatCode\Tests\Storage\Hydration\Property\UnderscoreNamingTest::testExtract";d:0.007;s:74:"FatCode\Tests\Storage\Hydration\Property\UnderscoreNamingTest::testHydrate";d:0.002;}}} \ No newline at end of file diff --git a/tests/Hydration/NamingStrategy/DirectNamingTest.php b/tests/Hydration/NamingStrategy/DirectNamingTest.php deleted file mode 100644 index 478a99b..0000000 --- a/tests/Hydration/NamingStrategy/DirectNamingTest.php +++ /dev/null @@ -1,15 +0,0 @@ -map('test1')); - } -} diff --git a/tests/Hydration/NamingStrategy/MapNamingTest.php b/tests/Hydration/NamingStrategy/MapNamingTest.php deleted file mode 100644 index a1047bf..0000000 --- a/tests/Hydration/NamingStrategy/MapNamingTest.php +++ /dev/null @@ -1,18 +0,0 @@ - 'mapped_name', 'otherName' => 'other_name']); - - self::assertSame('mapped_name', $namingStrategy->map('mappedName')); - self::assertSame('other_name', $namingStrategy->map('otherName')); - self::assertSame('unmapped_name', $namingStrategy->map('unmapped_name')); - } -} diff --git a/tests/Hydration/NamingStrategy/UnderscoreNamingTest.php b/tests/Hydration/NamingStrategy/UnderscoreNamingTest.php deleted file mode 100644 index 2c89add..0000000 --- a/tests/Hydration/NamingStrategy/UnderscoreNamingTest.php +++ /dev/null @@ -1,18 +0,0 @@ -map('mappedName')); - self::assertSame('other_name', $namingStrategy->map('otherName')); - self::assertSame('unmapped_name', $namingStrategy->map('unmappedName')); - } -} diff --git a/tests/Hydration/ObjectHydratorTest.php b/tests/Hydration/ObjectHydratorTest.php deleted file mode 100644 index eafb1f3..0000000 --- a/tests/Hydration/ObjectHydratorTest.php +++ /dev/null @@ -1,128 +0,0 @@ -addSchema($schema); - - self::assertSame($schema, $objectHydrator->getSchema($schema->getTargetClass())); - } - - public function testRegisterLoader() : void - { - $objectHydrator = new ObjectHydrator(); - self::assertFalse($objectHydrator->hasSchema(User::class)); - - $loader = new class implements SchemaLoader { - public function load(string $class): ?Schema - { - if ($class === User::class) { - return new UserSchema(); - } - - return null; - } - }; - $objectHydrator->addSchemaLoader($loader); - self::assertTrue($objectHydrator->hasSchema(User::class)); - self::assertInstanceOf(UserSchema::class, $objectHydrator->getSchema(User::class)); - } - - public function testFailGetOnUndefinedSchema() : void - { - $this->expectException(SchemaException::class); - $objectHydrator = new ObjectHydrator(); - $objectHydrator->getSchema('Something'); - } - - public function testHydrate() : void - { - $objectHydrator = new ObjectHydrator(); - $objectHydrator->addSchema(new UserSchema()); - $id = new ObjectId(); - $user = $objectHydrator->hydrate( - [ - 'id' => $id, - 'name' => [ - 'firstName' => 'John', - 'lastName' => 'Doe', - ], - 'wallet' => [ - 'amount' => '1000.20', - 'currency' => 'EUR', - ], - 'age' => '15', - 'creationTimeDate' => time(), - 'creationTimeTimezone' => 'Europe/Berlin', - ], - Instantiator::instantiate(User::class) - ); - - self::assertInstanceOf(User::class, $user); - self::assertSame(15, $user->getAge()); - self::assertInstanceOf(UserWallet::class, $user->getWallet()); - self::assertSame('1000.20', $user->getWallet()->getAmount()); - self::assertSame('EUR', $user->getWallet()->getCurrency()); - self::assertInstanceOf(UserName::class, $user->getName()); - self::assertSame('John', $user->getName()->getFirstName()); - self::assertSame('Doe', $user->getName()->getLastName()); - - $userFromIdentityMap = $objectHydrator->hydrate( - [ - 'id' => $id, - ], - Instantiator::instantiate(User::class) - ); - - self::assertSame($user, $userFromIdentityMap); - } - - public function testExtract() : void - { - $user = new User(new UserName('John', 'Doe'), new UserWallet('USD', '10.00')); - $id = $user->getId(); - $objectHydrator = new ObjectHydrator(); - $objectHydrator->addSchema(new UserSchema()); - - $extracted = $objectHydrator->extract($user); - - self::assertEquals( - [ - 'id' => $id, - 'name' => [ - 'firstName' => 'John', - 'lastName' => 'Doe', - ], - 'age' => 0, - 'favouriteNumber' => '0.00', - 'language' => '', - 'email' => '', - 'wallet' => [ - 'currency' => 'USD', - 'amount' => '10.00', - ], - 'eyeColor' => '', - 'creationTimeDate' => $user->getCreationTime()->getTimestamp(), - 'creationTimeTimezone' => 'UTC', - ], - $extracted - ); - } -} diff --git a/tests/Hydration/SchemaTest.php b/tests/Hydration/SchemaTest.php deleted file mode 100644 index 269904f..0000000 --- a/tests/Hydration/SchemaTest.php +++ /dev/null @@ -1,44 +0,0 @@ -getProperties(); - - self::assertCount(9, $schema); - self::assertInstanceOf(IdType::class, $properties['id']); - self::assertInstanceOf(StringType::class, $properties['language']); - self::assertInstanceOf(StringType::class, $properties['email']); - self::assertInstanceOf(IntegerType::class, $properties['age']); - self::assertInstanceOf(StringType::class, $properties['eyeColor']); - self::assertTrue($schema->definesId()); - } - - public function testIterateThroughSchema() : void - { - $schema = new UserSchema(); - - foreach ($schema as $name => $property) { - self::assertInstanceOf(Type::class, $property); - } - } - - public function testGetNamingStrategy() : void - { - $schema = new UserSchema(); - - self::assertInstanceOf(NamingStrategy::class, $schema->getNamingStrategy()); - } -} diff --git a/tests/Hydration/Type/ArrayTypeTest.php b/tests/Hydration/Type/ArrayTypeTest.php deleted file mode 100644 index 45afb67..0000000 --- a/tests/Hydration/Type/ArrayTypeTest.php +++ /dev/null @@ -1,37 +0,0 @@ -extract($exampleArray)); - - $type = new ArrayType(SerializationMethod::SERIALIZE()); - self::assertEquals(serialize($exampleArray), $type->extract($exampleArray)); - - $type = new ArrayType(SerializationMethod::JSON()); - self::assertEquals(json_encode($exampleArray), $type->extract($exampleArray)); - } - - public function testHydrate() : void - { - $type = new ArrayType(); - $exampleArray = [1, 2, 3]; - self::assertEquals($exampleArray, $type->hydrate($exampleArray)); - - $type = new ArrayType(SerializationMethod::SERIALIZE()); - self::assertEquals($exampleArray, $type->hydrate(serialize($exampleArray))); - - $type = new ArrayType(SerializationMethod::JSON()); - self::assertEquals($exampleArray, $type->hydrate(json_encode($exampleArray))); - } -} diff --git a/tests/Hydration/Type/BooleanTypeTest.php b/tests/Hydration/Type/BooleanTypeTest.php deleted file mode 100644 index d51a43b..0000000 --- a/tests/Hydration/Type/BooleanTypeTest.php +++ /dev/null @@ -1,55 +0,0 @@ -extract($given)); - } - - /** - * @param $given - * @param bool $expected - * @dataProvider provideHydrationValues - */ - public function testHydrate($given, bool $expected) : void - { - $boolean = new BooleanType(); - self::assertSame($expected, $boolean->hydrate($given)); - } - - public function provideExtractionValues() : array - { - return [ - [true, true], - [false, false], - [0, false], - [1, true], - ['false', true], - ['true', true], - ]; - } - - public function provideHydrationValues() : array - { - return [ - [true, true], - [false, false], - [0, false], - [1, true], - ['false', true], - ['true', true], - ]; - } -} diff --git a/tests/Hydration/Type/DateTimeTypeTest.php b/tests/Hydration/Type/DateTimeTypeTest.php deleted file mode 100644 index da5db1a..0000000 --- a/tests/Hydration/Type/DateTimeTypeTest.php +++ /dev/null @@ -1,67 +0,0 @@ -extract($given)); - } - - /** - * @param $given - * @param $expected - * @dataProvider provideHydrationValues - */ - public function testHydrate($given, $expected) : void - { - $type = new DateTimeType(); - self::assertEquals($expected, $type->hydrate($given)); - } - - public function testGetKeys() : void - { - $type = new DateTimeType(); - $keys = $type->getKeys('test'); - - self::assertSame('testDate', $keys[0]); - self::assertSame('testTimezone', $keys[1]); - } - - public function testNullable() : void - { - $type = new DateTimeType(); - - self::assertFalse($type->isNullable()); - $type->nullable(); - self::assertTrue($type->isNullable()); - } - - public function provideExtractionValues() : array - { - return [ - [new DateTime('@1553240567'), [1553240567, '+00:00']], - [new DateTime('2012-09-10 14:21:31', new DateTimeZone('Europe/Berlin')), [1347279691, 'Europe/Berlin']], - ]; - } - - public function provideHydrationValues() : array - { - return [ - [[1553240567, '+00:00'], new DateTime('@1553240567')], - [[1347279691, 'Europe/Berlin'], new DateTime('2012-09-10 14:21:31', new DateTimeZone('Europe/Berlin'))] - ]; - } -} diff --git a/tests/Hydration/Type/DateTypeTest.php b/tests/Hydration/Type/DateTypeTest.php deleted file mode 100644 index 7b41505..0000000 --- a/tests/Hydration/Type/DateTypeTest.php +++ /dev/null @@ -1,51 +0,0 @@ -extract($given)); - } - - /** - * @param $given - * @param $expected - * @dataProvider provideHydrationValues - */ - public function testHydrate($given, $expected) : void - { - $type = new DateType(); - $expected->setTime(0, 0, 0); - self::assertEquals($expected, $type->hydrate($given)); - } - - public function provideExtractionValues() : array - { - return [ - [new DateTime('@1553240567'), 20190322], - [new DateTime('2012-09-10 14:21:31'), 20120910], - ['2018-12-12', null] - ]; - } - - public function provideHydrationValues() : array - { - return [ - [20120910, new DateTime('2012-09-10')], - [20190322, new DateTime('@1553240567')] - ]; - } -} diff --git a/tests/Hydration/Type/DecimalTypeTest.php b/tests/Hydration/Type/DecimalTypeTest.php deleted file mode 100644 index ea79f46..0000000 --- a/tests/Hydration/Type/DecimalTypeTest.php +++ /dev/null @@ -1,64 +0,0 @@ -extract($given)); - } - - /** - * @param $given - * @param $expected - * @dataProvider provideHydrationValues - */ - public function testHydrate($given, $expected) : void - { - $type = new DecimalType(); - self::assertEquals($expected, $type->hydrate($given)); - } - - public function testFailOnInvalidInstantiation() : void - { - $this->expectException(TypeException::class); - new DecimalType(10, 2); - } - - public function provideExtractionValues() : array - { - return [ - [10.21, '10.21'], - [10.989, '10.98'], - [99999999.999, '99999999.99'], - [10, '10.00'], - ['10.202', '10.20'], - ['10.00', '10.00'], - ['10', '10.00'], - ]; - } - - public function provideHydrationValues() : array - { - return [ - [10.21, '10.21'], - [10.989, '10.98'], - [99999999.999, '99999999.99'], - [10, '10.00'], - ['10.202', '10.20'], - ['10.00', '10.00'], - ['10', '10.00'], - ]; - } -} diff --git a/tests/Hydration/Type/EmbedTypeTest.php b/tests/Hydration/Type/EmbedTypeTest.php deleted file mode 100644 index 60fbe97..0000000 --- a/tests/Hydration/Type/EmbedTypeTest.php +++ /dev/null @@ -1,43 +0,0 @@ -hydrate([ - 'id' => new ObjectId(), - 'name' => [ - 'firstName' => 'John', - 'lastName' => 'Doe', - ], - 'wallet' => [ - 'amount' => '1000.20', - 'currency' => 'EUR', - ], - 'age' => '15', - 'creationTimeDate' => time(), - 'creationTimeTimezone' => 'UTC', - ]); - - self::assertInstanceOf(User::class, $hydrated); - self::assertSame(15, $hydrated->getAge()); - self::assertInstanceOf(UserWallet::class, $hydrated->getWallet()); - self::assertSame('1000.20', $hydrated->getWallet()->getAmount()); - self::assertSame('EUR', $hydrated->getWallet()->getCurrency()); - self::assertInstanceOf(UserName::class, $hydrated->getName()); - self::assertSame('John', $hydrated->getName()->getFirstName()); - self::assertSame('Doe', $hydrated->getName()->getLastName()); - } -} diff --git a/tests/Hydration/Type/FloatTypeTest.php b/tests/Hydration/Type/FloatTypeTest.php deleted file mode 100644 index 7d877a0..0000000 --- a/tests/Hydration/Type/FloatTypeTest.php +++ /dev/null @@ -1,57 +0,0 @@ -extract($given)); - } - - /** - * @param $given - * @param $expected - * @dataProvider provideHydrationValues - */ - public function testHydrate($given, $expected) : void - { - $type = new FloatType(); - self::assertEquals($expected, $type->hydrate($given)); - } - - public function provideExtractionValues() : array - { - return [ - [10.21, 10.21], - [10.989, 10.989], - [99999999.999, 99999999.999], - [10, 10.00], - ['10.202', 10.202], - ['10.00', 10.00], - ['10', 10.00], - ]; - } - - public function provideHydrationValues() : array - { - return [ - [10.21, 10.21], - [10.989, 10.989], - [99999999.999, 99999999.999], - [10, 10.00], - ['10.202', 10.202], - ['10.00', 10.00], - ['10', 10.00], - ]; - } -} diff --git a/tests/Hydration/Type/IdTypeTest.php b/tests/Hydration/Type/IdTypeTest.php deleted file mode 100644 index b6b26b3..0000000 --- a/tests/Hydration/Type/IdTypeTest.php +++ /dev/null @@ -1,21 +0,0 @@ -extract(1)); - } - - public function testHydrate() : void - { - $type = new IdType(); - self::assertEquals(1, $type->hydrate(1)); - } -} diff --git a/tests/Hydration/Type/IntegerTypeTest.php b/tests/Hydration/Type/IntegerTypeTest.php deleted file mode 100644 index 67fe9c2..0000000 --- a/tests/Hydration/Type/IntegerTypeTest.php +++ /dev/null @@ -1,49 +0,0 @@ -extract($given)); - } - - /** - * @param $given - * @param $expected - * @dataProvider provideHydrationValues - */ - public function testHydrate($given, $expected) : void - { - $type = new IntegerType(); - self::assertEquals($expected, $type->hydrate($given)); - } - - public function provideExtractionValues() : array - { - return [ - [10.21, 10], - ['10', 10], - [9, 9], - ]; - } - - public function provideHydrationValues() : array - { - return [ - [10.21, 10], - ['10', 10], - [9, 9], - ]; - } -} diff --git a/tests/Hydration/Type/StringTypeTest.php b/tests/Hydration/Type/StringTypeTest.php deleted file mode 100644 index 967613a..0000000 --- a/tests/Hydration/Type/StringTypeTest.php +++ /dev/null @@ -1,67 +0,0 @@ -extract($given)); - } - - /** - * @param $given - * @param $expected - * @dataProvider provideHydrationValues - */ - public function testHydrate($given, $expected) : void - { - $type = new StringType(); - self::assertEquals($expected, $type->hydrate($given)); - } - - public function provideExtractionValues() : array - { - $test = new class { - public function __toString() : string - { - return 'test'; - } - }; - return [ - [10.21, '10.21'], - [true, '1'], - [false, ''], - [null, ''], - [10, '10'], - [$test, 'test'], - ]; - } - - public function provideHydrationValues() : array - { - $test = new class { - public function __toString() : string - { - return 'test'; - } - }; - return [ - [10.21, '10.21'], - [true, '1'], - [false, ''], - [null, ''], - [10, '10'], - [$test, 'test'], - ]; - } -} diff --git a/tests/IdentityMapTest.php b/tests/IdentityMapTest.php index e717f23..efddf2c 100644 --- a/tests/IdentityMapTest.php +++ b/tests/IdentityMapTest.php @@ -4,8 +4,9 @@ use FatCode\Storage\Exception\IdentityMapException; use FatCode\Storage\Hydration\IdentityMap; -use FatCode\Storage\Hydration\Instantiator; use FatCode\Tests\Storage\Fixtures\User; +use FatCode\Tests\Storage\Fixtures\UserName; +use FatCode\Tests\Storage\Fixtures\UserWallet; use PHPUnit\Framework\TestCase; use stdClass; @@ -13,7 +14,10 @@ final class IdentityMapTest extends TestCase { public function testAttach() : void { - $testInstance = Instantiator::instantiate(User::class); + $testInstance = new User( + new UserName('John', 'Doe'), + new UserWallet('GBP', '10.00') + ); $identityMap = new IdentityMap(); self::assertFalse($identityMap->has('testid')); @@ -32,7 +36,10 @@ public function testIsEmpty() : void public function testDetach() : void { - $testInstance = Instantiator::instantiate(User::class); + $testInstance = new User( + new UserName('John', 'Doe'), + new UserWallet('GBP', '10.00') + ); $identityMap = new IdentityMap(); self::assertFalse($identityMap->has('testid')); $identityMap->attach($testInstance, 'testid'); @@ -44,7 +51,10 @@ public function testDetach() : void public function testClear() : void { - $testInstance = Instantiator::instantiate(User::class); + $testInstance = new User( + new UserName('John', 'Doe'), + new UserWallet('GBP', '10.00') + ); $identityMap = new IdentityMap(); $identityMap->attach($testInstance, 'testid'); $identityMap->clear();