Files
piratepoet/mailpoet/tests/DataFactories/Log.php
Jan Jakes 125b0ab03d Avoid calling flush() in logger
This is to avoid unintended side effect of trying to save modified
or detached entities.

[MAILPOET-5745]
2023-12-04 12:13:51 +01:00

55 lines
1.3 KiB
PHP

<?php declare(strict_types = 1);
namespace MailPoet\Test\DataFactories;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Entities\LogEntity;
use MailPoet\Logging\LogRepository;
use MailPoetVendor\Carbon\Carbon;
class Log {
/** @var array */
private $data;
/** @var LogRepository */
private $repository;
public function __construct() {
$this->repository = ContainerWrapper::getInstance()->get(LogRepository::class);
$this->data = [
'name' => 'Log' . bin2hex(random_bytes(7)),
'level' => 5,
'message' => 'Message' . bin2hex(random_bytes(7)),
'created_at' => Carbon::now(),
];
}
/**
* @return $this
*/
public function withCreatedAt(\DateTimeInterface $date): Log {
return $this->update('created_at', $date);
}
public function create(): LogEntity {
$entity = new LogEntity();
$entity->setName($this->data['name']);
$entity->setLevel($this->data['level']);
$entity->setMessage($this->data['message']);
$entity->setCreatedAt($this->data['created_at']);
$this->repository->saveLog($entity);
return $entity;
}
/**
* @return $this
*/
private function update(string $item, $value): Log {
$data = $this->data;
$data[$item] = $value;
$new = clone $this;
$new->data = $data;
return $new;
}
}