| Server IP : 172.67.179.166 / Your IP : 162.159.110.19 Web Server : nginx/1.20.2 System : Linux 172-104-110-161.ip.linodeusercontent.com 3.10.0-1160.36.2.el7.x86_64 #1 SMP Wed Jul 21 11:57:15 UTC 2021 x86_64 User : www ( 1000) PHP Version : 8.1.9 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /www/wwwroot/specs.drivers-asus.com/framework-4.1.3/system/Test/Constraints/ |
Upload File : |
<?php
namespace CodeIgniter\Test\Constraints;
use CodeIgniter\Database\ConnectionInterface;
use PHPUnit\Framework\Constraint\Constraint;
class SeeInDatabase extends Constraint
{
/**
* The number of results that will show in the database
* in case of failure.
*
* @var integer
*/
protected $show = 3;
/**
* @var ConnectionInterface
*/
protected $db;
/**
* Data used to compare results against.
*
* @var array
*/
protected $data;
/**
* SeeInDatabase constructor.
*
* @param ConnectionInterface $db
* @param array $data
*/
public function __construct(ConnectionInterface $db, array $data)
{
$this->db = $db;
$this->data = $data;
}
/**
* Check if data is found in the table
*
* @param mixed $table
*
* @return boolean
*/
public function matches($table): bool
{
return $this->db->table($table)->where($this->data)->countAllResults() > 0;
}
/**
* Get the description of the failure
*
* @param mixed $table
*
* @return string
*/
public function failureDescription($table): string
{
return sprintf(
"a row in the table [%s] matches the attributes \n%s\n\n%s",
$table, $this->toString(JSON_PRETTY_PRINT), $this->getAdditionalInfo($table)
);
}
/**
* Gets additional records similar to $data.
*
* @param string $table
*
* @return string
*/
protected function getAdditionalInfo(string $table): string
{
$builder = $this->db->table($table);
$similar = $builder->where(
array_key_first($this->data),
$this->data[array_key_first($this->data)]
)->limit($this->show)
->get()->getResultArray();
if ($similar !== [])
{
$description = 'Found similar results: ' . json_encode($similar, JSON_PRETTY_PRINT);
}
else
{
// Does the table have any results at all?
$results = $this->db->table($table)
->limit($this->show)
->get()
->getResultArray();
if ($results !== [])
{
return 'The table is empty.';
}
$description = 'Found: ' . json_encode($results, JSON_PRETTY_PRINT);
}
$total = $this->db->table($table)->countAll();
if ($total > $this->show)
{
$description .= sprintf(' and %s others', $total - $this->show);
}
return $description;
}
/**
* Gets a string representation of the constraint
*
* @param integer $options
*
* @return string
*/
public function toString($options = 0): string
{
return json_encode($this->data, $options);
}
}