first commit

This commit is contained in:
Olu Amey
2021-09-13 06:53:04 -04:00
commit 32eb3ab418
3602 changed files with 875408 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use Config\App;
class MockAppConfig extends App
{
public $baseURL = 'http://example.com/';
public $uriProtocol = 'REQUEST_URI';
public $cookiePrefix = '';
public $cookieDomain = '';
public $cookiePath = '/';
public $cookieSecure = false;
public $cookieHTTPOnly = false;
public $cookieSameSite = 'Lax';
public $proxyIPs = '';
public $CSRFProtection = false;
public $CSRFTokenName = 'csrf_test_name';
public $CSRFHeaderName = 'X-CSRF-TOKEN';
public $CSRFCookieName = 'csrf_cookie_name';
public $CSRFExpire = 7200;
public $CSRFRegenerate = true;
public $CSRFExcludeURIs = ['http://example.com'];
public $CSRFRedirect = false;
public $CSRFSameSite = 'Lax';
public $CSPEnabled = false;
public $defaultLocale = 'en';
public $negotiateLocale = false;
public $supportedLocales = [
'en',
'es',
];
}
+27
View File
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use Config\Autoload;
class MockAutoload extends Autoload
{
public $psr4 = [];
public $classmap = [];
public function __construct()
{
// Don't call the parent since we don't want the default mappings.
// parent::__construct();
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\ConnectionInterface;
class MockBuilder extends BaseBuilder
{
public function __construct($tableName, ConnectionInterface &$db, array $options = null)
{
parent::__construct($tableName, $db, $options);
}
}
+47
View File
@@ -0,0 +1,47 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use Config\App;
class MockCLIConfig extends App
{
public $baseURL = 'http://example.com/';
public $uriProtocol = 'REQUEST_URI';
public $cookiePrefix = '';
public $cookieDomain = '';
public $cookiePath = '/';
public $cookieSecure = false;
public $cookieHTTPOnly = false;
public $cookieSameSite = 'Lax';
public $proxyIPs = '';
public $CSRFProtection = false;
public $CSRFTokenName = 'csrf_test_name';
public $CSRFCookieName = 'csrf_cookie_name';
public $CSRFExpire = 7200;
public $CSRFRegenerate = true;
public $CSRFExcludeURIs = ['http://example.com'];
public $CSRFSameSite = 'Lax';
public $CSPEnabled = false;
public $defaultLocale = 'en';
public $negotiateLocale = false;
public $supportedLocales = [
'en',
'es',
];
}
+60
View File
@@ -0,0 +1,60 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\HTTP\CURLRequest;
/**
* Class MockCURLRequest
*
* Simply allows us to not actually call cURL during the
* test runs. Instead, we can set the desired output
* and get back the set options.
*/
class MockCURLRequest extends CURLRequest
{
public $curl_options;
protected $output = '';
//--------------------------------------------------------------------
public function setOutput($output)
{
$this->output = $output;
return $this;
}
//--------------------------------------------------------------------
protected function sendRequest(array $curlOptions = []): string
{
// Save so we can access later.
$this->curl_options = $curlOptions;
return $this->output;
}
//--------------------------------------------------------------------
// for testing purposes only
public function getBaseURI()
{
return $this->baseURI;
}
// for testing purposes only
public function getDelay()
{
return $this->delay;
}
}
+289
View File
@@ -0,0 +1,289 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Cache\Handlers\BaseHandler;
use Closure;
class MockCache extends BaseHandler implements CacheInterface
{
/**
* Mock cache storage.
*
* @var array
*/
protected $cache = [];
/**
* Expiration times.
*
* @var ?int[]
*/
protected $expirations = [];
//--------------------------------------------------------------------
/**
* Takes care of any handler-specific setup that must be done.
*/
public function initialize()
{
}
//--------------------------------------------------------------------
/**
* Attempts to fetch an item from the cache store.
*
* @param string $key Cache item name
*
* @return mixed
*/
public function get(string $key)
{
$key = static::validateKey($key, $this->prefix);
return array_key_exists($key, $this->cache)
? $this->cache[$key]
: null;
}
//--------------------------------------------------------------------
/**
* Get an item from the cache, or execute the given Closure and store the result.
*
* @param string $key Cache item name
* @param integer $ttl Time to live
* @param Closure $callback Callback return value
*
* @return mixed
*/
public function remember(string $key, int $ttl, Closure $callback)
{
$value = $this->get($key);
if (! is_null($value))
{
return $value;
}
$this->save($key, $value = $callback(), $ttl);
return $value;
}
//--------------------------------------------------------------------
/**
* Saves an item to the cache store.
*
* The $raw parameter is only utilized by Mamcache in order to
* allow usage of increment() and decrement().
*
* @param string $key Cache item name
* @param mixed $value the data to save
* @param integer $ttl Time To Live, in seconds (default 60)
* @param boolean $raw Whether to store the raw value.
*
* @return boolean
*/
public function save(string $key, $value, int $ttl = 60, bool $raw = false)
{
$key = static::validateKey($key, $this->prefix);
$this->cache[$key] = $value;
$this->expirations[$key] = $ttl > 0 ? time() + $ttl : null;
return true;
}
//--------------------------------------------------------------------
/**
* Deletes a specific item from the cache store.
*
* @param string $key Cache item name
*
* @return boolean
*/
public function delete(string $key)
{
$key = static::validateKey($key, $this->prefix);
if (! isset($this->cache[$key]))
{
return false;
}
unset($this->cache[$key]);
unset($this->expirations[$key]);
return true;
}
//--------------------------------------------------------------------
/**
* Deletes items from the cache store matching a given pattern.
*
* @param string $pattern Cache items glob-style pattern
*
* @return integer
*/
public function deleteMatching(string $pattern)
{
$count = 0;
foreach (array_keys($this->cache) as $key)
{
if (fnmatch($pattern, $key))
{
$count++;
unset($this->cache[$key]);
unset($this->expirations[$key]);
}
}
return $count;
}
//--------------------------------------------------------------------
/**
* Performs atomic incrementation of a raw stored value.
*
* @param string $key Cache ID
* @param integer $offset Step/value to increase by
*
* @return boolean
*/
public function increment(string $key, int $offset = 1)
{
$key = static::validateKey($key, $this->prefix);
$data = $this->cache[$key] ?: null;
if (empty($data))
{
$data = 0;
}
elseif (! is_int($data))
{
return false;
}
return $this->save($key, $data + $offset);
}
//--------------------------------------------------------------------
/**
* Performs atomic decrementation of a raw stored value.
*
* @param string $key Cache ID
* @param integer $offset Step/value to increase by
*
* @return boolean
*/
public function decrement(string $key, int $offset = 1)
{
$key = static::validateKey($key, $this->prefix);
$data = $this->cache[$key] ?: null;
if (empty($data))
{
$data = 0;
}
elseif (! is_int($data))
{
return false;
}
return $this->save($key, $data - $offset);
}
//--------------------------------------------------------------------
/**
* Will delete all items in the entire cache.
*
* @return boolean
*/
public function clean()
{
$this->cache = [];
$this->expirations = [];
return true;
}
//--------------------------------------------------------------------
/**
* Returns information on the entire cache.
*
* The information returned and the structure of the data
* varies depending on the handler.
*
* @return string[] Keys currently present in the store
*/
public function getCacheInfo()
{
return array_keys($this->cache);
}
//--------------------------------------------------------------------
/**
* Returns detailed information about the specific item in the cache.
*
* @param string $key Cache item name.
*
* @return array|null
* Returns null if the item does not exist, otherwise array<string, mixed>
* with at least the 'expire' key for absolute epoch expiry (or null).
*/
public function getMetaData(string $key)
{
// Misses return null
if (! array_key_exists($key, $this->expirations))
{
return null;
}
// Count expired items as a miss
if (is_int($this->expirations[$key]) && $this->expirations[$key] > time())
{
return null;
}
return [
'expire' => $this->expirations[$key],
];
}
//--------------------------------------------------------------------
/**
* Determines if the driver is supported on this system.
*
* @return boolean
*/
public function isSupported(): bool
{
return true;
}
//--------------------------------------------------------------------
}
+22
View File
@@ -0,0 +1,22 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\CodeIgniter;
class MockCodeIgniter extends CodeIgniter
{
protected function callExit($code)
{
// Do not call exit() in testing.
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
if (! function_exists('is_cli'))
{
/**
* Is CLI?
*
* Test to see if a request was made from the command line.
* You can set the return value for testing.
*
* @param boolean $newReturn return value to set
* @return boolean
*/
function is_cli(bool $newReturn = null): bool
{
// PHPUnit always runs via CLI.
static $returnValue = true;
if ($newReturn !== null)
{
$returnValue = $newReturn;
}
return $returnValue;
}
}
+317
View File
@@ -0,0 +1,317 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\CodeIgniter;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\Query;
class MockConnection extends BaseConnection
{
protected $returnValues = [];
public $database;
public $lastQuery;
//--------------------------------------------------------------------
public function shouldReturn(string $method, $return)
{
$this->returnValues[$method] = $return;
return $this;
}
//--------------------------------------------------------------------
/**
* Orchestrates a query against the database. Queries must use
* Database\Statement objects to store the query and build it.
* This method works with the cache.
*
* Should automatically handle different connections for read/write
* queries if needed.
*
* @param string $sql
* @param mixed ...$binds
* @param boolean $setEscapeFlags
* @param string $queryClass
*
* @return BaseResult|Query|boolean
*
* @todo BC set $queryClass default as null in 4.1
*/
public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = '')
{
$queryClass = str_replace('Connection', 'Query', static::class);
$query = new $queryClass($this);
$query->setQuery($sql, $binds, $setEscapeFlags);
if (! empty($this->swapPre) && ! empty($this->DBPrefix))
{
$query->swapPrefix($this->DBPrefix, $this->swapPre);
}
$startTime = microtime(true);
$this->lastQuery = $query;
// Run the query
if (false === ($this->resultID = $this->simpleQuery($query->getQuery())))
{
$query->setDuration($startTime, $startTime);
// @todo deal with errors
return false;
}
$query->setDuration($startTime);
// resultID is not false, so it must be successful
if ($query->isWriteType())
{
return true;
}
// query is not write-type, so it must be read-type query; return QueryResult
$resultClass = str_replace('Connection', 'Result', get_class($this));
return new $resultClass($this->connID, $this->resultID);
}
//--------------------------------------------------------------------
/**
* Connect to the database.
*
* @param boolean $persistent
*
* @return mixed
*/
public function connect(bool $persistent = false)
{
$return = $this->returnValues['connect'] ?? true;
if (is_array($return))
{
// By removing the top item here, we can
// get a different value for, say, testing failover connections.
$return = array_shift($this->returnValues['connect']);
}
return $return;
}
//--------------------------------------------------------------------
/**
* Keep or establish the connection if no queries have been sent for
* a length of time exceeding the server's idle timeout.
*
* @return boolean
*/
public function reconnect(): bool
{
return true;
}
//--------------------------------------------------------------------
/**
* Select a specific database table to use.
*
* @param string $databaseName
*
* @return mixed
*/
public function setDatabase(string $databaseName)
{
$this->database = $databaseName;
return $this;
}
//--------------------------------------------------------------------
/**
* Returns a string containing the version of the database being used.
*
* @return string
*/
public function getVersion(): string
{
return CodeIgniter::CI_VERSION;
}
//--------------------------------------------------------------------
/**
* Executes the query against the database.
*
* @param string $sql
*
* @return mixed
*/
protected function execute(string $sql)
{
return $this->returnValues['execute'];
}
//--------------------------------------------------------------------
/**
* Returns the total number of rows affected by this query.
*
* @return integer
*/
public function affectedRows(): int
{
return 1;
}
//--------------------------------------------------------------------
/**
* Returns the last error code and message.
*
* Must return an array with keys 'code' and 'message':
*
* return ['code' => null, 'message' => null);
*
* @return array
*/
public function error(): array
{
return [
'code' => null,
'message' => null,
];
}
//--------------------------------------------------------------------
/**
* Insert ID
*
* @return integer
*/
public function insertID(): int
{
return $this->connID->insert_id; // @phpstan-ignore-line
}
//--------------------------------------------------------------------
/**
* Generates the SQL for listing tables in a platform-dependent manner.
*
* @param boolean $constrainByPrefix
*
* @return string
*/
protected function _listTables(bool $constrainByPrefix = false): string
{
return '';
}
//--------------------------------------------------------------------
/**
* Generates a platform-specific query string so that the column names can be fetched.
*
* @param string $table
*
* @return string
*/
protected function _listColumns(string $table = ''): string
{
return '';
}
/**
* @param string $table
* @return array
*/
protected function _fieldData(string $table): array
{
return [];
}
/**
* @param string $table
* @return array
*/
protected function _indexData(string $table): array
{
return [];
}
/**
* @param string $table
* @return array
*/
protected function _foreignKeyData(string $table): array
{
return [];
}
//--------------------------------------------------------------------
/**
* Close the connection.
*/
protected function _close()
{
}
//--------------------------------------------------------------------
/**
* Begin Transaction
*
* @return boolean
*/
protected function _transBegin(): bool
{
return true;
}
//--------------------------------------------------------------------
/**
* Commit Transaction
*
* @return boolean
*/
protected function _transCommit(): bool
{
return true;
}
//--------------------------------------------------------------------
/**
* Rollback Transaction
*
* @return boolean
*/
protected function _transRollback(): bool
{
return true;
}
//--------------------------------------------------------------------
}
+42
View File
@@ -0,0 +1,42 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\Email\Email;
use CodeIgniter\Events\Events;
class MockEmail extends Email
{
/**
* Value to return from mocked send().
*
* @var boolean
*/
public $returnValue = true;
public function send($autoClear = true)
{
if ($this->returnValue)
{
$this->setArchiveValues();
if ($autoClear)
{
$this->clear();
}
Events::trigger('email', $this->archive);
}
return $this->returnValue;
}
}
+40
View File
@@ -0,0 +1,40 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\Events\Events;
/**
* Events
*/
class MockEvents extends Events
{
public function getListeners()
{
return self::$listeners;
}
public function getEventsFile()
{
return self::$files;
}
public function getSimulate()
{
return self::$simulate;
}
public function unInitialize()
{
static::$initialized = false;
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\Log\Handlers\FileHandler;
/**
* Class MockFileLogger
*
* Extends FileHandler, exposing some inner workings
*/
class MockFileLogger extends FileHandler
{
/**
* Where would the log be written?
*/
public $destination;
//--------------------------------------------------------------------
public function __construct(array $config)
{
parent::__construct($config);
$this->handles = $config['handles'] ?? [];
$this->destination = $this->path . 'log-' . date('Y-m-d') . '.' . $this->fileExtension;
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\HTTP\IncomingRequest;
class MockIncomingRequest extends IncomingRequest
{
// public function populateHeaders()
// {
// // Don't do anything... force the tester to manually set the headers they want.
// }
public function detectURI($protocol, $baseURL)
{
// Do nothing...
}
}
+70
View File
@@ -0,0 +1,70 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\Language\Language;
class MockLanguage extends Language
{
/**
* Stores the data that should be
* returned by the 'requireFile()' method.
*
* @var mixed
*/
protected $data;
//--------------------------------------------------------------------
/**
* Sets the data that should be returned by the
* 'requireFile()' method to allow easy overrides
* during testing.
*
* @param array $data
* @param string $file
* @param string|null $locale
*
* @return $this
*/
public function setData(string $file, array $data, string $locale = null)
{
$this->language[$locale ?? $this->locale][$file] = $data;
return $this;
}
//--------------------------------------------------------------------
/**
* Provides an override that allows us to set custom
* data to be returned easily during testing.
*
* @param string $path
*
* @return array
*/
protected function requireFile(string $path): array
{
return $this->data ?? [];
}
//--------------------------------------------------------------------
/**
* Arbitrarily turnoff internationalization support for testing
*/
public function disableIntlSupport()
{
$this->intlSupport = false;
}
}
+108
View File
@@ -0,0 +1,108 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
class MockLogger
{
/*
|--------------------------------------------------------------------------
| Error Logging Threshold
|--------------------------------------------------------------------------
|
| You can enable error logging by setting a threshold over zero. The
| threshold determines what gets logged. Any values below or equal to the
| threshold will be logged. Threshold options are:
|
| 0 = Disables logging, Error logging TURNED OFF
| 1 = Emergency Messages - System is unusable
| 2 = Alert Messages - Action Must Be Taken Immediately
| 3 = Critical Messages - Application component unavailable, unexpected exception.
| 4 = Runtime Errors - Don't need immediate action, but should be monitored.
| 5 = Warnings - Exceptional occurrences that are not errors.
| 6 = Notices - Normal but significant events.
| 7 = Info - Interesting events, like user logging in, etc.
| 8 = Debug - Detailed debug information.
| 9 = All Messages
|
| You can also pass an array with threshold levels to show individual error types
|
| array(1, 2, 3, 8) = Emergency, Alert, Critical, and Debug messages
|
| For a live site you'll usually enable Critical or higher (3) to be logged otherwise
| your log files will fill up very fast.
|
*/
public $threshold = 9;
/*
|--------------------------------------------------------------------------
| Date Format for Logs
|--------------------------------------------------------------------------
|
| Each item that is logged has an associated date. You can use PHP date
| codes to set your own date formatting
|
*/
public $dateFormat = 'Y-m-d';
/*
|--------------------------------------------------------------------------
| Log Handlers
|--------------------------------------------------------------------------
|
| The logging system supports multiple actions to be taken when something
| is logged. This is done by allowing for multiple Handlers, special classes
| designed to write the log to their chosen destinations, whether that is
| a file on the getServer, a cloud-based service, or even taking actions such
| as emailing the dev team.
|
| Each handler is defined by the class name used for that handler, and it
| MUST implement the CodeIgniter\Log\Handlers\HandlerInterface interface.
|
| The value of each key is an array of configuration items that are sent
| to the constructor of each handler. The only required configuration item
| is the 'handles' element, which must be an array of integer log levels.
| This is most easily handled by using the constants defined in the
| Psr\Log\LogLevel class.
|
| Handlers are executed in the order defined in this array, starting with
| the handler on top and continuing down.
|
*/
public $handlers = [
//--------------------------------------------------------------------
// File Handler
//--------------------------------------------------------------------
'Tests\Support\Log\Handlers\TestHandler' => [
/*
* The log levels that this handler will handle.
*/
'handles' => [
'critical',
'alert',
'emergency',
'debug',
'error',
'info',
'notice',
'warning',
],
/*
* Logging Directory Path
*/
'path' => '',
],
];
}
+18
View File
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\Database\Query;
class MockQuery extends Query
{
}
@@ -0,0 +1,32 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\RESTful\ResourceController;
class MockResourceController extends ResourceController
{
public function getModel()
{
return $this->model;
}
public function getModelName()
{
return $this->modelName;
}
public function getFormat()
{
return $this->format;
}
}
@@ -0,0 +1,35 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\RESTful\ResourcePresenter;
class MockResourcePresenter extends ResourcePresenter
{
use ResponseTrait;
public function getModel()
{
return $this->model;
}
public function getModelName()
{
return $this->modelName;
}
public function getFormat()
{
return $this->format;
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\HTTP\Response;
/**
* Class MockResponse
*/
class MockResponse extends Response
{
/**
* If true, will not write output. Useful during testing.
*
* @var boolean
*/
protected $pretend = true;
// for testing
public function getPretend()
{
return $this->pretend;
}
// artificial error for testing
public function misbehave()
{
$this->statusCode = 0;
}
}
+120
View File
@@ -0,0 +1,120 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\Database\BaseResult;
class MockResult extends BaseResult
{
/**
* Gets the number of fields in the result set.
*
* @return integer
*/
public function getFieldCount(): int
{
return 0;
}
//--------------------------------------------------------------------
/**
* Generates an array of column names in the result set.
*
* @return array
*/
public function getFieldNames(): array
{
return [];
}
//--------------------------------------------------------------------
/**
* Generates an array of objects representing field meta-data.
*
* @return array
*/
public function getFieldData(): array
{
return [];
}
//--------------------------------------------------------------------
/**
* Frees the current result.
*
* @return mixed
*/
public function freeResult()
{
}
//--------------------------------------------------------------------
/**
* Moves the internal pointer to the desired offset. This is called
* internally before fetching results to make sure the result set
* starts at zero.
*
* @param integer $n
*
* @return mixed
*/
public function dataSeek($n = 0)
{
}
//--------------------------------------------------------------------
/**
* Returns the result set as an array.
*
* Overridden by driver classes.
*
* @return mixed
*/
protected function fetchAssoc()
{
}
//--------------------------------------------------------------------
/**
* Returns the result set as an object.
*
* Overridden by child classes.
*
* @param string $className
*
* @return object
*/
protected function fetchObject($className = 'stdClass')
{
return new $className;
}
//--------------------------------------------------------------------
/**
* Gets the number of fields in the result set.
*
* @return integer
*/
public function getNumRows(): int
{
return 0;
}
//--------------------------------------------------------------------
}
+25
View File
@@ -0,0 +1,25 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\Security\Security;
class MockSecurity extends Security
{
public function sendCookie(RequestInterface $request)
{
$_COOKIE['csrf_cookie_name'] = $this->hash;
return $this;
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace CodeIgniter\Test\Mock;
use Config\Security as Security;
/**
* @deprecated
*
* @codeCoverageIgnore
*/
class MockSecurityConfig extends Security
{
public $tokenName = 'csrf_test_name';
public $headerName = 'X-CSRF-TOKEN';
public $cookieName = 'csrf_cookie_name';
public $expires = 7200;
public $regenerate = true;
public $redirect = false;
public $samesite = 'Lax';
public $excludeURIs = ['http://example.com'];
}
+38
View File
@@ -0,0 +1,38 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\Config\BaseService;
use CodeIgniter\Autoloader\FileLocator;
class MockServices extends BaseService
{
public $psr4 = [
'Tests/Support' => TESTPATH . '_support/',
];
public $classmap = [];
//--------------------------------------------------------------------
public function __construct()
{
// Don't call the parent since we don't want the default mappings.
// parent::__construct();
}
//--------------------------------------------------------------------
public static function locator(bool $getShared = true)
{
return new FileLocator(static::autoloader());
}
}
+70
View File
@@ -0,0 +1,70 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\Cookie\Cookie;
use CodeIgniter\Session\Session;
/**
* Class MockSession
*
* Provides a safe way to test the Session class itself,
* that doesn't interact with the session or cookies at all.
*/
class MockSession extends Session
{
/**
* Holds our "cookie" data.
*
* @var Cookie[]
*/
public $cookies = [];
public $didRegenerate = false;
/**
* Sets the driver as the session handler in PHP.
* Extracted for easier testing.
*/
protected function setSaveHandler()
{
// session_set_save_handler($this->driver, true);
}
/**
* Starts the session.
* Extracted for testing reasons.
*/
protected function startSession()
{
// session_start();
$this->setCookie();
}
/**
* Takes care of setting the cookie on the client side.
* Extracted for testing reasons.
*/
protected function setCookie()
{
$expiration = $this->sessionExpiration === 0 ? 0 : time() + $this->sessionExpiration;
$this->cookie = $this->cookie->withValue(session_id())->withExpires($expiration);
$this->cookies[] = $this->cookie;
}
public function regenerate(bool $destroy = false)
{
$this->didRegenerate = true;
$_SESSION['__ci_last_regenerate'] = time();
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use BadMethodCallException;
use CodeIgniter\View\Table;
class MockTable extends Table
{
// Override inaccessible protected method
public function __call($method, $params)
{
if (is_callable([$this, '_' . $method]))
{
return call_user_func_array([$this, '_' . $method], $params);
}
throw new BadMethodCallException('Method ' . $method . ' was not found');
}
}