Skip to content

Commit

Permalink
Improve already added field/UA exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Mar 9, 2024
1 parent 2f14657 commit 38cd7d2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,12 @@ public function addField(string $name, $seed = []): Field
{
$this->assertIsModel();

if ($this->hasField($name)) {
throw (new Exception('Field with such name already exists'))
->addMoreInfo('name', $name)
->addMoreInfo('seed', $seed);
}

if (is_object($seed)) {
$field = $seed;
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/Model/UserActionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public function addUserAction(string $name, $seed = []): UserAction
{
$this->assertIsModel();

if ($this->hasUserAction($name)) {
throw (new Exception('User action with such name already exists'))
->addMoreInfo('name', $name)
->addMoreInfo('seed', $seed);
}

if ($seed instanceof \Closure) {
$seed = ['callback' => $seed];
}
Expand Down
13 changes: 12 additions & 1 deletion tests/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,11 +767,22 @@ public function testNormalizeObjectException(): void
$m->set('foo', 'ABC');
}

public function testAddFieldDirectly(): void
public function testAddFieldDuplicateNameException(): void
{
$m = new Model();
$m->addField('foo');

$this->expectException(Exception::class);
$this->expectExceptionMessage('Field with such name already exists');
$m->addField('foo');
}

public function testAddFieldDirectlyException(): void
{
$model = new Model();

$this->expectException(Exception::class);
$this->expectExceptionMessage('Field can be added using addField() method only');
$model->add(new Field());
}

Expand Down
25 changes: 19 additions & 6 deletions tests/JoinArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,35 @@ public function testDirection(): void
self::assertSame('test_id', $this->getProtected($j, 'masterField'));
self::assertSame('id', $this->getProtected($j, 'foreignField'));

$this->expectException(Exception::class); // TODO not implemented yet, see https://github.com/atk4/data/issues/803
$this->expectException(Exception::class);
$this->expectExceptionMessage('Joining tables on non-id fields is not implemented yet');
$j = $m->join('contact4.foo_id', ['masterField' => 'test_id', 'reverse' => true]);
// self::assertTrue($j->reverse);
// self::assertSame('test_id', $this->getProtected($j, 'masterField'));
// self::assertSame('foo_id', $this->getProtected($j, 'foreignField'));
}

public function testJoinException(): void
public function testDirectionException(): void
{
$db = new Persistence\Array_(['user' => [], 'contact' => []]);
$m = new Model($db, ['table' => 'user']);

$this->expectException(Exception::class);
$this->expectExceptionMessage('Joining tables on non-id fields is not implemented yet');
$m->join('contact.foo_id', ['masterField' => 'test_id']);
}

public function testAddJoinDuplicateNameException(): void
{
$db = new Persistence\Array_();
$m = new Model($db);
$m->join('foo');

$this->expectException(Exception::class);
$this->expectExceptionMessage('Join with such name already exists');
$m->join('foo');
}

public function testJoinSaving1(): void
{
$db = new Persistence\Array_(['user' => [], 'contact' => []]);
Expand Down Expand Up @@ -212,15 +225,16 @@ public function testJoinSaving3(): void
], $this->getInternalPersistenceData($db));
}

/* Joining tables on non-id fields is not implemented yet
public function testJoinSaving4(): void
{
$db = new Persistence\Array_(['user' => [], 'contact' => []]);
$user = new Model($db, ['table' => 'user']);
$user->addField('name');
$user->addField('code');
$this->expectException(Exception::class);
$this->expectExceptionMessage('Joining tables on non-id fields is not implemented yet');
$j = $user->join('contact.code', ['masterField' => 'code']);
$j->addField('contact_phone');
/* $j->addField('contact_phone');
$user = $user->createEntity();
$user->set('name', 'John');
Expand All @@ -236,9 +250,8 @@ public function testJoinSaving4(): void
'contact' => [
1 => ['id' => 1, 'code' => 'C28', 'contact_phone' => '+123'],
],
], $this->getInternalPersistenceData($db));
], $this->getInternalPersistenceData($db)); */
}
*/

public function testJoinLoading(): void
{
Expand Down
7 changes: 3 additions & 4 deletions tests/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Atk4\Data\Tests;

use Atk4\Core\Exception as CoreException;
use Atk4\Data\Exception;
use Atk4\Data\Field;
use Atk4\Data\Model;
Expand Down Expand Up @@ -355,12 +354,12 @@ public function testHookBreakers(): void
$m->delete();
}

public function testAddFieldDuplicateNameException(): void
public function testAddTitleDuplicateNameException(): void
{
$m = new Model_Item($this->db);

$this->expectException(CoreException::class);
$this->expectExceptionMessage('already exist');
$this->expectException(Exception::class);
$this->expectExceptionMessage('Field with such name already exists');
$m->hasOne('foo', ['model' => [Model_Item::class]])->addTitle();
}

Expand Down
10 changes: 10 additions & 0 deletions tests/UserActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ public function testPreview(): void
self::assertSame('Also Backup UaClient', $client->getUserAction('also_backup')->getDescription());
}

public function testAddUserActionDuplicateNameException(): void
{
$m = new Model();
$m->addUserAction('foo', static fn () => 1);

$this->expectException(Exception::class);
$this->expectExceptionMessage('User action with such name already exists');
$m->addUserAction('foo', static fn () => 1);
}

public function testAppliesToSingleRecordNotEntityException(): void
{
$client = new UaClient($this->pers);
Expand Down

0 comments on commit 38cd7d2

Please sign in to comment.