Skip to content

Commit b55b335

Browse files
committed
Updated test_app with better coverage
1 parent 6f26dda commit b55b335

3 files changed

Lines changed: 13 additions & 11 deletions

File tree

tests/test_app/Controller/NotesController.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,21 @@ public function listUsers()
165165
$this->set(compact('userNamed'));
166166
$userNamedMary = $this->fetchTable('Users')->findByNamed('Mary')->id;
167167
$this->set(compact('userNamedMary'));
168+
$myUserNamed = $this->Notes->MyUsers->findByNamed('John Doe')->id;//Users is a BelongsTo association
169+
$this->set(compact('myUserNamed'));
168170

169171
//UsersTable::findByRole is a magic finder method
170172
$userList = $this->Notes->Users->findByRole('admin')->all()->toArray();
171173
$this->set(compact('userList'));
172174
$userListGuest = $this->fetchTable('Users')->findByRole('guest')->all()->toArray();
173175
$this->set(compact('userListGuest'));
174-
$users = $this->Notes->Users->findAllByFoo(100);
175-
foreach ($users as $user) {
176-
$user->name = strtolower($user->name);
177-
}
176+
$myUsersList = $this->Notes->MyUsers->findByRole('admin')->all()->toArray();
177+
$this->set(compact('myUsersList'));
178+
179+
//UsersTable::findAllByFoo exists it is not a magic method
180+
$users = $this->Notes->Users->findAllByFoo(100) / 20;
181+
$myUsers = $this->Notes->MyUsers->findAllByFoo(100) / 30;
182+
$this->set(compact('users', 'myUsers'));
178183
//BelongsTo should match the correct Users table methods.
179184
$this->Notes->Users->blockOld();
180185
}

tests/test_app/Model/Table/NotesTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @method \App\Model\Entity\Note|\Cake\Datasource\EntityInterface get(mixed $primaryKey, string[]|string $finder = 'all',\Psr\SimpleCache\CacheInterface|string|null $cache = null,\Closure|string|null $cacheKey = null, mixed ...$args)
2222
* @property \App\Model\Table\VeryCustomize00009ArticlesTable&\Cake\ORM\Association\HasMany $VeryCustomize00009Articles
2323
* @property \Cake\ORM\Association\BelongsTo<\App\Model\Table\UsersTable> $Users
24-
* @property \Cake\ORM\Association\BelongsTo&\App\Model\Table\UsersTable $MyUsers
24+
* @property \Cake\ORM\Association\BelongsTo&\App\Model\Table\UsersTable $MyUsers//Don't use generic here, we need this way for testing
2525
*/
2626
class NotesTable extends Table
2727
{

tests/test_app/Model/Table/UsersTable.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,10 @@ public function findByNamed(string $name): User
6161

6262
/**
6363
* @param int $foo
64-
* @return list<\App\Model\Entity\User>
64+
* @return int
6565
*/
66-
public function findAllByFoo(int $foo): array
66+
public function findAllByFoo(int $foo): int
6767
{
68-
return [
69-
new User(['id' => 'first_' . $foo, 'name' => 'John Doe']),
70-
new User(['id' => 'second_' . $foo, 'name' => 'Mary J.']),
71-
];
68+
return $foo * 200;
7269
}
7370
}

0 commit comments

Comments
 (0)