From d15e5030f245b79504e0d575011662bdcdd2adcf Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 26 Nov 2024 17:21:27 +0100 Subject: [PATCH] Add more tests. --- tests/Fixture/RelationsFixture.php | 36 +++++++ tests/TestCase/Command/ModelCommandTest.php | 29 ++++++ .../Model/testBakeEntityCustomHidden.php | 1 + .../Model/testBakeEntityFullContext.php | 2 + .../Model/testBakeEntityHidden.php | 1 + .../Model/testBakeEntitySimple.php | 1 + .../Model/testBakeEntitySimpleUnchanged.php | 1 + .../Model/testBakeEntityWithPlugin.php | 2 + .../Model/testBakeTableWithPlugin.php | 5 + .../Model/testBakeWithRulesUnique.php | 4 + tests/schema.php | 28 ++++++ .../App/Controller/RelationsController.php | 99 +++++++++++++++++++ 12 files changed, 209 insertions(+) create mode 100644 tests/Fixture/RelationsFixture.php create mode 100644 tests/test_app/App/Controller/RelationsController.php diff --git a/tests/Fixture/RelationsFixture.php b/tests/Fixture/RelationsFixture.php new file mode 100644 index 00000000..d7b09a1f --- /dev/null +++ b/tests/Fixture/RelationsFixture.php @@ -0,0 +1,36 @@ + 1, + 'other_id' => 1, + 'body' => 'Try it out!', + ], + ]; +} diff --git a/tests/TestCase/Command/ModelCommandTest.php b/tests/TestCase/Command/ModelCommandTest.php index 4e981b8f..91cce3a3 100644 --- a/tests/TestCase/Command/ModelCommandTest.php +++ b/tests/TestCase/Command/ModelCommandTest.php @@ -668,6 +668,35 @@ public function testBelongsToGeneration() * @return void */ public function testBelongsToGenerationConstraints() + { + $model = $this->getTableLocator()->get('Relations'); + $command = new ModelCommand(); + $command->connection = 'test'; + $result = $command->findBelongsTo($model, []); + $expected = [ + 'belongsTo' => [ + [ + 'alias' => 'Users', + 'foreignKey' => 'user_id', + 'joinType' => 'INNER', + ], + [ + 'alias' => 'Others', + 'foreignKey' => 'other_id', + 'joinType' => 'INNER', + 'className' => 'Users', + ], + ], + ]; + $this->assertEquals($expected, $result); + } + + /** + * Test that belongsTo association generation uses aliased constraints on the table + * + * @return void + */ + public function testBelongsToGenerationConstraintsAliased() { $model = $this->getTableLocator()->get('Invitations'); $command = new ModelCommand(); diff --git a/tests/comparisons/Model/testBakeEntityCustomHidden.php b/tests/comparisons/Model/testBakeEntityCustomHidden.php index 7698be01..8ca1a19e 100644 --- a/tests/comparisons/Model/testBakeEntityCustomHidden.php +++ b/tests/comparisons/Model/testBakeEntityCustomHidden.php @@ -15,6 +15,7 @@ * @property \Cake\I18n\DateTime|null $updated * * @property \Bake\Test\App\Model\Entity\Comment[] $comments + * @property \Bake\Test\App\Model\Entity\Relation[] $relations * @property \Bake\Test\App\Model\Entity\TodoItem[] $todo_items */ class User extends Entity diff --git a/tests/comparisons/Model/testBakeEntityFullContext.php b/tests/comparisons/Model/testBakeEntityFullContext.php index 240761a8..4a4388ea 100644 --- a/tests/comparisons/Model/testBakeEntityFullContext.php +++ b/tests/comparisons/Model/testBakeEntityFullContext.php @@ -15,6 +15,7 @@ * @property \Cake\I18n\DateTime|null $updated * * @property \Bake\Test\App\Model\Entity\Comment[] $comments + * @property \Bake\Test\App\Model\Entity\Relation[] $relations * @property \Bake\Test\App\Model\Entity\TodoItem[] $todo_items */ class User extends Entity @@ -34,6 +35,7 @@ class User extends Entity 'created' => true, 'updated' => true, 'comments' => true, + 'relations' => true, 'todo_items' => true, ]; diff --git a/tests/comparisons/Model/testBakeEntityHidden.php b/tests/comparisons/Model/testBakeEntityHidden.php index ef086579..fe51ce0c 100644 --- a/tests/comparisons/Model/testBakeEntityHidden.php +++ b/tests/comparisons/Model/testBakeEntityHidden.php @@ -15,6 +15,7 @@ * @property \Cake\I18n\DateTime|null $updated * * @property \Bake\Test\App\Model\Entity\Comment[] $comments + * @property \Bake\Test\App\Model\Entity\Relation[] $relations * @property \Bake\Test\App\Model\Entity\TodoItem[] $todo_items */ class User extends Entity diff --git a/tests/comparisons/Model/testBakeEntitySimple.php b/tests/comparisons/Model/testBakeEntitySimple.php index 8a38a690..ee2ae419 100644 --- a/tests/comparisons/Model/testBakeEntitySimple.php +++ b/tests/comparisons/Model/testBakeEntitySimple.php @@ -15,6 +15,7 @@ * @property \Cake\I18n\DateTime|null $updated * * @property \Bake\Test\App\Model\Entity\Comment[] $comments + * @property \Bake\Test\App\Model\Entity\Relation[] $relations * @property \Bake\Test\App\Model\Entity\TodoItem[] $todo_items */ class User extends Entity diff --git a/tests/comparisons/Model/testBakeEntitySimpleUnchanged.php b/tests/comparisons/Model/testBakeEntitySimpleUnchanged.php index 8a38a690..ee2ae419 100644 --- a/tests/comparisons/Model/testBakeEntitySimpleUnchanged.php +++ b/tests/comparisons/Model/testBakeEntitySimpleUnchanged.php @@ -15,6 +15,7 @@ * @property \Cake\I18n\DateTime|null $updated * * @property \Bake\Test\App\Model\Entity\Comment[] $comments + * @property \Bake\Test\App\Model\Entity\Relation[] $relations * @property \Bake\Test\App\Model\Entity\TodoItem[] $todo_items */ class User extends Entity diff --git a/tests/comparisons/Model/testBakeEntityWithPlugin.php b/tests/comparisons/Model/testBakeEntityWithPlugin.php index 4707f601..a9efefbe 100644 --- a/tests/comparisons/Model/testBakeEntityWithPlugin.php +++ b/tests/comparisons/Model/testBakeEntityWithPlugin.php @@ -15,6 +15,7 @@ * @property \Cake\I18n\DateTime|null $updated * * @property \BakeTest\Model\Entity\Comment[] $comments + * @property \BakeTest\Model\Entity\Relation[] $relations * @property \BakeTest\Model\Entity\TodoItem[] $todo_items */ class User extends Entity @@ -34,6 +35,7 @@ class User extends Entity 'created' => true, 'updated' => true, 'comments' => true, + 'relations' => true, 'todo_items' => true, ]; diff --git a/tests/comparisons/Model/testBakeTableWithPlugin.php b/tests/comparisons/Model/testBakeTableWithPlugin.php index 6ca47126..a4797663 100644 --- a/tests/comparisons/Model/testBakeTableWithPlugin.php +++ b/tests/comparisons/Model/testBakeTableWithPlugin.php @@ -12,6 +12,7 @@ * Users Model * * @property \BakeTest\Model\Table\CommentsTable&\Cake\ORM\Association\HasMany $Comments + * @property \BakeTest\Model\Table\RelationsTable&\Cake\ORM\Association\HasMany $Relations * @property \BakeTest\Model\Table\TodoItemsTable&\Cake\ORM\Association\HasMany $TodoItems * * @method \BakeTest\Model\Entity\User newEmptyEntity() @@ -52,6 +53,10 @@ public function initialize(array $config): void 'foreignKey' => 'user_id', 'className' => 'BakeTest.Comments', ]); + $this->hasMany('Relations', [ + 'foreignKey' => 'user_id', + 'className' => 'BakeTest.Relations', + ]); $this->hasMany('TodoItems', [ 'foreignKey' => 'user_id', 'className' => 'BakeTest.TodoItems', diff --git a/tests/comparisons/Model/testBakeWithRulesUnique.php b/tests/comparisons/Model/testBakeWithRulesUnique.php index 775080bf..1c4e92eb 100644 --- a/tests/comparisons/Model/testBakeWithRulesUnique.php +++ b/tests/comparisons/Model/testBakeWithRulesUnique.php @@ -12,6 +12,7 @@ * Users Model * * @property \Bake\Test\App\Model\Table\CommentsTable&\Cake\ORM\Association\HasMany $Comments + * @property \Bake\Test\App\Model\Table\RelationsTable&\Cake\ORM\Association\HasMany $Relations * @property \Bake\Test\App\Model\Table\TodoItemsTable&\Cake\ORM\Association\HasMany $TodoItems * * @method \Bake\Test\App\Model\Entity\User newEmptyEntity() @@ -51,6 +52,9 @@ public function initialize(array $config): void $this->hasMany('Comments', [ 'foreignKey' => 'user_id', ]); + $this->hasMany('Relations', [ + 'foreignKey' => 'user_id', + ]); $this->hasMany('TodoItems', [ 'foreignKey' => 'user_id', ]); diff --git a/tests/schema.php b/tests/schema.php index 561ff0f7..b26ac345 100644 --- a/tests/schema.php +++ b/tests/schema.php @@ -383,6 +383,34 @@ ], 'constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]], ], + [ + 'table' => 'relations', + 'columns' => [ + 'id' => ['type' => 'integer'], + 'user_id' => ['type' => 'integer', 'null' => false], + 'other_id' => ['type' => 'integer', 'null' => false], + 'body' => 'text', + 'created' => 'datetime', + 'updated' => 'datetime', + ], + 'constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id']], + 'user_idx' => [ + 'type' => 'foreign', + 'columns' => ['user_id'], + 'references' => ['users', 'id'], + 'update' => 'noAction', + 'delete' => 'noAction', + ], + 'other_idx' => [ + 'type' => 'foreign', + 'columns' => ['other_id'], + 'references' => ['users', 'id'], + 'update' => 'noAction', + 'delete' => 'noAction', + ], + ], + ], [ 'table' => 'invitations', 'columns' => [ diff --git a/tests/test_app/App/Controller/RelationsController.php b/tests/test_app/App/Controller/RelationsController.php new file mode 100644 index 00000000..14140deb --- /dev/null +++ b/tests/test_app/App/Controller/RelationsController.php @@ -0,0 +1,99 @@ +Relations->find(); + $relations = $this->paginate($query); + + $this->set(compact('relations')); + } + + /** + * View method + * + * @param string|null $id Relation id. + * @return \Cake\Http\Response|null|void Renders view + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function view($id = null) + { + $relation = $this->Relations->get($id, contain: []); + $this->set(compact('relation')); + } + + /** + * Add method + * + * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise. + */ + public function add() + { + $relation = $this->Relations->newEmptyEntity(); + if ($this->request->is('post')) { + $relation = $this->Relations->patchEntity($relation, $this->request->getData()); + if ($this->Relations->save($relation)) { + $this->Flash->success(__('The relation has been saved.')); + + return $this->redirect(['action' => 'index']); + } + $this->Flash->error(__('The relation could not be saved. Please, try again.')); + } + $this->set(compact('relation')); + } + + /** + * Edit method + * + * @param string|null $id Relation id. + * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise. + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function edit($id = null) + { + $relation = $this->Relations->get($id, contain: []); + if ($this->request->is(['patch', 'post', 'put'])) { + $relation = $this->Relations->patchEntity($relation, $this->request->getData()); + if ($this->Relations->save($relation)) { + $this->Flash->success(__('The relation has been saved.')); + + return $this->redirect(['action' => 'index']); + } + $this->Flash->error(__('The relation could not be saved. Please, try again.')); + } + $this->set(compact('relation')); + } + + /** + * Delete method + * + * @param string|null $id Relation id. + * @return \Cake\Http\Response|null Redirects to index. + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function delete($id = null) + { + $this->request->allowMethod(['post', 'delete']); + $relation = $this->Relations->get($id); + if ($this->Relations->delete($relation)) { + $this->Flash->success(__('The relation has been deleted.')); + } else { + $this->Flash->error(__('The relation could not be deleted. Please, try again.')); + } + + return $this->redirect(['action' => 'index']); + } +}