54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../../../autoload/Domain/Users/UserRepository.php';
|
|
|
|
use Domain\Users\UserRepository;
|
|
|
|
class FakeUsersMdb
|
|
{
|
|
public $last_select = [];
|
|
public $last_get = [];
|
|
private $rows = [];
|
|
|
|
public function __construct( array $rows )
|
|
{
|
|
$this -> rows = $rows;
|
|
}
|
|
|
|
public function select( $table, $columns, $where )
|
|
{
|
|
$this -> last_select = [ 'table' => $table, 'columns' => $columns, 'where' => $where ];
|
|
return $this -> rows;
|
|
}
|
|
|
|
public function get( $table, $columns, $where )
|
|
{
|
|
$this -> last_get = [ 'table' => $table, 'columns' => $columns, 'where' => $where ];
|
|
foreach ( $this -> rows as $row )
|
|
{
|
|
if ( (int)$row['id'] === (int)$where['id'] )
|
|
return $row;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function run_user_repository_tests()
|
|
{
|
|
$rows = [
|
|
[ 'id' => 1, 'email' => 'admin@example.com', 'name' => 'Admin', 'surname' => 'User' ],
|
|
[ 'id' => 3, 'email' => 'user@example.com', 'name' => 'Normal', 'surname' => 'User' ]
|
|
];
|
|
|
|
$fake_mdb = new FakeUsersMdb( $rows );
|
|
$repository = new UserRepository( $fake_mdb );
|
|
|
|
$all = $repository -> all();
|
|
assert_true( count( $all ) === 2, 'Expected all() to return all users.' );
|
|
assert_true( $fake_mdb -> last_select['table'] === 'users', 'Expected all() to query users table.' );
|
|
|
|
$user = $repository -> byId( 3 );
|
|
assert_true( (int)$user['id'] === 3, 'Expected byId() to return matching user.' );
|
|
assert_true( (int)$fake_mdb -> last_get['where']['id'] === 3, 'Expected byId() to query requested id.' );
|
|
}
|