Skip to content

Commit

Permalink
DEBUG use extended Model everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 2, 2022
1 parent aa052d4 commit fe76395
Show file tree
Hide file tree
Showing 54 changed files with 419 additions and 380 deletions.
4 changes: 2 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ parameters:
- '~^Access to an undefined property Atk4\\Data\\Persistence::\$connection\.$~'
- '~^Call to an undefined method Atk4\\Data\\Persistence::dsql\(\)\.$~'
# for src/Field/SqlExpressionField.php
- '~^Call to an undefined method Atk4\\Data\\Model::expr\(\)\.$~'
- '~^Call to an undefined method Atk4\\Data\\Model2?::expr\(\)\.$~'
# for src/Model.php
- '~^Call to an undefined method Atk4\\Data\\Persistence::update\(\)\.$~'
- '~^Call to an undefined method Atk4\\Data\\Persistence::insert\(\)\.$~'
Expand All @@ -60,7 +60,7 @@ parameters:
- '~^Call to an undefined method Atk4\\Data\\Persistence::initQuery\(\)\.$~'
- '~^Call to an undefined method Atk4\\Data\\Persistence::lastInsertId\(\)\.$~'
# for src/Reference/HasMany.php
- '~^Call to an undefined method Atk4\\Data\\Model::dsql\(\)\.$~'
- '~^Call to an undefined method Atk4\\Data\\Model2?::dsql\(\)\.$~'
# for tests/FieldTest.php
- '~^Call to an undefined method Atk4\\Data\\Reference\\HasOne::addTitle\(\)\.$~'
# for tests/JoinSqlTest.php
Expand Down
2 changes: 1 addition & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public function getQueryArguments($operator, $value): array
Scope\Condition::OPERATOR_NOT_REGEXP,
], true)) {
$typecastField = new self(['type' => 'string']);
$typecastField->setOwner(new Model($this->getOwner()->persistence, ['table' => false]));
$typecastField->setOwner(new Model2($this->getOwner()->persistence, ['table' => false]));
$typecastField->short_name = $this->short_name;
$allowArray = false;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Model2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Atk4\Data;

class Model2 extends Model
{
}
3 changes: 2 additions & 1 deletion src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Atk4\Data\Exception;
use Atk4\Data\Field;
use Atk4\Data\Model;
use Atk4\Data\Model2;
use Atk4\Data\Persistence;
use Atk4\Data\Persistence\Array_\Action;
use Atk4\Data\Persistence\Array_\Action\RenameColumnIterator;
Expand Down Expand Up @@ -75,7 +76,7 @@ private function seedData(Model $model): void
return $join->foreign_table;
}, null, Array_\Join::class)();
if (isset($this->seedData[$joinTableName])) {
$dummyJoinModel = new Model($this, ['table' => $joinTableName]);
$dummyJoinModel = new Model2($this, ['table' => $joinTableName]);
$this->add($dummyJoinModel);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Persistence/Sql/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Atk4\Data\Persistence\Sql;

use Atk4\Data\Model;
use Atk4\Data\Model2;
use Atk4\Data\Persistence;

/**
Expand Down Expand Up @@ -147,7 +148,7 @@ public function beforeInsert(Model $entity, array &$data): void
$query->setMulti($model->persistence->typecastSaveRow($model, $this->getAndUnsetSaveBuffer($entity)));
// $query->set($this->foreign_field, null);
$query->insert();
$this->setId($entity, $model->persistence->lastInsertId(new Model($model->persistence, ['table' => $this->foreign_table])));
$this->setId($entity, $model->persistence->lastInsertId(new Model2($model->persistence, ['table' => $this->foreign_table])));

if ($this->hasJoin()) {
$this->getJoin()->setSaveBufferValue($entity, $this->master_field, $this->getId($entity));
Expand Down
30 changes: 15 additions & 15 deletions tests/BusinessModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Atk4\Core\Phpunit\TestCase;
use Atk4\Data\Exception;
use Atk4\Data\Field;
use Atk4\Data\Model;
use Atk4\Data\Model2;
use Atk4\Data\Persistence;
use Atk4\Data\Tests\Model\Client;
use Atk4\Data\Tests\Model\User;
Expand All @@ -16,7 +16,7 @@ class BusinessModelTest extends TestCase
{
public function testConstructFields(): void
{
$m = new Model();
$m = new Model2();
$m->addField('name');

$f = $m->getField('name');
Expand All @@ -29,7 +29,7 @@ public function testConstructFields(): void

public function testFieldAccess(): void
{
$m = new Model();
$m = new Model2();
$m->addField('name');
$m->addField('surname');
$m = $m->createEntity();
Expand All @@ -46,15 +46,15 @@ public function testFieldAccess(): void

public function testNoFieldException(): void
{
$m = new Model();
$m = new Model2();
$m = $m->createEntity();
$this->expectException(Exception::class);
$m->set('name', 5);
}

public function testDirty(): void
{
$m = new Model();
$m = new Model2();
$m->addField('name');
$m = $m->createEntity();
$dataRef = &$m->getDataRef();
Expand Down Expand Up @@ -107,7 +107,7 @@ public function testDirty(): void
$this->assertSame([], $m->getDirtyRef());

// now with defaults
$m = new Model();
$m = new Model2();
$f = $m->addField('name', ['default' => 'John']);
$m = $m->createEntity();
$this->assertSame('John', $f->default);
Expand All @@ -126,7 +126,7 @@ public function testDirty(): void
public function testDefaultInit(): void
{
$p = new Persistence\Array_();
$m = new Model($p);
$m = new Model2($p);
$m = $m->createEntity();

$this->assertNotNull($m->getField('id'));
Expand All @@ -137,7 +137,7 @@ public function testDefaultInit(): void

public function testException1(): void
{
$m = new Model();
$m = new Model2();
$m->addField('name');
$m->addField('surname');
$m->setOnlyFields(['surname']);
Expand All @@ -149,7 +149,7 @@ public function testException1(): void

public function testException1Fixed(): void
{
$m = new Model();
$m = new Model2();
$m->addField('name');
$m->addField('surname');
$m->setOnlyFields(['surname']);
Expand All @@ -163,7 +163,7 @@ public function testException1Fixed(): void

public function testSetTitle(): void
{
$m = new Model();
$m = new Model2();
$m->addField('name');
$m = $m->createEntity();
$m->set('name', 'foo');
Expand All @@ -178,31 +178,31 @@ public function testSetTitle(): void
*/
public function testException2(): void
{
$m = new Model();
$m = new Model2();
$m = $m->createEntity();
$this->expectException(\Error::class);
$m->set(0, 'foo'); // @phpstan-ignore-line
}

public function testException2a(): void
{
$m = new Model();
$m = new Model2();
$m = $m->createEntity();
$this->expectException(Exception::class);
$m->set('3', 'foo');
}

public function testException2b(): void
{
$m = new Model();
$m = new Model2();
$m = $m->createEntity();
$this->expectException(Exception::class);
$m->set('3b', 'foo');
}

public function testException2c(): void
{
$m = new Model();
$m = new Model2();
$m = $m->createEntity();
$this->expectException(Exception::class);
$m->set('', 'foo');
Expand All @@ -218,7 +218,7 @@ public function testClass1(): void

public function testNormalize(): void
{
$m = new Model();
$m = new Model2();
$m->addField('name', ['type' => 'string']);
$m->addField('age', ['type' => 'integer']);
$m->addField('data');
Expand Down
39 changes: 20 additions & 19 deletions tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Atk4\Data\Tests;

use Atk4\Data\Model;
use Atk4\Data\Model2;
use Atk4\Data\Schema\TestCase;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
Expand All @@ -20,7 +21,7 @@ public function testBasic(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addFields(['name', 'gender']);

$mm = $m->tryLoad(1);
Expand Down Expand Up @@ -52,7 +53,7 @@ public function testBasic(): void

public function testEntityNoScopeCloning(): void
{
$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$scope = $m->scope();
$this->assertSame($scope, $m->createEntity()->getModel()->scope());
$this->expectException(\Atk4\Data\Exception::class);
Expand All @@ -68,13 +69,13 @@ public function testEntityReloadWithDifferentIdException(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addFields(['name', 'gender']);

$m = $m->tryLoad(1);
$this->assertSame('John', $m->get('name'));
\Closure::bind(function () use ($m) {
$m->_entityId = 2;
$m->_entityId = null ?? 2; // @phpstan-ignore-line https://github.com/phpstan/phpstan-src/pull/706
}, null, Model::class)();
$this->expectException(\Atk4\Data\Exception::class);
$this->expectExceptionMessageMatches('~entity.+different~');
Expand All @@ -92,7 +93,7 @@ public function testNull(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addFields(['name', 'gender']);

$m->addCondition('gender', null);
Expand All @@ -117,7 +118,7 @@ public function testOperations(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addFields(['name', 'gender']);

$mm = $m->tryLoad(1);
Expand Down Expand Up @@ -163,7 +164,7 @@ public function testExpressions1(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addFields(['name', 'gender']);

$mm = $m->tryLoad(1);
Expand Down Expand Up @@ -195,7 +196,7 @@ public function testExpressions2(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addFields(['name', 'gender', 'surname']);

$mm = $m->tryLoad(1);
Expand Down Expand Up @@ -245,7 +246,7 @@ public function testExpressionJoin(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addFields(['name', 'gender', 'surname']);

$m->join('contact')->addField('contact_phone');
Expand Down Expand Up @@ -293,22 +294,22 @@ public function testArrayCondition(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addField('name');
$m->addCondition('name', ['John', 'Doe']);
$this->assertCount(1, $m->export());

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addField('name');
$m->addCondition('name', 'in', ['Johhny', 'Doe', 'Mary']);
$this->assertCount(2, $m->export());

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addField('name');
$m->addCondition('name', []); // this should not fail, always should be false
$this->assertCount(0, $m->export());

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addField('name');
$m->addCondition('name', 'not in', []); // this should not fail, always should be true
$this->assertCount(3, $m->export());
Expand All @@ -323,7 +324,7 @@ public function testDateCondition(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addField('name');
$m->addField('date', ['type' => 'date']);

Expand All @@ -340,7 +341,7 @@ public function testDateCondition2(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addField('name');
$m->addField('date', ['type' => 'date']);

Expand All @@ -358,7 +359,7 @@ public function testDateConditionFailure(): void
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m = new Model2($this->db, ['table' => 'user']);
$m->addField('name');
$m->addField('date', ['type' => 'date']);

Expand All @@ -376,7 +377,7 @@ public function testOrConditions(): void
],
]);

$u = (new Model($this->db, ['table' => 'user']))->addFields(['name']);
$u = (new Model2($this->db, ['table' => 'user']))->addFields(['name']);

$u->addCondition(Model\Scope::createOr(
['name', 'John'],
Expand Down Expand Up @@ -406,7 +407,7 @@ public function testLoadBy(): void
],
]);

$u = (new Model($this->db, ['table' => 'user']))->addFields(['name']);
$u = (new Model2($this->db, ['table' => 'user']))->addFields(['name']);

$u2 = $u->loadBy('name', 'John');
$this->assertSame(['id' => 1, 'name' => 'John'], $u2->get());
Expand Down Expand Up @@ -438,7 +439,7 @@ public function testLikeCondition(): void
],
]);

$u = new Model($this->db, ['table' => 'user']);
$u = new Model2($this->db, ['table' => 'user']);
$u->addField('name', ['type' => 'string']);
$u->addField('active', ['type' => 'boolean']);
$u->addField('created', ['type' => 'datetime']);
Expand Down
Loading

0 comments on commit fe76395

Please sign in to comment.