From fac26227327212aba346e79aa272d941d12e7ed4 Mon Sep 17 00:00:00 2001 From: Matthew Baggett Date: Mon, 24 Aug 2020 19:00:59 +0200 Subject: [PATCH] Initial testing code. Must prove Datetime works. --- .benzine.yml | 22 + .php_cs | 5 +- Makefile | 24 +- bin/find-autoloader.php | 6 +- bin/laminator | 2 +- composer.json | 9 +- docker-compose.yml | 21 +- phinx.php | 30 ++ phpunit.xml.dist | 57 +++ src/Laminator.php | 24 +- .../20200821163200_test_table_structure.php | 53 +++ test/app/src/App.php | 7 + .../src/Models/Base/BaseBlogPostsModel.php | 315 ++++++++++++++ .../src/Models/Base/BaseMigrationsModel.php | 280 +++++++++++++ test/app/src/Models/Base/BaseUsersModel.php | 352 ++++++++++++++++ test/app/src/Models/BlogPostsModel.php | 7 + test/app/src/Models/MigrationsModel.php | 7 + test/app/src/Models/UsersModel.php | 7 + .../Services/Base/BaseBlogPostsService.php | 222 ++++++++++ .../Services/Base/BaseMigrationsService.php | 218 ++++++++++ .../src/Services/Base/BaseUsersService.php | 218 ++++++++++ test/app/src/Services/BlogPostsService.php | 7 + test/app/src/Services/MigrationsService.php | 7 + test/app/src/Services/UsersService.php | 7 + .../Base/BaseBlogPostsTableGateway.php | 105 +++++ .../Base/BaseMigrationsTableGateway.php | 95 +++++ .../Base/BaseUsersTableGateway.php | 92 ++++ .../TableGateways/BlogPostsTableGateway.php | 7 + .../TableGateways/MigrationsTableGateway.php | 7 + .../src/TableGateways/UsersTableGateway.php | 7 + .../tests/Models/Generated/BlogPostsTest.php | 212 ++++++++++ .../tests/Models/Generated/MigrationsTest.php | 180 ++++++++ test/app/tests/Models/Generated/UsersTest.php | 246 +++++++++++ .../Services/Generated/BlogPostsTest.php | 393 ++++++++++++++++++ .../Services/Generated/MigrationsTest.php | 393 ++++++++++++++++++ .../tests/Services/Generated/UsersTest.php | 369 ++++++++++++++++ 36 files changed, 3989 insertions(+), 24 deletions(-) create mode 100644 .benzine.yml create mode 100644 phinx.php create mode 100644 phpunit.xml.dist create mode 100644 test/Migrations/20200821163200_test_table_structure.php create mode 100644 test/app/src/App.php create mode 100755 test/app/src/Models/Base/BaseBlogPostsModel.php create mode 100755 test/app/src/Models/Base/BaseMigrationsModel.php create mode 100755 test/app/src/Models/Base/BaseUsersModel.php create mode 100755 test/app/src/Models/BlogPostsModel.php create mode 100755 test/app/src/Models/MigrationsModel.php create mode 100755 test/app/src/Models/UsersModel.php create mode 100755 test/app/src/Services/Base/BaseBlogPostsService.php create mode 100755 test/app/src/Services/Base/BaseMigrationsService.php create mode 100755 test/app/src/Services/Base/BaseUsersService.php create mode 100755 test/app/src/Services/BlogPostsService.php create mode 100755 test/app/src/Services/MigrationsService.php create mode 100755 test/app/src/Services/UsersService.php create mode 100755 test/app/src/TableGateways/Base/BaseBlogPostsTableGateway.php create mode 100755 test/app/src/TableGateways/Base/BaseMigrationsTableGateway.php create mode 100755 test/app/src/TableGateways/Base/BaseUsersTableGateway.php create mode 100755 test/app/src/TableGateways/BlogPostsTableGateway.php create mode 100755 test/app/src/TableGateways/MigrationsTableGateway.php create mode 100755 test/app/src/TableGateways/UsersTableGateway.php create mode 100755 test/app/tests/Models/Generated/BlogPostsTest.php create mode 100755 test/app/tests/Models/Generated/MigrationsTest.php create mode 100755 test/app/tests/Models/Generated/UsersTest.php create mode 100755 test/app/tests/Services/Generated/BlogPostsTest.php create mode 100755 test/app/tests/Services/Generated/MigrationsTest.php create mode 100755 test/app/tests/Services/Generated/UsersTest.php diff --git a/.benzine.yml b/.benzine.yml new file mode 100644 index 0000000..c3b8d4b --- /dev/null +++ b/.benzine.yml @@ -0,0 +1,22 @@ +application: + name: Test + core: Benzine\ORM\Tests\App + default_access: public + debug: true + root: /app/test/app +laminator: + templates: + - Models + - Services + #- Controllers + #- Endpoints + #- Routes +databases: + default: + type: mysql + host: $MYSQL_HOST + username: $MYSQL_USER + password: $MYSQL_PASSWORD + database: $MYSQL_DATABASE + charset: utf8mb4 + diff --git a/.php_cs b/.php_cs index b172890..765ef31 100644 --- a/.php_cs +++ b/.php_cs @@ -6,9 +6,10 @@ if (!defined('__PHPCS_ROOT__')) { } $directories = [ - __PHPCS_ROOT__.'/src', __PHPCS_ROOT__.'/bin', - __PHPCS_ROOT__.'/tests', + __PHPCS_ROOT__.'/src', + __PHPCS_ROOT__.'/test/src', + __PHPCS_ROOT__.'/test/tests', ]; if (isset($additionalDirectories)) { diff --git a/Makefile b/Makefile index dc603af..a74ba45 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,24 @@ -test-blog-app: +SHELL:=/bin/bash +UID:=$(shell id -u) +DC_RUN:=docker-compose run -u ${UID} --rm + +clear: clear; - CURRENT_UID=`id -u`:`id -g` docker-compose run blog-app ./src/Generator/Laminator --workdir=./examples/blog-app setup: - composer install + docker-compose down -v + ${DC_RUN} test composer install + ${DC_RUN} test sleep 15 -test: setup test-blog-app \ No newline at end of file +migrate: + ${DC_RUN} test vendor/bin/phinx rollback + ${DC_RUN} test vendor/bin/phinx migrate + +regen: + ${DC_RUN} test bin/laminator + +clean: + ${DC_RUN} test vendor/bin/php-cs-fixer fix + +test: clear setup migrate regen clean + ${DC_RUN} test vendor/bin/phpunit \ No newline at end of file diff --git a/bin/find-autoloader.php b/bin/find-autoloader.php index 03ffc8c..2d78d52 100644 --- a/bin/find-autoloader.php +++ b/bin/find-autoloader.php @@ -1,6 +1,6 @@ isDir() && 'vendor' == $fileInfo->getFilename()) { - define("VENDOR_PATH", $fileInfo->getRealPath()); - require_once VENDOR_PATH . '/autoload.php'; + define('VENDOR_PATH', $fileInfo->getRealPath()); + require_once VENDOR_PATH.'/autoload.php'; return; } diff --git a/bin/laminator b/bin/laminator index 3ee43c9..da4c942 100755 --- a/bin/laminator +++ b/bin/laminator @@ -11,4 +11,4 @@ App::Instance() ->getApp() ->getContainer() ->get(Laminator::class) - ->makeLaminator(false); + ->makeLaminator(); diff --git a/composer.json b/composer.json index eb10060..159d983 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ ], "require": { "php": ">=7.4", + "ext-simplexml": "*", "laminas/laminas-db": "^2.11.1", "robmorgan/phinx": "^0.12.1", "mattketmo/camel": "^1.1", @@ -19,10 +20,16 @@ "gone.io/twig-extension-inflection": "^1.0", "gone.io/uuid": "^2.3" }, + "require-dev": { + "benzine/core": "dev-master", + "phpstan/phpstan": "^0.12", + "phpunit/phpcov": "^7.0", + "phpunit/phpunit": "^9.2" + }, "autoload": { "psr-4": { "Benzine\\ORM\\": "src", - "Benzine\\ORM\\Tests\\": "tests/" + "Benzine\\ORM\\Tests\\": "test/app/src" } }, "bin": [ diff --git a/docker-compose.yml b/docker-compose.yml index bdfb91b..3d5b98b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,23 +1,30 @@ version: "2.3" services: - blog-app: - image: gone/php:nginx + test: + image: gone/php:cli-7.4 volumes: - ./:/app depends_on: mysql: condition: service_healthy - user: ${CURRENT_UID} + redis: + condition: service_started + environment: + MYSQL_HOST: mysql + MYSQL_USER: testuser + MYSQL_PASSWORD: m4&ChangeMe^eN + MYSQL_DATABASE: blog + + redis: + image: redis mysql: image: benzine/mariadb:10.4 environment: MYSQL_ROOT_PASSWORD: ChangeMe - MYSQL_USER: bloguser + MYSQL_USER: testuser MYSQL_PASSWORD: m4&ChangeMe^eN MYSQL_DATABASE: blog ports: - - 127.0.0.1:3306:3306 - volumes: - - ./examples/blog-app/blog.sql:/docker-entrypoint-initdb.d/blog.sql \ No newline at end of file + - 127.0.0.1:3306:3306 \ No newline at end of file diff --git a/phinx.php b/phinx.php new file mode 100644 index 0000000..03fcdcb --- /dev/null +++ b/phinx.php @@ -0,0 +1,30 @@ + [ + 'migrations' => '%%PHINX_CONFIG_DIR%%/test/Migrations', + 'seeds' => '%%PHINX_CONFIG_DIR%%/test/Seeds' + ], + 'environments' => [ + 'default_environment' => 'testing', + 'default_migration_table' => 'Migrations', + + 'testing' => [ + 'adapter' => 'mysql', + 'host' => $environment['MYSQL_HOST'], + 'name' => $environment['MYSQL_DATABASE'], + 'user' => $environment['MYSQL_USER'], + 'pass' => $environment['MYSQL_PASSWORD'], + 'port' => '3306', + 'charset' => 'utf8', + ] + ], + 'version_order' => 'creation' +]; + +\Kint::dump($phinxConf); +return $phinxConf; \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..de8c44b --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,57 @@ + + + + + + + + + + test/app/tests/Models + + + test/app/tests/Services + + + + + + + + ./src + + ./vendor + ./views + ./assets + ./bin + ./tests + + + + + + + + + diff --git a/src/Laminator.php b/src/Laminator.php index ab5e043..877a8d0 100644 --- a/src/Laminator.php +++ b/src/Laminator.php @@ -2,6 +2,7 @@ namespace Benzine\ORM; +use Benzine\App; use Benzine\Configuration\Configuration; use Benzine\Configuration\Exceptions\Exception; use Benzine\Exceptions\BenzineException; @@ -17,9 +18,11 @@ use DirectoryIterator; use Gone\Twig\InflectionExtension; use Gone\Twig\TransformExtension; use Laminas\Stdlib\ConsoleHelper; +use Twig\Environment as TwigEnvironment; use Twig\Error\LoaderError; use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; +use Twig\Loader\FilesystemLoader as TwigFileSystemLoader; class Laminator { @@ -39,8 +42,8 @@ class Laminator 'clean' => [], ]; private static bool $useClassPrefixes = false; - private \Twig\Loader\FilesystemLoader $loader; - private \Twig\Environment $twig; + private TwigFileSystemLoader $loader; + private TwigEnvironment $twig; private Databases $databases; private array $ignoredTables = []; private \SimpleXMLElement $coverageReport; @@ -82,6 +85,10 @@ class Laminator } } + if (self::$benzineConfig->has(ConfigurationService::KEY_APP_ROOT)) { + $this->setWorkPath(self::$benzineConfig->get(ConfigurationService::KEY_APP_ROOT)); + } + $this->loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/Generator/templates'); $this->twig = new \Twig\Environment($this->loader, ['debug' => true]); $this->twig->addExtension(new \Twig\Extension\DebugExtension()); @@ -126,6 +133,13 @@ class Laminator return $this->workPath; } + public function setWorkPath(string $workPath): self + { + $this->workPath = $workPath; + + return $this; + } + public function exceptionHandler($exception): void { // UHOH exception handler @@ -153,11 +167,9 @@ class Laminator /** * @return \Slim\App */ - public function getApp() + public function _CORE() { - $instanceClass = APP_CORE_NAME; - - return $instanceClass::Instance() + return App::Instance() ->loadAllRoutes() ->getApp() ; diff --git a/test/Migrations/20200821163200_test_table_structure.php b/test/Migrations/20200821163200_test_table_structure.php new file mode 100644 index 0000000..a6f19488 --- /dev/null +++ b/test/Migrations/20200821163200_test_table_structure.php @@ -0,0 +1,53 @@ +table('Users', ['id' => 'userId']) + ->addColumn('name', 'string', ['length' => 32]) + ->addIndex(['name'], ['unique' => true]) + ->addColumn('email', 'string', ['length' => 320]) + ->addColumn('created', 'timestamp') + ->create() + ; + + $this->table('BlogPosts', ['id' => 'blogPostId']) + ->addColumn('title', 'string', ['length' => 64]) + ->addIndex(['title'], ['unique' => true]) + ->addColumn('description', 'text') + ->addColumn('userId', 'integer') + ->addForeignKey('userId', 'Users', 'userId') + ->addColumn('created', 'timestamp') + ->create() + ; + } +} diff --git a/test/app/src/App.php b/test/app/src/App.php new file mode 100644 index 0000000..a55ce4a --- /dev/null +++ b/test/app/src/App.php @@ -0,0 +1,7 @@ + 'blogPostId', + ]; + + protected array $_autoincrement_keys = [ + 'blogPostId' => 'blogPostId', + ]; + + // PHPType: int. DBType: int. Max Length: 2147483647. + protected ?int $blogPostId = null; + + // PHPType: string. DBType: varchar. Max Length: . + protected ?string $title = null; + + // PHPType: string. DBType: text. Max Length: . + protected ?string $description = null; + + // PHPType: int. DBType: int. Max Length: 2147483647. + protected ?int $userId = null; + + // PHPType: string. DBType: timestamp. Max Length: . + protected ?string $created = null; + + + private Services\UsersService $usersService; + + /** Caching entities **/ + protected array $cache = []; + protected ?Models\UsersModel $cachedUserObject; + + /** + * Lazy loading function to get an instance of Services\BlogPostsService + * + * @return Services\UsersService + */ + protected function getUsersService() : Services\UsersService + { + if (!isset($this->usersService)){ + $this->usersService = App::DI(Services\UsersService::class); + } + + return $this->usersService; + } + + + /** + * @param array $data an array of a Models\BlogPostsModel's properties + * + * @return Models\BlogPostsModel + */ + public static function factory(array $data = []) + { + return parent::factory($data); + } + + /** + * Returns an array describing the properties of this model. + * + * @return array + */ + public function getPropertyMeta(): array + { + $usersService = App::DI(Services\BlogPostsService::class); + return [ + self::FIELD_BLOGPOSTID => [ + 'type' => self::TYPE_BLOGPOSTID, + ], + self::FIELD_TITLE => [ + 'type' => self::TYPE_TITLE, + 'length' => 64, + ], + self::FIELD_DESCRIPTION => [ + 'type' => self::TYPE_DESCRIPTION, + 'length' => 65535, + ], + self::FIELD_USERID => [ + 'type' => self::TYPE_USERID, + 'remoteOptionsLoader' => $usersService->getAll(), + ], + self::FIELD_CREATED => [ + 'type' => self::TYPE_CREATED, + ], + ]; + } + + public function getBlogPostId(): ?int + { + return $this->blogPostId; + } + + /** + * @param int $blogPostId + * + * @return self + */ + public function setBlogPostId(int $blogPostId = null): self + { + $this->blogPostId = $blogPostId; + + return $this; + } + + public function getTitle(): ?string + { + return $this->title; + } + + /** + * @param string $title + * + * @return self + */ + public function setTitle(string $title = null): self + { + $this->title = $title; + + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + + /** + * @param string $description + * + * @return self + */ + public function setDescription(string $description = null): self + { + $this->description = $description; + + return $this; + } + + public function getUserId(): ?int + { + return $this->userId; + } + + /** + * @param int $userId + * + * @return self + */ + public function setUserId(int $userId = null): self + { + $this->userId = $userId; + + return $this; + } + + public function getCreated(): ?string + { + return $this->created; + } + + /** + * @param string $created + * + * @return self + */ + public function setCreated(string $created = null): self + { + $this->created = $created; + + return $this; + } + + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function fetchUserObject(): ?Models\UsersModel + { + if (!isset($this->cachedUserObject)){ + $this->cachedUserObject = $this->getUsersService() + ->getByField('userId', $this->getUserId()); + } + return $this->cachedUserObject; + } + + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function save(): Models\BlogPostsModel + { + /** @var TableGateways\BlogPostsTableGateway $tableGateway */ + $tableGateway = App::DI(TableGateways\BlogPostsTableGateway::class); + + return $tableGateway->save($this); + } + + /** + * Destroy the current record. + * Returns the number of affected rows. + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function destroy(): int + { + /** @var TableGateways\BlogPostsTableGateway $tableGateway */ + $tableGateway = App::DI(TableGateways\BlogPostsTableGateway::class); + + return $tableGateway->delete($this->getPrimaryKeys_dbColumns()); + } + + /** + * Destroy the current record, and any dependencies upon it, recursively. + * Returns the number of affected rows. + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function destroyThoroughly(): int + { + return $this->destroy(); + } + + /** + * Provides an array of all properties in this model. + * + * @return string[] + */ + public function getListOfProperties(): array + { + return [ + 'blogPostId' => 'blogPostId', + 'title' => 'title', + 'description' => 'description', + 'userId' => 'userId', + 'created' => 'created', + ]; + } + + /** + * Take an input array $data, and turn that array into a hydrated object. + * + * This has been re-written to be as permissive as possible with loading in data. This at some point will need to + * be re-re-written as a less messy solution (ie: picking one input field style and sticking with it) + * + * @todo re-rewrite this: pick one input field style and stick with it + * + * @param array $data dehydated object array + * + * @return Models\BlogPostsModel + */ + public function exchangeArray(array $data): self + { + if (isset($data['blogPostId'])) $this->setBlogPostId($data['blogPostId']); + if (isset($data['blogPostId'])) $this->setBlogPostId($data['blogPostId']); + if (isset($data['BlogPostId'])) $this->setBlogPostId($data['BlogPostId']); + if (isset($data['title'])) $this->setTitle($data['title']); + if (isset($data['title'])) $this->setTitle($data['title']); + if (isset($data['Title'])) $this->setTitle($data['Title']); + if (isset($data['description'])) $this->setDescription($data['description']); + if (isset($data['description'])) $this->setDescription($data['description']); + if (isset($data['Description'])) $this->setDescription($data['Description']); + if (isset($data['userId'])) $this->setUserId($data['userId']); + if (isset($data['userId'])) $this->setUserId($data['userId']); + if (isset($data['UserId'])) $this->setUserId($data['UserId']); + if (isset($data['created'])) $this->setCreated($data['created']); + if (isset($data['created'])) $this->setCreated($data['created']); + if (isset($data['Created'])) $this->setCreated($data['Created']); + return $this; + } + +} diff --git a/test/app/src/Models/Base/BaseMigrationsModel.php b/test/app/src/Models/Base/BaseMigrationsModel.php new file mode 100755 index 0000000..5057f54 --- /dev/null +++ b/test/app/src/Models/Base/BaseMigrationsModel.php @@ -0,0 +1,280 @@ + 'version', + ]; + + // PHPType: int. DBType: bigint. Max Length: 9223372036854775807. + protected ?int $version = null; + + // PHPType: string. DBType: varchar. Max Length: . + protected ?string $migration_name = null; + + // PHPType: string. DBType: timestamp. Max Length: . + protected ?string $start_time = null; + + // PHPType: string. DBType: timestamp. Max Length: . + protected ?string $end_time = null; + + // PHPType: int. DBType: tinyint. Max Length: 127. + protected ?int $breakpoint = null; + + + + /** Caching entities **/ + protected array $cache = []; + + + + /** + * @param array $data an array of a Models\MigrationsModel's properties + * + * @return Models\MigrationsModel + */ + public static function factory(array $data = []) + { + return parent::factory($data); + } + + /** + * Returns an array describing the properties of this model. + * + * @return array + */ + public function getPropertyMeta(): array + { + return [ + self::FIELD_VERSION => [ + 'type' => self::TYPE_VERSION, + ], + self::FIELD_MIGRATION_NAME => [ + 'type' => self::TYPE_MIGRATION_NAME, + 'length' => 100, + ], + self::FIELD_START_TIME => [ + 'type' => self::TYPE_START_TIME, + ], + self::FIELD_END_TIME => [ + 'type' => self::TYPE_END_TIME, + ], + self::FIELD_BREAKPOINT => [ + 'type' => self::TYPE_BREAKPOINT, + ], + ]; + } + + public function getVersion(): ?int + { + return $this->version; + } + + /** + * @param int $version + * + * @return self + */ + public function setVersion(int $version = null): self + { + $this->version = $version; + + return $this; + } + + public function getMigration_name(): ?string + { + return $this->migration_name; + } + + /** + * @param string $migration_name + * + * @return self + */ + public function setMigration_name(string $migration_name = null): self + { + $this->migration_name = $migration_name; + + return $this; + } + + public function getStart_time(): ?string + { + return $this->start_time; + } + + /** + * @param string $start_time + * + * @return self + */ + public function setStart_time(string $start_time = null): self + { + $this->start_time = $start_time; + + return $this; + } + + public function getEnd_time(): ?string + { + return $this->end_time; + } + + /** + * @param string $end_time + * + * @return self + */ + public function setEnd_time(string $end_time = null): self + { + $this->end_time = $end_time; + + return $this; + } + + public function getBreakpoint(): ?int + { + return $this->breakpoint; + } + + /** + * @param int $breakpoint + * + * @return self + */ + public function setBreakpoint(int $breakpoint = null): self + { + $this->breakpoint = $breakpoint; + + return $this; + } + + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function save(): Models\MigrationsModel + { + /** @var TableGateways\MigrationsTableGateway $tableGateway */ + $tableGateway = App::DI(TableGateways\MigrationsTableGateway::class); + + return $tableGateway->save($this); + } + + /** + * Destroy the current record. + * Returns the number of affected rows. + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function destroy(): int + { + /** @var TableGateways\MigrationsTableGateway $tableGateway */ + $tableGateway = App::DI(TableGateways\MigrationsTableGateway::class); + + return $tableGateway->delete($this->getPrimaryKeys_dbColumns()); + } + + /** + * Destroy the current record, and any dependencies upon it, recursively. + * Returns the number of affected rows. + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function destroyThoroughly(): int + { + return $this->destroy(); + } + + /** + * Provides an array of all properties in this model. + * + * @return string[] + */ + public function getListOfProperties(): array + { + return [ + 'version' => 'version', + 'migration_name' => 'migration_name', + 'start_time' => 'start_time', + 'end_time' => 'end_time', + 'breakpoint' => 'breakpoint', + ]; + } + + /** + * Take an input array $data, and turn that array into a hydrated object. + * + * This has been re-written to be as permissive as possible with loading in data. This at some point will need to + * be re-re-written as a less messy solution (ie: picking one input field style and sticking with it) + * + * @todo re-rewrite this: pick one input field style and stick with it + * + * @param array $data dehydated object array + * + * @return Models\MigrationsModel + */ + public function exchangeArray(array $data): self + { + if (isset($data['version'])) $this->setVersion($data['version']); + if (isset($data['version'])) $this->setVersion($data['version']); + if (isset($data['Version'])) $this->setVersion($data['Version']); + if (isset($data['migration_name'])) $this->setMigration_name($data['migration_name']); + if (isset($data['migration_name'])) $this->setMigration_name($data['migration_name']); + if (isset($data['Migration_name'])) $this->setMigration_name($data['Migration_name']); + if (isset($data['start_time'])) $this->setStart_time($data['start_time']); + if (isset($data['start_time'])) $this->setStart_time($data['start_time']); + if (isset($data['Start_time'])) $this->setStart_time($data['Start_time']); + if (isset($data['end_time'])) $this->setEnd_time($data['end_time']); + if (isset($data['end_time'])) $this->setEnd_time($data['end_time']); + if (isset($data['End_time'])) $this->setEnd_time($data['End_time']); + if (isset($data['breakpoint'])) $this->setBreakpoint($data['breakpoint']); + if (isset($data['breakpoint'])) $this->setBreakpoint($data['breakpoint']); + if (isset($data['Breakpoint'])) $this->setBreakpoint($data['Breakpoint']); + return $this; + } + +} diff --git a/test/app/src/Models/Base/BaseUsersModel.php b/test/app/src/Models/Base/BaseUsersModel.php new file mode 100755 index 0000000..c653a54 --- /dev/null +++ b/test/app/src/Models/Base/BaseUsersModel.php @@ -0,0 +1,352 @@ + 'userId', + ]; + + protected array $_autoincrement_keys = [ + 'userId' => 'userId', + ]; + + // PHPType: int. DBType: int. Max Length: 2147483647. + protected ?int $userId = null; + + // PHPType: string. DBType: varchar. Max Length: . + protected ?string $name = null; + + // PHPType: string. DBType: varchar. Max Length: . + protected ?string $email = null; + + // PHPType: string. DBType: timestamp. Max Length: . + protected ?string $created = null; + + + private Services\BlogPostsService $blogPostsService; + + /** Caching entities **/ + protected array $cache = []; + protected ?Models\BlogPostsModel $cachedBlogPostObject; + + + /** + * Lazy loading function to get an instance of Services\BlogPostsService + * + * @return Services\BlogPostsService + */ + protected function getBlogPostsService() : Services\BlogPostsService + { + if (!isset($this->blogPostsService)){ + $this->blogPostsService = App::DI(Services\BlogPostsService::class); + } + + return $this->blogPostsService; + } + + /** + * @param array $data an array of a Models\UsersModel's properties + * + * @return Models\UsersModel + */ + public static function factory(array $data = []) + { + return parent::factory($data); + } + + /** + * Returns an array describing the properties of this model. + * + * @return array + */ + public function getPropertyMeta(): array + { + return [ + self::FIELD_USERID => [ + 'type' => self::TYPE_USERID, + ], + self::FIELD_NAME => [ + 'type' => self::TYPE_NAME, + 'length' => 32, + ], + self::FIELD_EMAIL => [ + 'type' => self::TYPE_EMAIL, + 'length' => 320, + ], + self::FIELD_CREATED => [ + 'type' => self::TYPE_CREATED, + ], + ]; + } + + public function getUserId(): ?int + { + return $this->userId; + } + + /** + * @param int $userId + * + * @return self + */ + public function setUserId(int $userId = null): self + { + $this->userId = $userId; + + return $this; + } + + public function getName(): ?string + { + return $this->name; + } + + /** + * @param string $name + * + * @return self + */ + public function setName(string $name = null): self + { + $this->name = $name; + + return $this; + } + + public function getEmail(): ?string + { + return $this->email; + } + + /** + * @param string $email + * + * @return self + */ + public function setEmail(string $email = null): self + { + $this->email = $email; + + return $this; + } + + public function getCreated(): ?string + { + return $this->created; + } + + /** + * @param string $created + * + * @return self + */ + public function setCreated(string $created = null): self + { + $this->created = $created; + + return $this; + } + + + /** + * Fetch a singular BlogPost that references this Models\UsersModel. + * + * Local class: BlogPosts + * Remote class: Users + * + * @param $orderBy string Column to order by. Recommended to use the Constants in Models\BlogPostsModel:: + * @param $orderDirection string Either "DESC" or "ASC". Recommend using Select::ORDER_ASCENDING or Select::ORDER_DESCENDING + * + * @return Models\BlogPostsModel|null + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function fetchBlogPostObject( + $orderBy = null, + $orderDirection='ASC' + ): ?Models\BlogPostsModel { + if (!isset($this->cachedBlogPostObject)){ + $this->cachedBlogPostObject = $this->getBlogPostsService() + ->getByField( + Models\UsersModel::FIELD_USERID, + $this->getUserId(), + $orderBy, + $orderDirection + ); + } + return $this->cachedBlogPostObject; + } + + /** + * Fetch all matching BlogPost that reference this Models\UsersModel. + * + * Local class: BlogPosts + * Remote class: Users + * + * @param $limit int Number to fetch maximum + * @param $orderBy string Column to order by. Recommended to use the Constants in Models\BlogPostsModel:: + * @param $orderDirection string Either "DESC" or "ASC". Recommend using Select::ORDER_ASCENDING or Select::ORDER_DESCENDING + * + * @return Models\BlogPostsModel[]|null + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function fetchBlogPostObjects( + int $limit = null, + string $orderBy = null, + string $orderDirection='ASC' + ): ?array { + if (!isset($this->cachedBlogPostObjects)){ + $this->cachedBlogPostObjects = $this + ->getBlogPostsService() + ->getManyByField( + Models\BlogPostsModel::FIELD_USERID, + $this->getUserId(), + $limit, + $orderBy, + $orderDirection + ); + } + + return $this->cachedBlogPostObjects; + } + + /** + * Count the number of matching BlogPost that reference this Models\UsersModel. + * Returns the number of objects found. + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function countBlogPostObjects(): int { + return $this + ->getBlogPostsService() + ->countByField( + Models\UsersModel::FIELD_USERID, + $this->getUserId() + ); + } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function save(): Models\UsersModel + { + /** @var TableGateways\UsersTableGateway $tableGateway */ + $tableGateway = App::DI(TableGateways\UsersTableGateway::class); + + return $tableGateway->save($this); + } + + /** + * Destroy the current record. + * Returns the number of affected rows. + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function destroy(): int + { + /** @var TableGateways\UsersTableGateway $tableGateway */ + $tableGateway = App::DI(TableGateways\UsersTableGateway::class); + + return $tableGateway->delete($this->getPrimaryKeys_dbColumns()); + } + + /** + * Destroy the current record, and any dependencies upon it, recursively. + * Returns the number of affected rows. + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function destroyThoroughly(): int + { + return $this->destroy(); + } + + /** + * Provides an array of all properties in this model. + * + * @return string[] + */ + public function getListOfProperties(): array + { + return [ + 'userId' => 'userId', + 'name' => 'name', + 'email' => 'email', + 'created' => 'created', + ]; + } + + /** + * Take an input array $data, and turn that array into a hydrated object. + * + * This has been re-written to be as permissive as possible with loading in data. This at some point will need to + * be re-re-written as a less messy solution (ie: picking one input field style and sticking with it) + * + * @todo re-rewrite this: pick one input field style and stick with it + * + * @param array $data dehydated object array + * + * @return Models\UsersModel + */ + public function exchangeArray(array $data): self + { + if (isset($data['userId'])) $this->setUserId($data['userId']); + if (isset($data['userId'])) $this->setUserId($data['userId']); + if (isset($data['UserId'])) $this->setUserId($data['UserId']); + if (isset($data['name'])) $this->setName($data['name']); + if (isset($data['name'])) $this->setName($data['name']); + if (isset($data['Name'])) $this->setName($data['Name']); + if (isset($data['email'])) $this->setEmail($data['email']); + if (isset($data['email'])) $this->setEmail($data['email']); + if (isset($data['Email'])) $this->setEmail($data['Email']); + if (isset($data['created'])) $this->setCreated($data['created']); + if (isset($data['created'])) $this->setCreated($data['created']); + if (isset($data['Created'])) $this->setCreated($data['Created']); + return $this; + } + +} diff --git a/test/app/src/Models/BlogPostsModel.php b/test/app/src/Models/BlogPostsModel.php new file mode 100755 index 0000000..8363c5c --- /dev/null +++ b/test/app/src/Models/BlogPostsModel.php @@ -0,0 +1,7 @@ +usersTableGateway = $usersTableGateway; + $this->blogPostsTableGateway = $blogPostsTableGateway; + } + + public function getNewTableGatewayInstance(): TableGateways\BlogPostsTableGateway + { + return $this->blogPostsTableGateway; + } + + public function getNewModelInstance($dataExchange = []): Models\BlogPostsModel + { + return $this->blogPostsTableGateway->getNewModelInstance($dataExchange); + } + + /** + * @param null|int $limit + * @param null|int $offset + * @param null|array|\Closure[] $wheres + * @param null|Expression|string $order + * @param null|string $orderDirection + * + * @return Models\BlogPostsModel[] + */ + public function getAll( + int $limit = null, + int $offset = null, + array $wheres = null, + $order = null, + string $orderDirection = null + ) { + return parent::getAll( + $limit, + $offset, + $wheres, + $order, + $orderDirection + ); + } + + + /** + * @param string $field + * @param $value + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * + * @return null|Models\BlogPostsModel + */ + public function getByField(string $field, $value, $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?Models\BlogPostsModel + { + /** @var TableGateways\BlogPostsTableGateway $blogPostsTable */ + $blogPostsTable = $this->getNewTableGatewayInstance(); + + return $blogPostsTable->getByField($field, $value, $orderBy, $orderDirection); + } + + /** + * @param string $field + * @param $value + * @param $limit int + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * + * @return null|Models\BlogPostsModel[] + */ + public function getManyByField(string $field, $value, int $limit = null, $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?array + { + /** @var TableGateways\BlogPostsTableGateway $blogPostsTable */ + $blogPostsTable = $this->getNewTableGatewayInstance(); + + return $blogPostsTable->getManyByField($field, $value, $limit, $orderBy, $orderDirection); + } + + /** + * @param string $field + * @param $value + * + * @return int + */ + public function countByField(string $field, $value): int + { + $blogPostsTable = $this->getNewTableGatewayInstance(); + + return $blogPostsTable->countByField($field, $value); + } + + /** + * @return Models\BlogPostsModel + */ + public function getRandom(): ?Models\BlogPostsModel + { + /** @var TableGateways\BlogPostsTableGateway $blogPostsTable */ + $blogPostsTable = $this->getNewTableGatewayInstance(); + + return $blogPostsTable->fetchRandom(); + } + + /** + * @param array|\Closure|Predicate\PredicateInterface|string|Where $keyValue + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * + * @return Models\BlogPostsModel + */ + public function getMatching($keyValue = [], $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?Models\BlogPostsModel + { + /** @var TableGateways\BlogPostsTableGateway $blogPostsTable */ + $blogPostsTable = $this->getNewTableGatewayInstance(); + + return $blogPostsTable->getMatching($keyValue, $orderBy, $orderDirection); + } + + /** + * @param array|\Closure|Predicate\PredicateInterface|string|Where $keyValue + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * @param $limit int Limit the number of matches returned + * + * @return Models\BlogPostsModel[] + */ + public function getManyMatching($keyValue = [], $orderBy = null, $orderDirection = Select::ORDER_ASCENDING, int $limit = null): ?array + { + /** @var TableGateways\BlogPostsTableGateway $blogPostsTable */ + $blogPostsTable = $this->getNewTableGatewayInstance(); + + return $blogPostsTable->getManyMatching($keyValue, $orderBy, $orderDirection, $limit); + } + + /** + * @param $dataExchange + * + * @return Models\BlogPostsModel + */ + public function createFromArray($dataExchange): Models\BlogPostsModel + { + /** @var TableGateways\BlogPostsTableGateway $blogPostsTable */ + $blogPostsTable = $this->getNewTableGatewayInstance(); + $blogPosts = $this->getNewModelInstance($dataExchange); + + return $blogPostsTable->save($blogPosts); + } + + + /** + * @param string $field + * @param mixed value + * + * @return int + */ + public function deleteByField(string $field, $value): int + { + /** @var TableGateways\BlogPostsTableGateway $blogPostsTable */ + $blogPostsTable = $this->getNewTableGatewayInstance(); + + return $blogPostsTable->delete([$field => $value]); + } + + public function getTermPlural(): string + { + return 'BlogPosts'; + } + + public function getTermSingular(): string + { + return 'BlogPosts'; + } + + /** + * Get a version of this object pre-populated with nonsense. + * + * @returns Models\BlogPostsModel + */ + public function getMockObject(): Models\BlogPostsModel + { + return $this->getNewTableGatewayInstance()->getNewMockModelInstance(); + } +} diff --git a/test/app/src/Services/Base/BaseMigrationsService.php b/test/app/src/Services/Base/BaseMigrationsService.php new file mode 100755 index 0000000..5ee5d3c --- /dev/null +++ b/test/app/src/Services/Base/BaseMigrationsService.php @@ -0,0 +1,218 @@ +migrationsTableGateway = $migrationsTableGateway; + } + + public function getNewTableGatewayInstance(): TableGateways\MigrationsTableGateway + { + return $this->migrationsTableGateway; + } + + public function getNewModelInstance($dataExchange = []): Models\MigrationsModel + { + return $this->migrationsTableGateway->getNewModelInstance($dataExchange); + } + + /** + * @param null|int $limit + * @param null|int $offset + * @param null|array|\Closure[] $wheres + * @param null|Expression|string $order + * @param null|string $orderDirection + * + * @return Models\MigrationsModel[] + */ + public function getAll( + int $limit = null, + int $offset = null, + array $wheres = null, + $order = null, + string $orderDirection = null + ) { + return parent::getAll( + $limit, + $offset, + $wheres, + $order, + $orderDirection + ); + } + + + /** + * @param string $field + * @param $value + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * + * @return null|Models\MigrationsModel + */ + public function getByField(string $field, $value, $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?Models\MigrationsModel + { + /** @var TableGateways\MigrationsTableGateway $migrationsTable */ + $migrationsTable = $this->getNewTableGatewayInstance(); + + return $migrationsTable->getByField($field, $value, $orderBy, $orderDirection); + } + + /** + * @param string $field + * @param $value + * @param $limit int + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * + * @return null|Models\MigrationsModel[] + */ + public function getManyByField(string $field, $value, int $limit = null, $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?array + { + /** @var TableGateways\MigrationsTableGateway $migrationsTable */ + $migrationsTable = $this->getNewTableGatewayInstance(); + + return $migrationsTable->getManyByField($field, $value, $limit, $orderBy, $orderDirection); + } + + /** + * @param string $field + * @param $value + * + * @return int + */ + public function countByField(string $field, $value): int + { + $migrationsTable = $this->getNewTableGatewayInstance(); + + return $migrationsTable->countByField($field, $value); + } + + /** + * @return Models\MigrationsModel + */ + public function getRandom(): ?Models\MigrationsModel + { + /** @var TableGateways\MigrationsTableGateway $migrationsTable */ + $migrationsTable = $this->getNewTableGatewayInstance(); + + return $migrationsTable->fetchRandom(); + } + + /** + * @param array|\Closure|Predicate\PredicateInterface|string|Where $keyValue + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * + * @return Models\MigrationsModel + */ + public function getMatching($keyValue = [], $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?Models\MigrationsModel + { + /** @var TableGateways\MigrationsTableGateway $migrationsTable */ + $migrationsTable = $this->getNewTableGatewayInstance(); + + return $migrationsTable->getMatching($keyValue, $orderBy, $orderDirection); + } + + /** + * @param array|\Closure|Predicate\PredicateInterface|string|Where $keyValue + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * @param $limit int Limit the number of matches returned + * + * @return Models\MigrationsModel[] + */ + public function getManyMatching($keyValue = [], $orderBy = null, $orderDirection = Select::ORDER_ASCENDING, int $limit = null): ?array + { + /** @var TableGateways\MigrationsTableGateway $migrationsTable */ + $migrationsTable = $this->getNewTableGatewayInstance(); + + return $migrationsTable->getManyMatching($keyValue, $orderBy, $orderDirection, $limit); + } + + /** + * @param $dataExchange + * + * @return Models\MigrationsModel + */ + public function createFromArray($dataExchange): Models\MigrationsModel + { + /** @var TableGateways\MigrationsTableGateway $migrationsTable */ + $migrationsTable = $this->getNewTableGatewayInstance(); + $migrations = $this->getNewModelInstance($dataExchange); + + return $migrationsTable->save($migrations); + } + + + /** + * @param string $field + * @param mixed value + * + * @return int + */ + public function deleteByField(string $field, $value): int + { + /** @var TableGateways\MigrationsTableGateway $migrationsTable */ + $migrationsTable = $this->getNewTableGatewayInstance(); + + return $migrationsTable->delete([$field => $value]); + } + + public function getTermPlural(): string + { + return 'Migrations'; + } + + public function getTermSingular(): string + { + return 'Migrations'; + } + + /** + * Get a version of this object pre-populated with nonsense. + * + * @returns Models\MigrationsModel + */ + public function getMockObject(): Models\MigrationsModel + { + return $this->getNewTableGatewayInstance()->getNewMockModelInstance(); + } +} diff --git a/test/app/src/Services/Base/BaseUsersService.php b/test/app/src/Services/Base/BaseUsersService.php new file mode 100755 index 0000000..c748356 --- /dev/null +++ b/test/app/src/Services/Base/BaseUsersService.php @@ -0,0 +1,218 @@ +usersTableGateway = $usersTableGateway; + } + + public function getNewTableGatewayInstance(): TableGateways\UsersTableGateway + { + return $this->usersTableGateway; + } + + public function getNewModelInstance($dataExchange = []): Models\UsersModel + { + return $this->usersTableGateway->getNewModelInstance($dataExchange); + } + + /** + * @param null|int $limit + * @param null|int $offset + * @param null|array|\Closure[] $wheres + * @param null|Expression|string $order + * @param null|string $orderDirection + * + * @return Models\UsersModel[] + */ + public function getAll( + int $limit = null, + int $offset = null, + array $wheres = null, + $order = null, + string $orderDirection = null + ) { + return parent::getAll( + $limit, + $offset, + $wheres, + $order, + $orderDirection + ); + } + + + /** + * @param string $field + * @param $value + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * + * @return null|Models\UsersModel + */ + public function getByField(string $field, $value, $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?Models\UsersModel + { + /** @var TableGateways\UsersTableGateway $usersTable */ + $usersTable = $this->getNewTableGatewayInstance(); + + return $usersTable->getByField($field, $value, $orderBy, $orderDirection); + } + + /** + * @param string $field + * @param $value + * @param $limit int + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * + * @return null|Models\UsersModel[] + */ + public function getManyByField(string $field, $value, int $limit = null, $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?array + { + /** @var TableGateways\UsersTableGateway $usersTable */ + $usersTable = $this->getNewTableGatewayInstance(); + + return $usersTable->getManyByField($field, $value, $limit, $orderBy, $orderDirection); + } + + /** + * @param string $field + * @param $value + * + * @return int + */ + public function countByField(string $field, $value): int + { + $usersTable = $this->getNewTableGatewayInstance(); + + return $usersTable->countByField($field, $value); + } + + /** + * @return Models\UsersModel + */ + public function getRandom(): ?Models\UsersModel + { + /** @var TableGateways\UsersTableGateway $usersTable */ + $usersTable = $this->getNewTableGatewayInstance(); + + return $usersTable->fetchRandom(); + } + + /** + * @param array|\Closure|Predicate\PredicateInterface|string|Where $keyValue + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * + * @return Models\UsersModel + */ + public function getMatching($keyValue = [], $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?Models\UsersModel + { + /** @var TableGateways\UsersTableGateway $usersTable */ + $usersTable = $this->getNewTableGatewayInstance(); + + return $usersTable->getMatching($keyValue, $orderBy, $orderDirection); + } + + /** + * @param array|\Closure|Predicate\PredicateInterface|string|Where $keyValue + * @param $orderBy string Field to sort by + * @param $orderDirection string Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING) + * @param $limit int Limit the number of matches returned + * + * @return Models\UsersModel[] + */ + public function getManyMatching($keyValue = [], $orderBy = null, $orderDirection = Select::ORDER_ASCENDING, int $limit = null): ?array + { + /** @var TableGateways\UsersTableGateway $usersTable */ + $usersTable = $this->getNewTableGatewayInstance(); + + return $usersTable->getManyMatching($keyValue, $orderBy, $orderDirection, $limit); + } + + /** + * @param $dataExchange + * + * @return Models\UsersModel + */ + public function createFromArray($dataExchange): Models\UsersModel + { + /** @var TableGateways\UsersTableGateway $usersTable */ + $usersTable = $this->getNewTableGatewayInstance(); + $users = $this->getNewModelInstance($dataExchange); + + return $usersTable->save($users); + } + + + /** + * @param string $field + * @param mixed value + * + * @return int + */ + public function deleteByField(string $field, $value): int + { + /** @var TableGateways\UsersTableGateway $usersTable */ + $usersTable = $this->getNewTableGatewayInstance(); + + return $usersTable->delete([$field => $value]); + } + + public function getTermPlural(): string + { + return 'Users'; + } + + public function getTermSingular(): string + { + return 'Users'; + } + + /** + * Get a version of this object pre-populated with nonsense. + * + * @returns Models\UsersModel + */ + public function getMockObject(): Models\UsersModel + { + return $this->getNewTableGatewayInstance()->getNewMockModelInstance(); + } +} diff --git a/test/app/src/Services/BlogPostsService.php b/test/app/src/Services/BlogPostsService.php new file mode 100755 index 0000000..bb5477a --- /dev/null +++ b/test/app/src/Services/BlogPostsService.php @@ -0,0 +1,7 @@ +usersTableGateway = $usersTableGateway; + $this->faker = $faker; + $this->databaseConnection = $databaseConnection; + $this->database = $this->databaseConnection->getDatabase('default'); + + $resultSetPrototype = new ResultSet(ResultSet::TYPE_ARRAYOBJECT, new $this->model()); + + parent::__construct($this->table, $this->database->getAdapter(), null, $resultSetPrototype); + } + + /** + * @return Models\BlogPostsModel + */ + public function getNewMockModelInstance() + { + // Generate a Random Users. + $randomUsers = $this->usersTableGateway->fetchRandom(); + + return $this->getNewModelInstance([ + // blogPostId. Type = int. PHPType = int. Has no related objects. Default is literal null + // created. Type = timestamp. PHPType = string. Has no related objects. Default is interpreted current_timestamp() + 'created' => $this->faker->dateTime()->format("Y-m-d H:i:s"), // @todo: Make datetime fields accept DateTime objects instead of strings. - MB + // description. Type = text. PHPType = string. Has no related objects. Default is literal null + 'description' => substr($this->faker->text(500), 0, 500), + // title. Type = varchar. PHPType = string. Has no related objects. Default is literal null + 'title' => substr($this->faker->text(64), 0, 64), + // userId. Type = int. PHPType = int. Has related objects. Default is literal null + 'userId' => + $randomUsers instanceof Models\UsersModel + ? $this->usersTableGateway->fetchRandom()->getUserId() + : $this->usersTableGateway->getNewMockModelInstance()->save()->getUserId(), + ]); + } + + /** + * @param array $data + * + * @return Models\BlogPostsModel + */ + public function getNewModelInstance(array $data = []): Models\BlogPostsModel + { + return parent::getNewModelInstance($data); + } + + /** + * @param Models\BlogPostsModel $model + * + * @return Models\BlogPostsModel + */ + public function save(Model $model): Models\BlogPostsModel + { + return parent::save($model); + } +} diff --git a/test/app/src/TableGateways/Base/BaseMigrationsTableGateway.php b/test/app/src/TableGateways/Base/BaseMigrationsTableGateway.php new file mode 100755 index 0000000..1d987b3 --- /dev/null +++ b/test/app/src/TableGateways/Base/BaseMigrationsTableGateway.php @@ -0,0 +1,95 @@ +faker = $faker; + $this->databaseConnection = $databaseConnection; + $this->database = $this->databaseConnection->getDatabase('default'); + + $resultSetPrototype = new ResultSet(ResultSet::TYPE_ARRAYOBJECT, new $this->model()); + + parent::__construct($this->table, $this->database->getAdapter(), null, $resultSetPrototype); + } + + /** + * @return Models\MigrationsModel + */ + public function getNewMockModelInstance() + { + return $this->getNewModelInstance([ + // breakpoint. Type = tinyint. PHPType = int. Has no related objects. Default is literal null + 'breakpoint' => $this->faker->numberBetween(1, 0.01), + // end_time. Type = timestamp. PHPType = string. Has no related objects. Default is literal NULL + 'end_time' => $this->faker->dateTime()->format("Y-m-d H:i:s"), // @todo: Make datetime fields accept DateTime objects instead of strings. - MB + // migration_name. Type = varchar. PHPType = string. Has no related objects. Default is literal NULL + 'migration_name' => substr($this->faker->text(100), 0, 100), + // start_time. Type = timestamp. PHPType = string. Has no related objects. Default is literal NULL + 'start_time' => $this->faker->dateTime()->format("Y-m-d H:i:s"), // @todo: Make datetime fields accept DateTime objects instead of strings. - MB + // version. Type = bigint. PHPType = int. Has no related objects. Default is literal null + 'version' => $this->faker->numberBetween(1, 0.01), + ]); + } + + /** + * @param array $data + * + * @return Models\MigrationsModel + */ + public function getNewModelInstance(array $data = []): Models\MigrationsModel + { + return parent::getNewModelInstance($data); + } + + /** + * @param Models\MigrationsModel $model + * + * @return Models\MigrationsModel + */ + public function save(Model $model): Models\MigrationsModel + { + return parent::save($model); + } +} diff --git a/test/app/src/TableGateways/Base/BaseUsersTableGateway.php b/test/app/src/TableGateways/Base/BaseUsersTableGateway.php new file mode 100755 index 0000000..904a612 --- /dev/null +++ b/test/app/src/TableGateways/Base/BaseUsersTableGateway.php @@ -0,0 +1,92 @@ +faker = $faker; + $this->databaseConnection = $databaseConnection; + $this->database = $this->databaseConnection->getDatabase('default'); + + $resultSetPrototype = new ResultSet(ResultSet::TYPE_ARRAYOBJECT, new $this->model()); + + parent::__construct($this->table, $this->database->getAdapter(), null, $resultSetPrototype); + } + + /** + * @return Models\UsersModel + */ + public function getNewMockModelInstance() + { + return $this->getNewModelInstance([ + // created. Type = timestamp. PHPType = string. Has no related objects. Default is interpreted current_timestamp() + 'created' => $this->faker->dateTime()->format("Y-m-d H:i:s"), // @todo: Make datetime fields accept DateTime objects instead of strings. - MB + // email. Type = varchar. PHPType = string. Has no related objects. Default is literal null + 'email' => substr($this->faker->text(320), 0, 320), + // name. Type = varchar. PHPType = string. Has no related objects. Default is literal null + 'name' => substr($this->faker->text(32), 0, 32), + // userId. Type = int. PHPType = int. Has no related objects. Default is literal null + ]); + } + + /** + * @param array $data + * + * @return Models\UsersModel + */ + public function getNewModelInstance(array $data = []): Models\UsersModel + { + return parent::getNewModelInstance($data); + } + + /** + * @param Models\UsersModel $model + * + * @return Models\UsersModel + */ + public function save(Model $model): Models\UsersModel + { + return parent::save($model); + } +} diff --git a/test/app/src/TableGateways/BlogPostsTableGateway.php b/test/app/src/TableGateways/BlogPostsTableGateway.php new file mode 100755 index 0000000..aea9469 --- /dev/null +++ b/test/app/src/TableGateways/BlogPostsTableGateway.php @@ -0,0 +1,7 @@ +testTableGateway = App::DI(BlogPostsTableGateway::class); + $this->testInstance = $this->testTableGateway->getNewMockModelInstance(); + } + + public function testExchangeArray() + { + $data = []; + $data['blogPostId'] = self::getFaker()->randomDigitNotNull; + $data['title'] = self::getFaker()->word; + $data['description'] = self::getFaker()->word; + $data['userId'] = self::getFaker()->randomDigitNotNull; + $data['created'] = self::getFaker()->word; + $this->testInstance = new BlogPostsModel($data); + $this->assertEquals($data['blogPostId'], $this->testInstance->getBlogPostId()); + $this->assertEquals($data['title'], $this->testInstance->getTitle()); + $this->assertEquals($data['description'], $this->testInstance->getDescription()); + $this->assertEquals($data['userId'], $this->testInstance->getUserId()); + $this->assertEquals($data['created'], $this->testInstance->getCreated()); + } + + public function testGetRandom() + { + // If there is no data in the table, create some. + if (0 == $this->testTableGateway->getCount()) { + $dummyObject = $this->testTableGateway->getNewMockModelInstance(); + $this->testTableGateway->save($dummyObject); + } + + $blogPost = $this->testTableGateway->fetchRandom(); + $this->assertTrue($blogPost instanceof BlogPostsModel, 'Make sure that "'.get_class($blogPost).'" matches "BlogPostsModel"'); + + return $blogPost; + } + + public function testNewMockModelInstance() + { + $new = $this->testTableGateway->getNewMockModelInstance(); + + $this->assertInstanceOf( + Models\BlogPostsModel::class, + $new + ); + + $new->save(); + + return $new; + } + + public function testNewModelFactory() + { + $instance = BlogPostsModel::factory(); + + $this->assertInstanceOf( + Models\BlogPostsModel::class, + $instance + ); + } + + public function testSave() + { + /** @var Models\BlogPostsModel $mockModel */ + /** @var Models\BlogPostsModel $savedModel */ + $mockModel = $this->testTableGateway->getNewMockModelInstance(); + $savedModel = $mockModel->save(); + + $mockModelArray = $mockModel->__toArray(); + $savedModelArray = $savedModel->__toArray(); + + // Remove auto increments from test. + foreach ($mockModel->getAutoIncrementKeys() as $autoIncrementKey => $discard) { + foreach ($mockModelArray as $key => $value) { + if (strtolower($key) == strtolower($autoIncrementKey)) { + unset($mockModelArray[$key], $savedModelArray[$key]); + } + } + } + // Remove non-literal fields from comparison - these will be autogenerated by the server and is outside the scope of this test. + unset($mockModelArray['Created'], $savedModelArray['Created']); + + $this->assertEquals($mockModelArray, $savedModelArray); + } + + /** + * @depends testGetRandom + */ + public function testSettersAndGetters(BlogPostsModel $blogPosts) + { + $this->assertTrue(method_exists($blogPosts, 'getblogPostId')); + $this->assertTrue(method_exists($blogPosts, 'setblogPostId')); + $this->assertTrue(method_exists($blogPosts, 'gettitle')); + $this->assertTrue(method_exists($blogPosts, 'settitle')); + $this->assertTrue(method_exists($blogPosts, 'getdescription')); + $this->assertTrue(method_exists($blogPosts, 'setdescription')); + $this->assertTrue(method_exists($blogPosts, 'getuserId')); + $this->assertTrue(method_exists($blogPosts, 'setuserId')); + $this->assertTrue(method_exists($blogPosts, 'getcreated')); + $this->assertTrue(method_exists($blogPosts, 'setcreated')); + + $testBlogPosts = new BlogPostsModel(); + $input = self::getFaker()->randomDigitNotNull; + $testBlogPosts->setBlogPostId($input); + $this->assertEquals($input, $testBlogPosts->getBlogPostId()); + $input = self::getFaker()->word; + $testBlogPosts->setTitle($input); + $this->assertEquals($input, $testBlogPosts->getTitle()); + $input = self::getFaker()->word; + $testBlogPosts->setDescription($input); + $this->assertEquals($input, $testBlogPosts->getDescription()); + $input = self::getFaker()->randomDigitNotNull; + $testBlogPosts->setUserId($input); + $this->assertEquals($input, $testBlogPosts->getUserId()); + $input = self::getFaker()->word; + $testBlogPosts->setCreated($input); + $this->assertEquals($input, $testBlogPosts->getCreated()); + } + + public function testAutoincrementedIdIsApplied() + { + $new = $this->testTableGateway->getNewMockModelInstance(); + + // Set primary keys to null. + $new->setblogPostId(null); + + // Save the object + $saved = $new->save(); + + // verify that the AI keys have been set. + $this->assertNotNull($saved->getblogPostId()); + $this->assertGreaterThan(0, $saved->getblogPostId()); + } + + /** + * @large + */ + public function testDestroy() + { + /** @var Models\BlogPostsModel $destroyableModel */ + $destroyableModel = $this->testTableGateway->getNewMockModelInstance(); + $destroyableModel->save(); + $this->assertTrue(true, $destroyableModel->destroy()); + } + + /** + * @large + */ + public function testDestroyThoroughly() + { + /** @var Models\BlogPostsModel $destroyableModel */ + $destroyableModel = $this->testTableGateway->getNewMockModelInstance(); + $destroyableModel->save(); + $this->assertGreaterThan(0, $destroyableModel->destroyThoroughly()); + } + + + /** + * @depends testNewMockModelInstance + */ + public function test_RelatedObjects_FetchUserObject(BlogPostsModel $blogPost) + { + // Verify the function exists + $this->assertTrue(method_exists($blogPost, "fetchUserObject")); + + // Call the function on it + $blogPostModel = $blogPost->fetchUserObject(); + + $this->assertInstanceOf(Models\UsersModel::class, $blogPostModel); + } + + public function testGetPropertyMeta() + { + $propertyMeta = $this->testInstance->getPropertyMeta(); + $this->assertTrue(is_array($propertyMeta)); + $this->assertGreaterThan(0, count($propertyMeta)); + $this->assertArrayHasKey(BlogPostsModel::FIELD_BLOGPOSTID, $propertyMeta); + $this->assertArrayHasKey(BlogPostsModel::FIELD_TITLE, $propertyMeta); + $this->assertArrayHasKey(BlogPostsModel::FIELD_DESCRIPTION, $propertyMeta); + $this->assertArrayHasKey(BlogPostsModel::FIELD_USERID, $propertyMeta); + $this->assertArrayHasKey(BlogPostsModel::FIELD_CREATED, $propertyMeta); + } +} diff --git a/test/app/tests/Models/Generated/MigrationsTest.php b/test/app/tests/Models/Generated/MigrationsTest.php new file mode 100755 index 0000000..a1a4763 --- /dev/null +++ b/test/app/tests/Models/Generated/MigrationsTest.php @@ -0,0 +1,180 @@ +testTableGateway = App::DI(MigrationsTableGateway::class); + $this->testInstance = $this->testTableGateway->getNewMockModelInstance(); + } + + public function testExchangeArray() + { + $data = []; + $data['version'] = self::getFaker()->randomDigitNotNull; + $data['migration_name'] = self::getFaker()->word; + $data['start_time'] = self::getFaker()->word; + $data['end_time'] = self::getFaker()->word; + $data['breakpoint'] = self::getFaker()->randomDigitNotNull; + $this->testInstance = new MigrationsModel($data); + $this->assertEquals($data['version'], $this->testInstance->getVersion()); + $this->assertEquals($data['migration_name'], $this->testInstance->getMigration_name()); + $this->assertEquals($data['start_time'], $this->testInstance->getStart_time()); + $this->assertEquals($data['end_time'], $this->testInstance->getEnd_time()); + $this->assertEquals($data['breakpoint'], $this->testInstance->getBreakpoint()); + } + + public function testGetRandom() + { + // If there is no data in the table, create some. + if (0 == $this->testTableGateway->getCount()) { + $dummyObject = $this->testTableGateway->getNewMockModelInstance(); + $this->testTableGateway->save($dummyObject); + } + + $migration = $this->testTableGateway->fetchRandom(); + $this->assertTrue($migration instanceof MigrationsModel, 'Make sure that "'.get_class($migration).'" matches "MigrationsModel"'); + + return $migration; + } + + public function testNewMockModelInstance() + { + $new = $this->testTableGateway->getNewMockModelInstance(); + + $this->assertInstanceOf( + Models\MigrationsModel::class, + $new + ); + + $new->save(); + + return $new; + } + + public function testNewModelFactory() + { + $instance = MigrationsModel::factory(); + + $this->assertInstanceOf( + Models\MigrationsModel::class, + $instance + ); + } + + public function testSave() + { + /** @var Models\MigrationsModel $mockModel */ + /** @var Models\MigrationsModel $savedModel */ + $mockModel = $this->testTableGateway->getNewMockModelInstance(); + $savedModel = $mockModel->save(); + + $mockModelArray = $mockModel->__toArray(); + $savedModelArray = $savedModel->__toArray(); + + // Remove auto increments from test. + foreach ($mockModel->getAutoIncrementKeys() as $autoIncrementKey => $discard) { + foreach ($mockModelArray as $key => $value) { + if (strtolower($key) == strtolower($autoIncrementKey)) { + unset($mockModelArray[$key], $savedModelArray[$key]); + } + } + } + + $this->assertEquals($mockModelArray, $savedModelArray); + } + + /** + * @depends testGetRandom + */ + public function testSettersAndGetters(MigrationsModel $migrations) + { + $this->assertTrue(method_exists($migrations, 'getversion')); + $this->assertTrue(method_exists($migrations, 'setversion')); + $this->assertTrue(method_exists($migrations, 'getmigration_name')); + $this->assertTrue(method_exists($migrations, 'setmigration_name')); + $this->assertTrue(method_exists($migrations, 'getstart_time')); + $this->assertTrue(method_exists($migrations, 'setstart_time')); + $this->assertTrue(method_exists($migrations, 'getend_time')); + $this->assertTrue(method_exists($migrations, 'setend_time')); + $this->assertTrue(method_exists($migrations, 'getbreakpoint')); + $this->assertTrue(method_exists($migrations, 'setbreakpoint')); + + $testMigrations = new MigrationsModel(); + $input = self::getFaker()->randomDigitNotNull; + $testMigrations->setVersion($input); + $this->assertEquals($input, $testMigrations->getVersion()); + $input = self::getFaker()->word; + $testMigrations->setMigration_name($input); + $this->assertEquals($input, $testMigrations->getMigration_name()); + $input = self::getFaker()->word; + $testMigrations->setStart_time($input); + $this->assertEquals($input, $testMigrations->getStart_time()); + $input = self::getFaker()->word; + $testMigrations->setEnd_time($input); + $this->assertEquals($input, $testMigrations->getEnd_time()); + $input = self::getFaker()->randomDigitNotNull; + $testMigrations->setBreakpoint($input); + $this->assertEquals($input, $testMigrations->getBreakpoint()); + } + + /** + * @large + */ + public function testDestroy() + { + /** @var Models\MigrationsModel $destroyableModel */ + $destroyableModel = $this->testTableGateway->getNewMockModelInstance(); + $destroyableModel->save(); + $this->assertTrue(true, $destroyableModel->destroy()); + } + + /** + * @large + */ + public function testDestroyThoroughly() + { + /** @var Models\MigrationsModel $destroyableModel */ + $destroyableModel = $this->testTableGateway->getNewMockModelInstance(); + $destroyableModel->save(); + $this->assertGreaterThan(0, $destroyableModel->destroyThoroughly()); + } + + public function testGetPropertyMeta() + { + $propertyMeta = $this->testInstance->getPropertyMeta(); + $this->assertTrue(is_array($propertyMeta)); + $this->assertGreaterThan(0, count($propertyMeta)); + $this->assertArrayHasKey(MigrationsModel::FIELD_VERSION, $propertyMeta); + $this->assertArrayHasKey(MigrationsModel::FIELD_MIGRATION_NAME, $propertyMeta); + $this->assertArrayHasKey(MigrationsModel::FIELD_START_TIME, $propertyMeta); + $this->assertArrayHasKey(MigrationsModel::FIELD_END_TIME, $propertyMeta); + $this->assertArrayHasKey(MigrationsModel::FIELD_BREAKPOINT, $propertyMeta); + } +} diff --git a/test/app/tests/Models/Generated/UsersTest.php b/test/app/tests/Models/Generated/UsersTest.php new file mode 100755 index 0000000..ea54d12 --- /dev/null +++ b/test/app/tests/Models/Generated/UsersTest.php @@ -0,0 +1,246 @@ +testTableGateway = App::DI(UsersTableGateway::class); + $this->testInstance = $this->testTableGateway->getNewMockModelInstance(); + } + + public function testExchangeArray() + { + $data = []; + $data['userId'] = self::getFaker()->randomDigitNotNull; + $data['name'] = self::getFaker()->word; + $data['email'] = self::getFaker()->word; + $data['created'] = self::getFaker()->word; + $this->testInstance = new UsersModel($data); + $this->assertEquals($data['userId'], $this->testInstance->getUserId()); + $this->assertEquals($data['name'], $this->testInstance->getName()); + $this->assertEquals($data['email'], $this->testInstance->getEmail()); + $this->assertEquals($data['created'], $this->testInstance->getCreated()); + } + + public function testGetRandom() + { + // If there is no data in the table, create some. + if (0 == $this->testTableGateway->getCount()) { + $dummyObject = $this->testTableGateway->getNewMockModelInstance(); + $this->testTableGateway->save($dummyObject); + } + + $user = $this->testTableGateway->fetchRandom(); + $this->assertTrue($user instanceof UsersModel, 'Make sure that "'.get_class($user).'" matches "UsersModel"'); + + return $user; + } + + public function testNewMockModelInstance() + { + $new = $this->testTableGateway->getNewMockModelInstance(); + + $this->assertInstanceOf( + Models\UsersModel::class, + $new + ); + + $new->save(); + + return $new; + } + + public function testNewModelFactory() + { + $instance = UsersModel::factory(); + + $this->assertInstanceOf( + Models\UsersModel::class, + $instance + ); + } + + public function testSave() + { + /** @var Models\UsersModel $mockModel */ + /** @var Models\UsersModel $savedModel */ + $mockModel = $this->testTableGateway->getNewMockModelInstance(); + $savedModel = $mockModel->save(); + + $mockModelArray = $mockModel->__toArray(); + $savedModelArray = $savedModel->__toArray(); + + // Remove auto increments from test. + foreach ($mockModel->getAutoIncrementKeys() as $autoIncrementKey => $discard) { + foreach ($mockModelArray as $key => $value) { + if (strtolower($key) == strtolower($autoIncrementKey)) { + unset($mockModelArray[$key], $savedModelArray[$key]); + } + } + } + // Remove non-literal fields from comparison - these will be autogenerated by the server and is outside the scope of this test. + unset($mockModelArray['Created'], $savedModelArray['Created']); + + $this->assertEquals($mockModelArray, $savedModelArray); + } + + /** + * @depends testGetRandom + */ + public function testSettersAndGetters(UsersModel $users) + { + $this->assertTrue(method_exists($users, 'getuserId')); + $this->assertTrue(method_exists($users, 'setuserId')); + $this->assertTrue(method_exists($users, 'getname')); + $this->assertTrue(method_exists($users, 'setname')); + $this->assertTrue(method_exists($users, 'getemail')); + $this->assertTrue(method_exists($users, 'setemail')); + $this->assertTrue(method_exists($users, 'getcreated')); + $this->assertTrue(method_exists($users, 'setcreated')); + + $testUsers = new UsersModel(); + $input = self::getFaker()->randomDigitNotNull; + $testUsers->setUserId($input); + $this->assertEquals($input, $testUsers->getUserId()); + $input = self::getFaker()->word; + $testUsers->setName($input); + $this->assertEquals($input, $testUsers->getName()); + $input = self::getFaker()->word; + $testUsers->setEmail($input); + $this->assertEquals($input, $testUsers->getEmail()); + $input = self::getFaker()->word; + $testUsers->setCreated($input); + $this->assertEquals($input, $testUsers->getCreated()); + } + + public function testAutoincrementedIdIsApplied() + { + $new = $this->testTableGateway->getNewMockModelInstance(); + + // Set primary keys to null. + $new->setuserId(null); + + // Save the object + $saved = $new->save(); + + // verify that the AI keys have been set. + $this->assertNotNull($saved->getuserId()); + $this->assertGreaterThan(0, $saved->getuserId()); + } + + /** + * @large + */ + public function testDestroy() + { + /** @var Models\UsersModel $destroyableModel */ + $destroyableModel = $this->testTableGateway->getNewMockModelInstance(); + $destroyableModel->save(); + $this->assertTrue(true, $destroyableModel->destroy()); + } + + /** + * @large + */ + public function testDestroyThoroughly() + { + /** @var Models\UsersModel $destroyableModel */ + $destroyableModel = $this->testTableGateway->getNewMockModelInstance(); + $destroyableModel->save(); + $this->assertGreaterThan(0, $destroyableModel->destroyThoroughly()); + } + + /** + * @depends testNewMockModelInstance + */ + public function test_RemoteObjects_FetchBlogPostObjects(UsersModel $user) + { + // Verify the function exists + $this->assertTrue(method_exists($user, "fetchBlogPostObjects")); + + /** @var TableGateways\BlogPostsTableGateway $tableGateway */ + $tableGateway = App::DI(TableGateways\BlogPostsTableGateway::class); + + $user->save(); + + $this->assertNotNull($user->getUserId()); + + /** @var Models\BlogPostsModel $newBlogPostsModel */ + $newBlogPostsModel = $tableGateway->getNewMockModelInstance(); + $newBlogPostsModel->setUserId($user->getUserId()); + + // Save our new model. Make offerings to the gods of phpunit & transaction rollback to clean it up afterwards. + $newBlogPostsModel->save(); + $this->assertNotNull($newBlogPostsModel->getUserId()); + + // Call the singular function on it + $blogPostsModel = $user->fetchBlogPostObject(); + + $this->assertInstanceOf(Models\BlogPostsModel::class, $blogPostsModel); + + // Call the plural function on it + $blogPostModels = $user->fetchBlogPostObjects(); + + $this->assertGreaterThanOrEqual(1, count($blogPostModels), "fetchBlogPostObjects() failed to return atleast 1 Models\BlogPostsModel."); + $this->assertContainsOnlyInstancesOf(Models\BlogPostsModel::class, $blogPostModels); + + return [$user, $blogPostModels]; + } + + /** + * @depends test_RemoteObjects_FetchBlogPostObjects + */ + public function test_RemoteObjects_CountBlogPostObjects($list) + { + /** + * @var $user Models\UsersModel + * @var $blogPostModels Models\BlogPostsModel[] + */ + list($user, $blogPostModels) = $list; + + // Verify the function exists + $this->assertTrue(method_exists($user, "countBlogPostObjects")); + + // Call the function on it + $count = $user->countBlogPostObjects(); + + $this->assertCount($count, $blogPostModels); + } + + public function testGetPropertyMeta() + { + $propertyMeta = $this->testInstance->getPropertyMeta(); + $this->assertTrue(is_array($propertyMeta)); + $this->assertGreaterThan(0, count($propertyMeta)); + $this->assertArrayHasKey(UsersModel::FIELD_USERID, $propertyMeta); + $this->assertArrayHasKey(UsersModel::FIELD_NAME, $propertyMeta); + $this->assertArrayHasKey(UsersModel::FIELD_EMAIL, $propertyMeta); + $this->assertArrayHasKey(UsersModel::FIELD_CREATED, $propertyMeta); + } +} diff --git a/test/app/tests/Services/Generated/BlogPostsTest.php b/test/app/tests/Services/Generated/BlogPostsTest.php new file mode 100755 index 0000000..e04edc6 --- /dev/null +++ b/test/app/tests/Services/Generated/BlogPostsTest.php @@ -0,0 +1,393 @@ +getNewMockModelInstance() + ->save(); + } + } + + /** + * @before + */ + public function setupBlogPostsService(): void + { + $this->blogPostsService = App::DI(Services\BlogPostsService::class); + } + + /** + * @before + */ + public function setupBlogPostsTableGateway(): void + { + $this->blogPostsTableGateway = App::DI(BlogPostsTableGateway::class); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getNewModelInstance + */ + public function testGetNewModelInstance() + { + $this->assertInstanceOf( + BlogPostsModel::class, + $this->blogPostsService->getNewModelInstance() + ); + } + + /** + * @large + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getAll + */ + public function testGetAll() + { + $all = $this->blogPostsService->getAll(); + $this->assertInstanceOf( + BlogPostsModel::class, + reset($all) + ); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getRandom + */ + public function testGetRandom() + { + $random = $this->blogPostsService->getRandom(); + $this->assertInstanceOf( + BlogPostsModel::class, + $random + ); + + return $random; + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getByField + */ + public function testGetByPrimaryKeys(BlogPostsModel $random) + { + /** @var BlogPostsModel $found */ + // By blogPostId + $found = $this->blogPostsService->getByField(BlogPostsModel::FIELD_BLOGPOSTID, $random->getblogPostId()); + $this->assertInstanceOf( + BlogPostsModel::class, + $found + ); + $this->assertEquals($random, $found); + } + + /** + * @depends testGetRandom + */ + public function testCreateFromArray(BlogPostsModel $random) + { + $this->assertInstanceOf( + BlogPostsModel::class, + $this->blogPostsService->createFromArray($random->__toArray()) + ); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getMockObject + */ + public function testGetMockObject() + { + $this->assertInstanceOf( + BlogPostsModel::class, + $this->blogPostsService->getMockObject() + ); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getByField + */ + public function testGetByField(BlogPostsModel $random) + { + $found = $this->blogPostsService->getByField( + BlogPostsModel::FIELD_BLOGPOSTID, + $random->getBlogPostId() + ); + $this->assertInstanceOf( + BlogPostsModel::class, + $found, + "Calling Services\\BlogPostsService->getByField((BlogPostsModel::FIELD_BLOGPOSTID) failed to find a BlogPostsModel" + ); + $found = $this->blogPostsService->getByField( + BlogPostsModel::FIELD_TITLE, + $random->getTitle() + ); + $this->assertInstanceOf( + BlogPostsModel::class, + $found, + "Calling Services\\BlogPostsService->getByField((BlogPostsModel::FIELD_TITLE) failed to find a BlogPostsModel" + ); + $found = $this->blogPostsService->getByField( + BlogPostsModel::FIELD_DESCRIPTION, + $random->getDescription() + ); + $this->assertInstanceOf( + BlogPostsModel::class, + $found, + "Calling Services\\BlogPostsService->getByField((BlogPostsModel::FIELD_DESCRIPTION) failed to find a BlogPostsModel" + ); + $found = $this->blogPostsService->getByField( + BlogPostsModel::FIELD_USERID, + $random->getUserId() + ); + $this->assertInstanceOf( + BlogPostsModel::class, + $found, + "Calling Services\\BlogPostsService->getByField((BlogPostsModel::FIELD_USERID) failed to find a BlogPostsModel" + ); + $found = $this->blogPostsService->getByField( + BlogPostsModel::FIELD_CREATED, + $random->getCreated() + ); + $this->assertInstanceOf( + BlogPostsModel::class, + $found, + "Calling Services\\BlogPostsService->getByField((BlogPostsModel::FIELD_CREATED) failed to find a BlogPostsModel" + ); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::countByField + */ + public function testCountByField(BlogPostsModel $random) + { + $found = $this->blogPostsService->countByField(BlogPostsModel::FIELD_BLOGPOSTID, $random->getBlogPostId()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\BlogPostsService->countByField(BlogPostsModel::FIELD_BLOGPOSTID) failed to count a BlogPostsModel" + ); + $found = $this->blogPostsService->countByField(BlogPostsModel::FIELD_TITLE, $random->getTitle()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\BlogPostsService->countByField(BlogPostsModel::FIELD_TITLE) failed to count a BlogPostsModel" + ); + $found = $this->blogPostsService->countByField(BlogPostsModel::FIELD_DESCRIPTION, $random->getDescription()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\BlogPostsService->countByField(BlogPostsModel::FIELD_DESCRIPTION) failed to count a BlogPostsModel" + ); + $found = $this->blogPostsService->countByField(BlogPostsModel::FIELD_USERID, $random->getUserId()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\BlogPostsService->countByField(BlogPostsModel::FIELD_USERID) failed to count a BlogPostsModel" + ); + $found = $this->blogPostsService->countByField(BlogPostsModel::FIELD_CREATED, $random->getCreated()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\BlogPostsService->countByField(BlogPostsModel::FIELD_CREATED) failed to count a BlogPostsModel" + ); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getManyByField + */ + public function testGetManyByField(BlogPostsModel $random) + { + // Testing get by blogPostId + $this->assertContainsOnlyInstancesOf( + BlogPostsModel::class, + $this->blogPostsService->getManyByField( + BlogPostsModel::FIELD_BLOGPOSTID, + $random->getblogPostId(), + 5 + ) + ); + // Testing get by title + $this->assertContainsOnlyInstancesOf( + BlogPostsModel::class, + $this->blogPostsService->getManyByField( + BlogPostsModel::FIELD_TITLE, + $random->gettitle(), + 5 + ) + ); + // Testing get by description + $this->assertContainsOnlyInstancesOf( + BlogPostsModel::class, + $this->blogPostsService->getManyByField( + BlogPostsModel::FIELD_DESCRIPTION, + $random->getdescription(), + 5 + ) + ); + // Testing get by userId + $this->assertContainsOnlyInstancesOf( + BlogPostsModel::class, + $this->blogPostsService->getManyByField( + BlogPostsModel::FIELD_USERID, + $random->getuserId(), + 5 + ) + ); + // Testing get by created + $this->assertContainsOnlyInstancesOf( + BlogPostsModel::class, + $this->blogPostsService->getManyByField( + BlogPostsModel::FIELD_CREATED, + $random->getcreated(), + 5 + ) + ); + } + + private function getMockDataFilter(): \Closure + { + $mockData = self::$MockData; + return function (Where $where) use ($mockData) { + $where + ->nest() + ->equalTo(BlogPostsModel::FIELD_BLOGPOSTID, $mockData[0]->getblogPostId()) + ->or + ->equalTo(BlogPostsModel::FIELD_BLOGPOSTID, $mockData[1]->getblogPostId()) + ->or + ->equalTo(BlogPostsModel::FIELD_BLOGPOSTID, $mockData[2]->getblogPostId()) + ->or + ->equalTo(BlogPostsModel::FIELD_BLOGPOSTID, $mockData[3]->getblogPostId()) + ->or + ->equalTo(BlogPostsModel::FIELD_BLOGPOSTID, $mockData[4]->getblogPostId()) + ->or + ->equalTo(BlogPostsModel::FIELD_BLOGPOSTID, $mockData[5]->getblogPostId()) + ->unnest() + ; + }; + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getManyMatching + */ + public function testGetManyMatching(BlogPostsModel $random) + { + $filter = $this->getMockDataFilter(); + + $all = $this->blogPostsService->getManyMatching($filter); + $this->assertGreaterThan(0, count($all)); + $this->assertContainsOnlyInstancesOf(BlogPostsModel::class, $all); + + $one = $this->blogPostsService->getManyMatching($filter, null, Select::ORDER_ASCENDING, 1); + $this->assertEquals(1, count($one)); + $this->assertContainsOnlyInstancesOf(BlogPostsModel::class, $one); + + $asc = $this->blogPostsService->getMatching($filter, BlogPostsModel::FIELD_BLOGPOSTID, Select::ORDER_ASCENDING); + $desc = $this->blogPostsService->getMatching($filter, BlogPostsModel::FIELD_BLOGPOSTID, Select::ORDER_DESCENDING); + $this->assertEquals(blogPostsModel::class, get_class($asc)); + $this->assertEquals(blogPostsModel::class, get_class($desc)); + $this->assertNotEquals($asc, $desc); + $this->assertEquals($random, $this->blogPostsService->getMatching([BlogPostsModel::FIELD_BLOGPOSTID => $random->getblogPostId()])); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getMatching + */ + public function testGetMatching(BlogPostsModel $random) + { + $filter = $this->getMockDataFilter(); + + $all = $this->blogPostsService->getMatching($filter); + $this->assertEquals(blogPostsModel::class, get_class($all)); + + $asc = $this->blogPostsService->getMatching($filter, BlogPostsModel::FIELD_BLOGPOSTID, Select::ORDER_ASCENDING); + $desc = $this->blogPostsService->getMatching($filter, BlogPostsModel::FIELD_BLOGPOSTID, Select::ORDER_DESCENDING); + $this->assertEquals(blogPostsModel::class, get_class($asc)); + $this->assertEquals(blogPostsModel::class, get_class($desc)); + $this->assertNotEquals($asc, $desc); + $this->assertEquals($random, $this->blogPostsService->getMatching([BlogPostsModel::FIELD_BLOGPOSTID => $random->getblogPostId()])); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::deleteByField + */ + public function testDeleteByField() + { + /** @var BlogPostsModel[] $allDeleted */ + $allDeleted = []; + /** @var BlogPostsModel $deleteable */ + $deleteable = $this->blogPostsTableGateway + ->getNewMockModelInstance() + ->save(); + $this->assertEquals(1, $this->blogPostsService->deleteByField(BlogPostsModel::FIELD_BLOGPOSTID, $deleteable->getblogPostId())); + $allDeleted[] = $deleteable; + return $allDeleted; + } + + /** + * @depends testDeleteByField + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::deleteByField + * @param BlogPostsModel[] $allDeleted + */ + public function testDeleteByFieldVerify(array $allDeleted) + { + /** @var BlogPostsModel $deleteable */ + // By blogPostId + $deleteable = array_pop($allDeleted); + $this->assertEquals(null, $this->blogPostsService->getByField(BlogPostsModel::FIELD_BLOGPOSTID, $deleteable->getblogPostId())); + + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getTermPlural + */ + public function testGetTermPlural() + { + $this->assertNotEmpty($this->blogPostsService->getTermPlural()); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseBlogPostsService::getTermSingular + */ + public function testGetTermSingular() + { + $this->assertNotEmpty($this->blogPostsService->getTermSingular()); + } +} diff --git a/test/app/tests/Services/Generated/MigrationsTest.php b/test/app/tests/Services/Generated/MigrationsTest.php new file mode 100755 index 0000000..efa1fa6 --- /dev/null +++ b/test/app/tests/Services/Generated/MigrationsTest.php @@ -0,0 +1,393 @@ +getNewMockModelInstance() + ->save(); + } + } + + /** + * @before + */ + public function setupMigrationsService(): void + { + $this->migrationsService = App::DI(Services\MigrationsService::class); + } + + /** + * @before + */ + public function setupMigrationsTableGateway(): void + { + $this->migrationsTableGateway = App::DI(MigrationsTableGateway::class); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getNewModelInstance + */ + public function testGetNewModelInstance() + { + $this->assertInstanceOf( + MigrationsModel::class, + $this->migrationsService->getNewModelInstance() + ); + } + + /** + * @large + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getAll + */ + public function testGetAll() + { + $all = $this->migrationsService->getAll(); + $this->assertInstanceOf( + MigrationsModel::class, + reset($all) + ); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getRandom + */ + public function testGetRandom() + { + $random = $this->migrationsService->getRandom(); + $this->assertInstanceOf( + MigrationsModel::class, + $random + ); + + return $random; + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getByField + */ + public function testGetByPrimaryKeys(MigrationsModel $random) + { + /** @var MigrationsModel $found */ + // By version + $found = $this->migrationsService->getByField(MigrationsModel::FIELD_VERSION, $random->getversion()); + $this->assertInstanceOf( + MigrationsModel::class, + $found + ); + $this->assertEquals($random, $found); + } + + /** + * @depends testGetRandom + */ + public function testCreateFromArray(MigrationsModel $random) + { + $this->assertInstanceOf( + MigrationsModel::class, + $this->migrationsService->createFromArray($random->__toArray()) + ); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getMockObject + */ + public function testGetMockObject() + { + $this->assertInstanceOf( + MigrationsModel::class, + $this->migrationsService->getMockObject() + ); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getByField + */ + public function testGetByField(MigrationsModel $random) + { + $found = $this->migrationsService->getByField( + MigrationsModel::FIELD_VERSION, + $random->getVersion() + ); + $this->assertInstanceOf( + MigrationsModel::class, + $found, + "Calling Services\\MigrationsService->getByField((MigrationsModel::FIELD_VERSION) failed to find a MigrationsModel" + ); + $found = $this->migrationsService->getByField( + MigrationsModel::FIELD_MIGRATION_NAME, + $random->getMigration_name() + ); + $this->assertInstanceOf( + MigrationsModel::class, + $found, + "Calling Services\\MigrationsService->getByField((MigrationsModel::FIELD_MIGRATION_NAME) failed to find a MigrationsModel" + ); + $found = $this->migrationsService->getByField( + MigrationsModel::FIELD_START_TIME, + $random->getStart_time() + ); + $this->assertInstanceOf( + MigrationsModel::class, + $found, + "Calling Services\\MigrationsService->getByField((MigrationsModel::FIELD_START_TIME) failed to find a MigrationsModel" + ); + $found = $this->migrationsService->getByField( + MigrationsModel::FIELD_END_TIME, + $random->getEnd_time() + ); + $this->assertInstanceOf( + MigrationsModel::class, + $found, + "Calling Services\\MigrationsService->getByField((MigrationsModel::FIELD_END_TIME) failed to find a MigrationsModel" + ); + $found = $this->migrationsService->getByField( + MigrationsModel::FIELD_BREAKPOINT, + $random->getBreakpoint() + ); + $this->assertInstanceOf( + MigrationsModel::class, + $found, + "Calling Services\\MigrationsService->getByField((MigrationsModel::FIELD_BREAKPOINT) failed to find a MigrationsModel" + ); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::countByField + */ + public function testCountByField(MigrationsModel $random) + { + $found = $this->migrationsService->countByField(MigrationsModel::FIELD_VERSION, $random->getVersion()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\MigrationsService->countByField(MigrationsModel::FIELD_VERSION) failed to count a MigrationsModel" + ); + $found = $this->migrationsService->countByField(MigrationsModel::FIELD_MIGRATION_NAME, $random->getMigration_name()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\MigrationsService->countByField(MigrationsModel::FIELD_MIGRATION_NAME) failed to count a MigrationsModel" + ); + $found = $this->migrationsService->countByField(MigrationsModel::FIELD_START_TIME, $random->getStart_time()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\MigrationsService->countByField(MigrationsModel::FIELD_START_TIME) failed to count a MigrationsModel" + ); + $found = $this->migrationsService->countByField(MigrationsModel::FIELD_END_TIME, $random->getEnd_time()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\MigrationsService->countByField(MigrationsModel::FIELD_END_TIME) failed to count a MigrationsModel" + ); + $found = $this->migrationsService->countByField(MigrationsModel::FIELD_BREAKPOINT, $random->getBreakpoint()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\MigrationsService->countByField(MigrationsModel::FIELD_BREAKPOINT) failed to count a MigrationsModel" + ); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getManyByField + */ + public function testGetManyByField(MigrationsModel $random) + { + // Testing get by version + $this->assertContainsOnlyInstancesOf( + MigrationsModel::class, + $this->migrationsService->getManyByField( + MigrationsModel::FIELD_VERSION, + $random->getversion(), + 5 + ) + ); + // Testing get by migration_name + $this->assertContainsOnlyInstancesOf( + MigrationsModel::class, + $this->migrationsService->getManyByField( + MigrationsModel::FIELD_MIGRATION_NAME, + $random->getmigration_name(), + 5 + ) + ); + // Testing get by start_time + $this->assertContainsOnlyInstancesOf( + MigrationsModel::class, + $this->migrationsService->getManyByField( + MigrationsModel::FIELD_START_TIME, + $random->getstart_time(), + 5 + ) + ); + // Testing get by end_time + $this->assertContainsOnlyInstancesOf( + MigrationsModel::class, + $this->migrationsService->getManyByField( + MigrationsModel::FIELD_END_TIME, + $random->getend_time(), + 5 + ) + ); + // Testing get by breakpoint + $this->assertContainsOnlyInstancesOf( + MigrationsModel::class, + $this->migrationsService->getManyByField( + MigrationsModel::FIELD_BREAKPOINT, + $random->getbreakpoint(), + 5 + ) + ); + } + + private function getMockDataFilter(): \Closure + { + $mockData = self::$MockData; + return function (Where $where) use ($mockData) { + $where + ->nest() + ->equalTo(MigrationsModel::FIELD_VERSION, $mockData[0]->getversion()) + ->or + ->equalTo(MigrationsModel::FIELD_VERSION, $mockData[1]->getversion()) + ->or + ->equalTo(MigrationsModel::FIELD_VERSION, $mockData[2]->getversion()) + ->or + ->equalTo(MigrationsModel::FIELD_VERSION, $mockData[3]->getversion()) + ->or + ->equalTo(MigrationsModel::FIELD_VERSION, $mockData[4]->getversion()) + ->or + ->equalTo(MigrationsModel::FIELD_VERSION, $mockData[5]->getversion()) + ->unnest() + ; + }; + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getManyMatching + */ + public function testGetManyMatching(MigrationsModel $random) + { + $filter = $this->getMockDataFilter(); + + $all = $this->migrationsService->getManyMatching($filter); + $this->assertGreaterThan(0, count($all)); + $this->assertContainsOnlyInstancesOf(MigrationsModel::class, $all); + + $one = $this->migrationsService->getManyMatching($filter, null, Select::ORDER_ASCENDING, 1); + $this->assertEquals(1, count($one)); + $this->assertContainsOnlyInstancesOf(MigrationsModel::class, $one); + + $asc = $this->migrationsService->getMatching($filter, MigrationsModel::FIELD_VERSION, Select::ORDER_ASCENDING); + $desc = $this->migrationsService->getMatching($filter, MigrationsModel::FIELD_VERSION, Select::ORDER_DESCENDING); + $this->assertEquals(migrationsModel::class, get_class($asc)); + $this->assertEquals(migrationsModel::class, get_class($desc)); + $this->assertNotEquals($asc, $desc); + $this->assertEquals($random, $this->migrationsService->getMatching([MigrationsModel::FIELD_VERSION => $random->getversion()])); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getMatching + */ + public function testGetMatching(MigrationsModel $random) + { + $filter = $this->getMockDataFilter(); + + $all = $this->migrationsService->getMatching($filter); + $this->assertEquals(migrationsModel::class, get_class($all)); + + $asc = $this->migrationsService->getMatching($filter, MigrationsModel::FIELD_VERSION, Select::ORDER_ASCENDING); + $desc = $this->migrationsService->getMatching($filter, MigrationsModel::FIELD_VERSION, Select::ORDER_DESCENDING); + $this->assertEquals(migrationsModel::class, get_class($asc)); + $this->assertEquals(migrationsModel::class, get_class($desc)); + $this->assertNotEquals($asc, $desc); + $this->assertEquals($random, $this->migrationsService->getMatching([MigrationsModel::FIELD_VERSION => $random->getversion()])); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::deleteByField + */ + public function testDeleteByField() + { + /** @var MigrationsModel[] $allDeleted */ + $allDeleted = []; + /** @var MigrationsModel $deleteable */ + $deleteable = $this->migrationsTableGateway + ->getNewMockModelInstance() + ->save(); + $this->assertEquals(1, $this->migrationsService->deleteByField(MigrationsModel::FIELD_VERSION, $deleteable->getversion())); + $allDeleted[] = $deleteable; + return $allDeleted; + } + + /** + * @depends testDeleteByField + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::deleteByField + * @param MigrationsModel[] $allDeleted + */ + public function testDeleteByFieldVerify(array $allDeleted) + { + /** @var MigrationsModel $deleteable */ + // By version + $deleteable = array_pop($allDeleted); + $this->assertEquals(null, $this->migrationsService->getByField(MigrationsModel::FIELD_VERSION, $deleteable->getversion())); + + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getTermPlural + */ + public function testGetTermPlural() + { + $this->assertNotEmpty($this->migrationsService->getTermPlural()); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseMigrationsService::getTermSingular + */ + public function testGetTermSingular() + { + $this->assertNotEmpty($this->migrationsService->getTermSingular()); + } +} diff --git a/test/app/tests/Services/Generated/UsersTest.php b/test/app/tests/Services/Generated/UsersTest.php new file mode 100755 index 0000000..2bc6fd8 --- /dev/null +++ b/test/app/tests/Services/Generated/UsersTest.php @@ -0,0 +1,369 @@ +getNewMockModelInstance() + ->save(); + } + } + + /** + * @before + */ + public function setupUsersService(): void + { + $this->usersService = App::DI(Services\UsersService::class); + } + + /** + * @before + */ + public function setupUsersTableGateway(): void + { + $this->usersTableGateway = App::DI(UsersTableGateway::class); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getNewModelInstance + */ + public function testGetNewModelInstance() + { + $this->assertInstanceOf( + UsersModel::class, + $this->usersService->getNewModelInstance() + ); + } + + /** + * @large + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getAll + */ + public function testGetAll() + { + $all = $this->usersService->getAll(); + $this->assertInstanceOf( + UsersModel::class, + reset($all) + ); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getRandom + */ + public function testGetRandom() + { + $random = $this->usersService->getRandom(); + $this->assertInstanceOf( + UsersModel::class, + $random + ); + + return $random; + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getByField + */ + public function testGetByPrimaryKeys(UsersModel $random) + { + /** @var UsersModel $found */ + // By userId + $found = $this->usersService->getByField(UsersModel::FIELD_USERID, $random->getuserId()); + $this->assertInstanceOf( + UsersModel::class, + $found + ); + $this->assertEquals($random, $found); + } + + /** + * @depends testGetRandom + */ + public function testCreateFromArray(UsersModel $random) + { + $this->assertInstanceOf( + UsersModel::class, + $this->usersService->createFromArray($random->__toArray()) + ); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getMockObject + */ + public function testGetMockObject() + { + $this->assertInstanceOf( + UsersModel::class, + $this->usersService->getMockObject() + ); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getByField + */ + public function testGetByField(UsersModel $random) + { + $found = $this->usersService->getByField( + UsersModel::FIELD_USERID, + $random->getUserId() + ); + $this->assertInstanceOf( + UsersModel::class, + $found, + "Calling Services\\UsersService->getByField((UsersModel::FIELD_USERID) failed to find a UsersModel" + ); + $found = $this->usersService->getByField( + UsersModel::FIELD_NAME, + $random->getName() + ); + $this->assertInstanceOf( + UsersModel::class, + $found, + "Calling Services\\UsersService->getByField((UsersModel::FIELD_NAME) failed to find a UsersModel" + ); + $found = $this->usersService->getByField( + UsersModel::FIELD_EMAIL, + $random->getEmail() + ); + $this->assertInstanceOf( + UsersModel::class, + $found, + "Calling Services\\UsersService->getByField((UsersModel::FIELD_EMAIL) failed to find a UsersModel" + ); + $found = $this->usersService->getByField( + UsersModel::FIELD_CREATED, + $random->getCreated() + ); + $this->assertInstanceOf( + UsersModel::class, + $found, + "Calling Services\\UsersService->getByField((UsersModel::FIELD_CREATED) failed to find a UsersModel" + ); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::countByField + */ + public function testCountByField(UsersModel $random) + { + $found = $this->usersService->countByField(UsersModel::FIELD_USERID, $random->getUserId()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\UsersService->countByField(UsersModel::FIELD_USERID) failed to count a UsersModel" + ); + $found = $this->usersService->countByField(UsersModel::FIELD_NAME, $random->getName()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\UsersService->countByField(UsersModel::FIELD_NAME) failed to count a UsersModel" + ); + $found = $this->usersService->countByField(UsersModel::FIELD_EMAIL, $random->getEmail()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\UsersService->countByField(UsersModel::FIELD_EMAIL) failed to count a UsersModel" + ); + $found = $this->usersService->countByField(UsersModel::FIELD_CREATED, $random->getCreated()); + $this->assertGreaterThanOrEqual( + 1, + $found, + "Calling Services\\UsersService->countByField(UsersModel::FIELD_CREATED) failed to count a UsersModel" + ); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getManyByField + */ + public function testGetManyByField(UsersModel $random) + { + // Testing get by userId + $this->assertContainsOnlyInstancesOf( + UsersModel::class, + $this->usersService->getManyByField( + UsersModel::FIELD_USERID, + $random->getuserId(), + 5 + ) + ); + // Testing get by name + $this->assertContainsOnlyInstancesOf( + UsersModel::class, + $this->usersService->getManyByField( + UsersModel::FIELD_NAME, + $random->getname(), + 5 + ) + ); + // Testing get by email + $this->assertContainsOnlyInstancesOf( + UsersModel::class, + $this->usersService->getManyByField( + UsersModel::FIELD_EMAIL, + $random->getemail(), + 5 + ) + ); + // Testing get by created + $this->assertContainsOnlyInstancesOf( + UsersModel::class, + $this->usersService->getManyByField( + UsersModel::FIELD_CREATED, + $random->getcreated(), + 5 + ) + ); + } + + private function getMockDataFilter(): \Closure + { + $mockData = self::$MockData; + return function (Where $where) use ($mockData) { + $where + ->nest() + ->equalTo(UsersModel::FIELD_USERID, $mockData[0]->getuserId()) + ->or + ->equalTo(UsersModel::FIELD_USERID, $mockData[1]->getuserId()) + ->or + ->equalTo(UsersModel::FIELD_USERID, $mockData[2]->getuserId()) + ->or + ->equalTo(UsersModel::FIELD_USERID, $mockData[3]->getuserId()) + ->or + ->equalTo(UsersModel::FIELD_USERID, $mockData[4]->getuserId()) + ->or + ->equalTo(UsersModel::FIELD_USERID, $mockData[5]->getuserId()) + ->unnest() + ; + }; + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getManyMatching + */ + public function testGetManyMatching(UsersModel $random) + { + $filter = $this->getMockDataFilter(); + + $all = $this->usersService->getManyMatching($filter); + $this->assertGreaterThan(0, count($all)); + $this->assertContainsOnlyInstancesOf(UsersModel::class, $all); + + $one = $this->usersService->getManyMatching($filter, null, Select::ORDER_ASCENDING, 1); + $this->assertEquals(1, count($one)); + $this->assertContainsOnlyInstancesOf(UsersModel::class, $one); + + $asc = $this->usersService->getMatching($filter, UsersModel::FIELD_USERID, Select::ORDER_ASCENDING); + $desc = $this->usersService->getMatching($filter, UsersModel::FIELD_USERID, Select::ORDER_DESCENDING); + $this->assertEquals(usersModel::class, get_class($asc)); + $this->assertEquals(usersModel::class, get_class($desc)); + $this->assertNotEquals($asc, $desc); + $this->assertEquals($random, $this->usersService->getMatching([UsersModel::FIELD_USERID => $random->getuserId()])); + } + + /** + * @depends testGetRandom + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getMatching + */ + public function testGetMatching(UsersModel $random) + { + $filter = $this->getMockDataFilter(); + + $all = $this->usersService->getMatching($filter); + $this->assertEquals(usersModel::class, get_class($all)); + + $asc = $this->usersService->getMatching($filter, UsersModel::FIELD_USERID, Select::ORDER_ASCENDING); + $desc = $this->usersService->getMatching($filter, UsersModel::FIELD_USERID, Select::ORDER_DESCENDING); + $this->assertEquals(usersModel::class, get_class($asc)); + $this->assertEquals(usersModel::class, get_class($desc)); + $this->assertNotEquals($asc, $desc); + $this->assertEquals($random, $this->usersService->getMatching([UsersModel::FIELD_USERID => $random->getuserId()])); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::deleteByField + */ + public function testDeleteByField() + { + /** @var UsersModel[] $allDeleted */ + $allDeleted = []; + /** @var UsersModel $deleteable */ + $deleteable = $this->usersTableGateway + ->getNewMockModelInstance() + ->save(); + $this->assertEquals(1, $this->usersService->deleteByField(UsersModel::FIELD_USERID, $deleteable->getuserId())); + $allDeleted[] = $deleteable; + return $allDeleted; + } + + /** + * @depends testDeleteByField + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::deleteByField + * @param UsersModel[] $allDeleted + */ + public function testDeleteByFieldVerify(array $allDeleted) + { + /** @var UsersModel $deleteable */ + // By userId + $deleteable = array_pop($allDeleted); + $this->assertEquals(null, $this->usersService->getByField(UsersModel::FIELD_USERID, $deleteable->getuserId())); + + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getTermPlural + */ + public function testGetTermPlural() + { + $this->assertNotEmpty($this->usersService->getTermPlural()); + } + + /** + * @covers \Benzine\ORM\Tests\Services\Base\BaseUsersService::getTermSingular + */ + public function testGetTermSingular() + { + $this->assertNotEmpty($this->usersService->getTermSingular()); + } +}