Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 26, 2024
1 parent dbd22fd commit d15e503
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/Fixture/RelationsFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Bake\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
* InvitationsFixture
*/
class RelationsFixture extends TestFixture
{
/**
* records property
*
* @var array
*/
public array $records = [
[
'user_id' => 1,
'other_id' => 1,
'body' => 'Try it out!',
],
];
}
29 changes: 29 additions & 0 deletions tests/TestCase/Command/ModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions tests/comparisons/Model/testBakeEntityCustomHidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/comparisons/Model/testBakeEntityFullContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,6 +35,7 @@ class User extends Entity
'created' => true,
'updated' => true,
'comments' => true,
'relations' => true,
'todo_items' => true,
];

Expand Down
1 change: 1 addition & 0 deletions tests/comparisons/Model/testBakeEntityHidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/comparisons/Model/testBakeEntitySimple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/comparisons/Model/testBakeEntitySimpleUnchanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/comparisons/Model/testBakeEntityWithPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,6 +35,7 @@ class User extends Entity
'created' => true,
'updated' => true,
'comments' => true,
'relations' => true,
'todo_items' => true,
];

Expand Down
5 changes: 5 additions & 0 deletions tests/comparisons/Model/testBakeTableWithPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions tests/comparisons/Model/testBakeWithRulesUnique.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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',
]);
Expand Down
28 changes: 28 additions & 0 deletions tests/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
99 changes: 99 additions & 0 deletions tests/test_app/App/Controller/RelationsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);

namespace Bake\Test\App\Controller;

/**
* Relations Controller
*
*/
class RelationsController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$query = $this->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']);
}
}

0 comments on commit d15e503

Please sign in to comment.