Initial testing code. Must prove Datetime works.

This commit is contained in:
Greyscale 2020-08-24 20:11:43 +02:00
parent cb532fbd9d
commit 7f8f20151b
15 changed files with 158 additions and 122 deletions

View file

@ -22,6 +22,9 @@
</php>
<testsuites>
<testsuite name="Human">
<file>test/app/tests/Models/DateTimeFieldsTest.php</file>
</testsuite>
<testsuite name="Models">
<directory>test/app/tests/Models</directory>
</testsuite>

View file

@ -63,9 +63,9 @@ abstract class Model implements ModelInterface, \Serializable
foreach ($this->getListOfProperties() as $dbField => $property) {
$currentValue = $this->{$property};
if($currentValue instanceof \DateTime){
$array[$dbField] = $currentValue->format("Y-m-d H:i:s");
}else {
if ($currentValue instanceof \DateTime) {
$array[$dbField] = $currentValue->format('Y-m-d H:i:s');
} else {
$array[$dbField] = $currentValue;
}
}

View file

@ -50,20 +50,22 @@ abstract class BaseBlogPostsModel extends AbstractModel implements ModelInterfac
'blogPostId' => 'blogPostId',
];
// PHPType: int. DBType: int. Max Length: 2147483647.
// PHPType: int. DBType: int.
// Max Length: 2147483647.
protected ?int $blogPostId = null;
// PHPType: string. DBType: varchar. Max Length: .
// PHPType: string. DBType: varchar.
protected ?string $title = null;
// PHPType: string. DBType: text. Max Length: .
// PHPType: string. DBType: text.
protected ?string $description = null;
// PHPType: int. DBType: int. Max Length: 2147483647.
// PHPType: int. DBType: int.
// Max Length: 2147483647.
protected ?int $userId = null;
// PHPType: string. DBType: timestamp. Max Length: .
protected ?string $created = null;
// PHPType: \DateTime. DBType: timestamp.
protected ?\DateTime $created = null;
private Services\UsersService $usersService;
@ -104,7 +106,9 @@ abstract class BaseBlogPostsModel extends AbstractModel implements ModelInterfac
*/
public function getPropertyMeta(): array
{
$usersService = App::DI(Services\BlogPostsService::class);
/** @var Services\UsersService $usersService */
$usersService = App::DI(Services\UsersService::class);
return [
self::FIELD_BLOGPOSTID => [
'type' => self::TYPE_BLOGPOSTID,
@ -119,7 +123,10 @@ abstract class BaseBlogPostsModel extends AbstractModel implements ModelInterfac
],
self::FIELD_USERID => [
'type' => self::TYPE_USERID,
'remoteOptionsLoader' => $usersService->getAll(),
'service' => $usersService,
'remoteOptionsLoader' => function() use ($usersService){
return $usersService->getAll();
},
],
self::FIELD_CREATED => [
'type' => self::TYPE_CREATED,
@ -133,7 +140,7 @@ abstract class BaseBlogPostsModel extends AbstractModel implements ModelInterfac
}
/**
* @param int $blogPostId
* @param int|null $blogPostId
*
* @return self
*/
@ -150,7 +157,7 @@ abstract class BaseBlogPostsModel extends AbstractModel implements ModelInterfac
}
/**
* @param string $title
* @param string|null $title
*
* @return self
*/
@ -167,7 +174,7 @@ abstract class BaseBlogPostsModel extends AbstractModel implements ModelInterfac
}
/**
* @param string $description
* @param string|null $description
*
* @return self
*/
@ -184,7 +191,7 @@ abstract class BaseBlogPostsModel extends AbstractModel implements ModelInterfac
}
/**
* @param int $userId
* @param int|null $userId
*
* @return self
*/
@ -195,17 +202,17 @@ abstract class BaseBlogPostsModel extends AbstractModel implements ModelInterfac
return $this;
}
public function getCreated(): ?string
public function getCreated(): ?\DateTime
{
return $this->created;
}
/**
* @param string $created
* @param \DateTime|null $created
*
* @return self
*/
public function setCreated(string $created = null): self
public function setCreated(\DateTime $created = null): self
{
$this->created = $created;
@ -283,33 +290,19 @@ abstract class BaseBlogPostsModel extends AbstractModel implements ModelInterfac
/**
* 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;
return $this
->setBlogPostId($data['blogPostId'] ?? $data['BlogPostId'])
->setTitle($data['title'] ?? $data['Title'])
->setDescription($data['description'] ?? $data['Description'])
->setUserId($data['userId'] ?? $data['UserId'])
->setCreated(\DateTime::createFromFormat("Y-m-d H:i:s", $data['created'] ?? $data['Created']))
;
}
}

View file

@ -46,19 +46,21 @@ abstract class BaseMigrationsModel extends AbstractModel implements ModelInterfa
'version' => 'version',
];
// PHPType: int. DBType: bigint. Max Length: 9223372036854775807.
// PHPType: int. DBType: bigint.
// Max Length: 9223372036854775807.
protected ?int $version = null;
// PHPType: string. DBType: varchar. Max Length: .
// PHPType: string. DBType: varchar.
protected ?string $migration_name = null;
// PHPType: string. DBType: timestamp. Max Length: .
protected ?string $start_time = null;
// PHPType: \DateTime. DBType: timestamp.
protected ?\DateTime $start_time = null;
// PHPType: string. DBType: timestamp. Max Length: .
protected ?string $end_time = null;
// PHPType: \DateTime. DBType: timestamp.
protected ?\DateTime $end_time = null;
// PHPType: int. DBType: tinyint. Max Length: 127.
// PHPType: int. DBType: tinyint.
// Max Length: 127.
protected ?int $breakpoint = null;
@ -85,6 +87,7 @@ abstract class BaseMigrationsModel extends AbstractModel implements ModelInterfa
*/
public function getPropertyMeta(): array
{
return [
self::FIELD_VERSION => [
'type' => self::TYPE_VERSION,
@ -111,7 +114,7 @@ abstract class BaseMigrationsModel extends AbstractModel implements ModelInterfa
}
/**
* @param int $version
* @param int|null $version
*
* @return self
*/
@ -128,7 +131,7 @@ abstract class BaseMigrationsModel extends AbstractModel implements ModelInterfa
}
/**
* @param string $migration_name
* @param string|null $migration_name
*
* @return self
*/
@ -139,34 +142,34 @@ abstract class BaseMigrationsModel extends AbstractModel implements ModelInterfa
return $this;
}
public function getStart_time(): ?string
public function getStart_time(): ?\DateTime
{
return $this->start_time;
}
/**
* @param string $start_time
* @param \DateTime|null $start_time
*
* @return self
*/
public function setStart_time(string $start_time = null): self
public function setStart_time(\DateTime $start_time = null): self
{
$this->start_time = $start_time;
return $this;
}
public function getEnd_time(): ?string
public function getEnd_time(): ?\DateTime
{
return $this->end_time;
}
/**
* @param string $end_time
* @param \DateTime|null $end_time
*
* @return self
*/
public function setEnd_time(string $end_time = null): self
public function setEnd_time(\DateTime $end_time = null): self
{
$this->end_time = $end_time;
@ -179,7 +182,7 @@ abstract class BaseMigrationsModel extends AbstractModel implements ModelInterfa
}
/**
* @param int $breakpoint
* @param int|null $breakpoint
*
* @return self
*/
@ -248,33 +251,19 @@ abstract class BaseMigrationsModel extends AbstractModel implements ModelInterfa
/**
* 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;
return $this
->setVersion($data['version'] ?? $data['Version'])
->setMigration_name($data['migration_name'] ?? $data['Migration_name'])
->setStart_time(\DateTime::createFromFormat("Y-m-d H:i:s", $data['start_time'] ?? $data['Start_time']))
->setEnd_time(\DateTime::createFromFormat("Y-m-d H:i:s", $data['end_time'] ?? $data['End_time']))
->setBreakpoint($data['breakpoint'] ?? $data['Breakpoint'])
;
}
}

View file

@ -48,17 +48,18 @@ abstract class BaseUsersModel extends AbstractModel implements ModelInterface
'userId' => 'userId',
];
// PHPType: int. DBType: int. Max Length: 2147483647.
// PHPType: int. DBType: int.
// Max Length: 2147483647.
protected ?int $userId = null;
// PHPType: string. DBType: varchar. Max Length: .
// PHPType: string. DBType: varchar.
protected ?string $name = null;
// PHPType: string. DBType: varchar. Max Length: .
// PHPType: string. DBType: varchar.
protected ?string $email = null;
// PHPType: string. DBType: timestamp. Max Length: .
protected ?string $created = null;
// PHPType: \DateTime. DBType: timestamp.
protected ?\DateTime $created = null;
private Services\BlogPostsService $blogPostsService;
@ -99,6 +100,7 @@ abstract class BaseUsersModel extends AbstractModel implements ModelInterface
*/
public function getPropertyMeta(): array
{
return [
self::FIELD_USERID => [
'type' => self::TYPE_USERID,
@ -123,7 +125,7 @@ abstract class BaseUsersModel extends AbstractModel implements ModelInterface
}
/**
* @param int $userId
* @param int|null $userId
*
* @return self
*/
@ -140,7 +142,7 @@ abstract class BaseUsersModel extends AbstractModel implements ModelInterface
}
/**
* @param string $name
* @param string|null $name
*
* @return self
*/
@ -157,7 +159,7 @@ abstract class BaseUsersModel extends AbstractModel implements ModelInterface
}
/**
* @param string $email
* @param string|null $email
*
* @return self
*/
@ -168,17 +170,17 @@ abstract class BaseUsersModel extends AbstractModel implements ModelInterface
return $this;
}
public function getCreated(): ?string
public function getCreated(): ?\DateTime
{
return $this->created;
}
/**
* @param string $created
* @param \DateTime|null $created
*
* @return self
*/
public function setCreated(string $created = null): self
public function setCreated(\DateTime $created = null): self
{
$this->created = $created;
@ -323,30 +325,18 @@ abstract class BaseUsersModel extends AbstractModel implements ModelInterface
/**
* 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;
return $this
->setUserId($data['userId'] ?? $data['UserId'])
->setName($data['name'] ?? $data['Name'])
->setEmail($data['email'] ?? $data['Email'])
->setCreated(\DateTime::createFromFormat("Y-m-d H:i:s", $data['created'] ?? $data['Created']))
;
}
}

View file

@ -27,7 +27,6 @@ use Benzine\Exceptions\BenzineException;
abstract class BaseBlogPostsTableGateway extends AbstractTableGateway implements TableGatewayInterface
{
protected $table = 'BlogPosts';
//protected string $database = 'default';
protected string $model = Models\BlogPostsModel::class;
protected Generator $faker;
protected Connection\Databases $databaseConnection;
@ -69,8 +68,8 @@ abstract class BaseBlogPostsTableGateway extends AbstractTableGateway implements
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
// created. Type = timestamp. PHPType = \DateTime. Has no related objects. Default is interpreted current_timestamp()
'created' => $this->faker->word,
// 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

View file

@ -27,7 +27,6 @@ use Benzine\Exceptions\BenzineException;
abstract class BaseMigrationsTableGateway extends AbstractTableGateway implements TableGatewayInterface
{
protected $table = 'Migrations';
//protected string $database = 'default';
protected string $model = Models\MigrationsModel::class;
protected Generator $faker;
protected Connection\Databases $databaseConnection;
@ -62,12 +61,12 @@ abstract class BaseMigrationsTableGateway extends AbstractTableGateway implement
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
// end_time. Type = timestamp. PHPType = \DateTime. Has no related objects. Default is literal NULL
'end_time' => $this->faker->word,
// 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
// start_time. Type = timestamp. PHPType = \DateTime. Has no related objects. Default is literal NULL
'start_time' => $this->faker->word,
// version. Type = bigint. PHPType = int. Has no related objects. Default is literal null
'version' => $this->faker->numberBetween(1, 0.01),
]);

View file

@ -27,7 +27,6 @@ use Benzine\Exceptions\BenzineException;
abstract class BaseUsersTableGateway extends AbstractTableGateway implements TableGatewayInterface
{
protected $table = 'Users';
//protected string $database = 'default';
protected string $model = Models\UsersModel::class;
protected Generator $faker;
protected Connection\Databases $databaseConnection;
@ -60,8 +59,8 @@ abstract class BaseUsersTableGateway extends AbstractTableGateway implements Tab
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
// created. Type = timestamp. PHPType = \DateTime. Has no related objects. Default is interpreted current_timestamp()
'created' => $this->faker->word,
// 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

View file

@ -0,0 +1,64 @@
<?php
namespace Benzine\ORM\Tests\Test\Models;
use Benzine\ORM\Abstracts\Model;
use Benzine\ORM\Tests\App as App;
use Benzine\ORM\Tests\Models;
use Benzine\ORM\Tests\Models\UsersModel;
use Benzine\ORM\Tests\Services\UsersService;
use Benzine\ORM\Tests\TableGateways;
use Benzine\ORM\Tests\TableGateways\UsersTableGateway;
use Gone\UUID\UUID;
use Benzine\Tests\BaseTestCase;
class DateTimeFieldsTest extends BaseTestCase
{
/** @var Model[] */
private $entititesToCleanUp = [];
private UsersService $usersService;
public function setUp(): void
{
parent::setUp();
$this->usersService = \Benzine\App::DI(UsersService::class);
}
public function tearDown(): void
{
foreach($this->entititesToCleanUp as $model){
$model->destroy();
}
parent::tearDown();
}
/**
* @covers \Benzine\ORM\Abstracts\Model::__toRawArray()
* @covers \Benzine\ORM\Abstracts\Model::exchangeArray()
*/
public function testCreateWithDateTime(){
$user = new UsersModel();
$user->setName("Matthew Baggett");
$user->setEmail("matthew@baggett.me");
$dateTime = new \DateTime();
$dateTime->setDate(1990, 06,01);
$dateTime->setTime(04,00,00);
$user->setCreated($dateTime);
$saved = $user->save();
$this->entititesToCleanUp[] = $saved;
// Assert that it actually saved.
$this->assertGreaterThan(0, $saved->getUserId());
$this->assertEquals("1990-06-01 04:00:00", $saved->getCreated()->format("Y-m-d H:i:s"));
$reloaded = $this->usersService->getByField(UsersModel::FIELD_USERID, $saved->getUserId());
$this->assertEquals("1990-06-01 04:00:00", $reloaded->getCreated()->format("Y-m-d H:i:s"));
}
}

View file

@ -2,7 +2,7 @@
namespace Benzine\ORM\Tests\Test\Models\Generated;
use Benzine\ORM\Tests\App as App;
use Benzine\ORM\Tests\Test as App;
use Benzine\ORM\Tests\Models;
use Benzine\ORM\Tests\Models\BlogPostsModel;
use Benzine\ORM\Tests\TableGateways;

View file

@ -2,7 +2,7 @@
namespace Benzine\ORM\Tests\Test\Models\Generated;
use Benzine\ORM\Tests\App as App;
use Benzine\ORM\Tests\Test as App;
use Benzine\ORM\Tests\Models;
use Benzine\ORM\Tests\Models\MigrationsModel;
use Benzine\ORM\Tests\TableGateways;

View file

@ -2,7 +2,7 @@
namespace Benzine\ORM\Tests\Test\Models\Generated;
use Benzine\ORM\Tests\App as App;
use Benzine\ORM\Tests\Test as App;
use Benzine\ORM\Tests\Models;
use Benzine\ORM\Tests\Models\UsersModel;
use Benzine\ORM\Tests\TableGateways;

View file

@ -2,7 +2,7 @@
namespace Benzine\ORM\Tests\Test\Services\Generated;
use Benzine\ORM\Tests\App as App;
use Benzine\ORM\Tests\Test as App;
use Benzine\ORM\Tests\TableGateways\BlogPostsTableGateway;
use Benzine\ORM\Tests\Services;
use Benzine\ORM\Tests\Models\BlogPostsModel;

View file

@ -2,7 +2,7 @@
namespace Benzine\ORM\Tests\Test\Services\Generated;
use Benzine\ORM\Tests\App as App;
use Benzine\ORM\Tests\Test as App;
use Benzine\ORM\Tests\TableGateways\MigrationsTableGateway;
use Benzine\ORM\Tests\Services;
use Benzine\ORM\Tests\Models\MigrationsModel;

View file

@ -2,7 +2,7 @@
namespace Benzine\ORM\Tests\Test\Services\Generated;
use Benzine\ORM\Tests\App as App;
use Benzine\ORM\Tests\Test as App;
use Benzine\ORM\Tests\TableGateways\UsersTableGateway;
use Benzine\ORM\Tests\Services;
use Benzine\ORM\Tests\Models\UsersModel;