XBackBone/tests/TestCase.php

72 lines
2.4 KiB
PHP
Raw Normal View History

2020-04-14 22:08:51 +00:00
<?php
namespace Tests;
2020-04-14 22:08:51 +00:00
use PHPUnit\Framework\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
2020-10-01 16:08:35 +00:00
use WithApplication;
2020-04-14 22:08:51 +00:00
protected function setUp(): void
2020-04-14 22:08:51 +00:00
{
2020-10-01 16:08:35 +00:00
parent::setUp();
$_SESSION = []; // ugly workaround to the the session superglobal between tests
$this->createApplication();
2020-09-28 16:20:25 +00:00
}
2020-10-03 14:01:00 +00:00
2020-10-11 15:42:41 +00:00
/**
* @param $key
* @param null $value
*/
2020-10-03 14:01:00 +00:00
public function updateSetting($key, $value = null)
{
if (!$this->database()->query('SELECT `value` FROM `settings` WHERE `key` = '.$this->database()->getPdo()->quote($key))->fetch()) {
$this->database()->query('INSERT INTO `settings`(`key`, `value`) VALUES ('.$this->database()->getPdo()->quote($key).', ?)', $value);
} else {
$this->database()->query('UPDATE `settings` SET `value`=? WHERE `key` = '.$this->database()->getPdo()->quote($key), $value);
}
}
2020-10-11 15:42:41 +00:00
/**
* @param string $email
* @param string $username
* @param string $password
* @return string
*/
2020-10-03 14:01:00 +00:00
public function createAdminUser($email = 'admin@example.com', $username = 'admin', $password = 'admin')
{
2020-10-11 15:42:41 +00:00
return $this->createUser([
'email' => $email,
'username' => $username,
'password' => password_hash($password, PASSWORD_DEFAULT),
]);
}
/**
* @param array $attributes
* @return string
*/
public function createUser($attributes = [])
{
$attributes = array_replace_recursive([
'email' => 'user@example.com',
'username' => 'user',
'password' => password_hash('user', PASSWORD_DEFAULT),
'is_admin' => 1,
'active' => 1,
'user_code' => humanRandomString(5),
'token' => 'token_'.md5(uniqid('', true)),
'max_disk_quota' => -1,
'activate_token' => null,
'ldap' => 0,
'hide_uploads' => 0,
'copy_raw' => 0,
2020-10-25 13:47:31 +00:00
'reset_token' => null,
2020-10-11 15:42:41 +00:00
], $attributes);
2020-10-25 13:47:31 +00:00
$this->database()->query('INSERT INTO `users`(`email`, `username`, `password`, `is_admin`, `active`, `user_code`, `token`, `max_disk_quota`, `activate_token`, `ldap`, `hide_uploads`, `copy_raw`, `reset_token`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', array_values($attributes));
2020-10-03 14:01:00 +00:00
return $this->database()->getPdo()->lastInsertId();
}
}