News: Bug fixes, cleanup, comments & formatting

Use more magically available methods and properties
Fixed atom feed and stats not using the new model
main
Igor Scheller 5 years ago
parent 6534191d59
commit b878740f80

@ -1,10 +1,12 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Engelsystem\Migrations; namespace Engelsystem\Migrations;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Migration\Migration; use Engelsystem\Database\Migration\Migration;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use stdClass; use stdClass;
@ -13,7 +15,8 @@ use stdClass;
*/ */
class CreateNewsTable extends Migration class CreateNewsTable extends Migration
{ {
use ChangesReferences, Reference; use ChangesReferences;
use Reference;
/** /**
* Creates the news table, copies the data and drops the News table. * Creates the news table, copies the data and drops the News table.
@ -23,7 +26,7 @@ class CreateNewsTable extends Migration
$hasPreviousNewsTable = $this->schema->hasTable('News'); $hasPreviousNewsTable = $this->schema->hasTable('News');
if ($hasPreviousNewsTable) { if ($hasPreviousNewsTable) {
// rename because some SQL DBMS handle identifiers case insensitive // Rename because some SQL DBMS handle identifiers case insensitive
$this->schema->rename('News', 'PreviousNews'); $this->schema->rename('News', 'PreviousNews');
} }
@ -47,7 +50,7 @@ class CreateNewsTable extends Migration
*/ */
public function down(): void public function down(): void
{ {
// rename because some SQL DBMS handle identifiers case insensitive // Rename as some SQL DBMS handle identifiers case insensitive
$this->schema->rename('news', 'new_news'); $this->schema->rename('news', 'new_news');
$this->createPreviousNewsTable(); $this->createPreviousNewsTable();
@ -59,9 +62,13 @@ class CreateNewsTable extends Migration
'ID', 'ID',
'unsignedInteger' 'unsignedInteger'
); );
$this->schema->drop('new_news'); $this->schema->drop('new_news');
} }
/**
* @return void
*/
private function createNewNewsTable(): void private function createNewNewsTable(): void
{ {
$this->schema->create('news', function (Blueprint $table) { $this->schema->create('news', function (Blueprint $table) {
@ -74,17 +81,20 @@ class CreateNewsTable extends Migration
}); });
} }
/**
* @return void
*/
private function copyPreviousToNewNewsTable(): void private function copyPreviousToNewNewsTable(): void
{ {
$connection = $this->schema->getConnection();
/** @var stdClass[] $previousNewsRecords */ /** @var stdClass[] $previousNewsRecords */
$previousNewsRecords = $this->schema $previousNewsRecords = $connection
->getConnection()
->table('PreviousNews') ->table('PreviousNews')
->get(); ->get();
foreach ($previousNewsRecords as $previousNews) { foreach ($previousNewsRecords as $previousNews) {
$date = Carbon::createFromTimestamp($previousNews->Datum); $date = Carbon::createFromTimestamp($previousNews->Datum);
$this->schema->getConnection()->table('news')->insert([ $connection->table('news')->insert([
'id' => $previousNews->ID, 'id' => $previousNews->ID,
'title' => $previousNews->Betreff, 'title' => $previousNews->Betreff,
'text' => $previousNews->Text, 'text' => $previousNews->Text,
@ -96,6 +106,9 @@ class CreateNewsTable extends Migration
} }
} }
/**
* @return void
*/
private function createPreviousNewsTable(): void private function createPreviousNewsTable(): void
{ {
$this->schema->create('News', function (Blueprint $table) { $this->schema->create('News', function (Blueprint $table) {
@ -104,19 +117,19 @@ class CreateNewsTable extends Migration
$table->string('Betreff', 150) $table->string('Betreff', 150)
->default(''); ->default('');
$table->text('Text'); $table->text('Text');
$table->boolean('Treffen'); $this->references($table, 'users', 'UID');
$table->unsignedInteger('UID'); $table->boolean('Treffen')->default(false);
$table->foreign('UID')
->references('id')
->on('users');
}); });
} }
/**
* @return void
*/
private function copyNewToPreviousNewsTable(): void private function copyNewToPreviousNewsTable(): void
{ {
/** @var stdClass[] $newsRecords */ $connection = $this->schema->getConnection();
$newsRecords = $this->schema /** @var Collection[]|stdClass[] $newsRecords */
->getConnection() $newsRecords = $connection
->table('new_news') ->table('new_news')
->get(); ->get();
@ -124,7 +137,7 @@ class CreateNewsTable extends Migration
$date = Carbon::createFromFormat('Y-m-d H:i:s', $newsRecord->created_at) $date = Carbon::createFromFormat('Y-m-d H:i:s', $newsRecord->created_at)
->getTimestamp(); ->getTimestamp();
$this->schema->getConnection()->table('News')->insert([ $connection->table('News')->insert([
'ID' => $newsRecord->id, 'ID' => $newsRecord->id,
'Datum' => $date, 'Datum' => $date,
'Betreff' => $newsRecord->title, 'Betreff' => $newsRecord->title,

@ -1,13 +1,12 @@
<?php <?php
use Engelsystem\Models\News\News; use Engelsystem\Models\News;
/** /**
* @return string * @return string
*/ */
function admin_news() function admin_news()
{ {
$user = auth()->user();
$request = request(); $request = request();
if (!$request->has('action')) { if (!$request->has('action')) {
@ -45,7 +44,7 @@ function admin_news()
form_info(__('Author'), User_Nick_render($user_source)), form_info(__('Author'), User_Nick_render($user_source)),
form_text('eBetreff', __('Subject'), $news->title), form_text('eBetreff', __('Subject'), $news->title),
form_textarea('eText', __('Message'), $news->text), form_textarea('eText', __('Message'), $news->text),
form_checkbox('eTreffen', __('Meeting'), $news->is_meeting === true, 1), form_checkbox('eTreffen', __('Meeting'), $news->is_meeting, 1),
form_submit('submit', __('Save')) form_submit('submit', __('Save'))
], ],
page_link_to('admin_news', ['action' => 'save', 'id' => $news_id]) page_link_to('admin_news', ['action' => 'save', 'id' => $news_id])

@ -1,7 +1,8 @@
<?php <?php
use Engelsystem\Database\DB;
use Engelsystem\Http\Exceptions\HttpForbidden; use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Models\News;
use Illuminate\Database\Eloquent\Collection;
/** /**
* Publically available page to feed the news to feed readers * Publically available page to feed the news to feed readers
@ -23,15 +24,11 @@ function user_atom()
throw new HttpForbidden('Not allowed', ['content-type' => 'text/text']); throw new HttpForbidden('Not allowed', ['content-type' => 'text/text']);
} }
$news = DB::select(' $news = $request->has('meetings') ? News::whereIsMeeting((bool)$request->get('meetings', false)) : News::query();
SELECT * $news
FROM `News` ->limit((int)config('display_news'))
' . (!$request->has('meetings') ? '' : 'WHERE `Treffen` = 1 ') . ' ->orderByDesc('updated_at');
ORDER BY `ID` $output = make_atom_entries_from_news($news->get());
DESC LIMIT ' . (int)config('display_news')
);
$output = make_atom_entries_from_news($news);
header('Content-Type: application/atom+xml; charset=utf-8'); header('Content-Type: application/atom+xml; charset=utf-8');
header('Content-Length: ' . strlen($output)); header('Content-Length: ' . strlen($output));
@ -39,12 +36,14 @@ function user_atom()
} }
/** /**
* @param array[] $news_entries * @param News[]|Collection $news_entries
* @return string * @return string
*/ */
function make_atom_entries_from_news($news_entries) function make_atom_entries_from_news($news_entries)
{ {
$request = app('request'); $request = app('request');
$updatedAt = isset($news_entries[0]) ? $news_entries[0]->updated_at->format('Y-m-d\TH:i:sP') : '0000:00:00T00:00:00+00:00';
$html = '<?xml version="1.0" encoding="utf-8"?> $html = '<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"> <feed xmlns="http://www.w3.org/2005/Atom">
<title>' . config('app_name') . '</title> <title>' . config('app_name') . '</title>
@ -55,7 +54,7 @@ function make_atom_entries_from_news($news_entries)
$request->getRequestUri() $request->getRequestUri()
)) ))
. '</id> . '</id>
<updated>' . date('Y-m-d\TH:i:sP', $news_entries[0]['Datum']) . '</updated>' . "\n"; <updated>' . $updatedAt . '</updated>' . "\n";
foreach ($news_entries as $news_entry) { foreach ($news_entries as $news_entry) {
$html .= make_atom_entry_from_news($news_entry); $html .= make_atom_entry_from_news($news_entry);
} }
@ -64,21 +63,21 @@ function make_atom_entries_from_news($news_entries)
} }
/** /**
* @param array $news_entry * @param News $news
* @return string * @return string
*/ */
function make_atom_entry_from_news($news_entry) function make_atom_entry_from_news(News $news)
{ {
return ' return '
<entry> <entry>
<title>' . htmlspecialchars($news_entry['Betreff']) . '</title> <title>' . htmlspecialchars($news->title) . '</title>
<link href="' . page_link_to('news_comments', ['nid' => $news_entry['ID']]) . '"/> <link href="' . page_link_to('news_comments', ['nid' => $news->id]) . '"/>
<id>' . preg_replace( <id>' . preg_replace(
'#^https?://#', '#^https?://#',
'', '',
page_link_to('news_comments', ['nid' => $news_entry['ID']]) page_link_to('news_comments', ['nid' => $news->id])
) . '</id> ) . '</id>
<updated>' . date('Y-m-d\TH:i:sP', $news_entry['Datum']) . '</updated> <updated>' . $news->updated_at->format('Y-m-d\TH:i:sP') . '</updated>
<summary type="html">' . htmlspecialchars($news_entry['Text']) . '</summary> <summary type="html">' . htmlspecialchars($news->text) . '</summary>
</entry>' . "\n"; </entry>' . "\n";
} }

@ -1,7 +1,7 @@
<?php <?php
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
use Engelsystem\Models\News\News; use Engelsystem\Models\News;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
@ -43,7 +43,7 @@ function user_meetings()
$page = 0; $page = 0;
} }
$news = News::where('is_meeting', true) $news = News::whereIsMeeting(true)
->orderBy('created_at', 'DESC') ->orderBy('created_at', 'DESC')
->limit($display_news) ->limit($display_news)
->offset($page * $display_news) ->offset($page * $display_news)
@ -53,7 +53,7 @@ function user_meetings()
$html .= display_news($entry); $html .= display_news($entry);
} }
$dis_rows = ceil(News::where('is_meeting', true)->count() / $display_news); $dis_rows = ceil(News::whereIsMeeting(true)->count() / $display_news);
$html .= '<div class="text-center">' . '<ul class="pagination">'; $html .= '<div class="text-center">' . '<ul class="pagination">';
for ($i = 0; $i < $dis_rows; $i++) { for ($i = 0; $i < $dis_rows; $i++) {
if ($request->has('page') && $i == $request->input('page', 0)) { if ($request->has('page') && $i == $request->input('page', 0)) {
@ -110,7 +110,7 @@ function display_news(News $news): string
} }
$html .= '<span class="glyphicon glyphicon-time"></span> ' . $news->created_at->format('Y-m-d H:i') . '&emsp;'; $html .= '<span class="glyphicon glyphicon-time"></span> ' . $news->created_at->format('Y-m-d H:i') . '&emsp;';
$html .= User_Nick_render(User::find($news->user_id)); $html .= User_Nick_render($news->user);
if ($page != 'news_comments') { if ($page != 'news_comments') {
$html .= '&emsp;<a href="' . page_link_to('news_comments', ['nid' => $news->id]) . '">' $html .= '&emsp;<a href="' . page_link_to('news_comments', ['nid' => $news->id]) . '">'
. '<span class="glyphicon glyphicon-comment"></span> ' . '<span class="glyphicon glyphicon-comment"></span> '
@ -137,9 +137,8 @@ function user_news_comments()
if ( if (
$request->has('nid') $request->has('nid')
&& preg_match('/^\d{1,}$/', $nid) && preg_match('/^\d{1,}$/', $nid)
&& News::where('id', $request->input('nid'))->count() > 0 && $news = News::find($nid)
) { ) {
$news = News::find('id');
if ($request->hasPostData('submit') && $request->has('text')) { if ($request->hasPostData('submit') && $request->has('text')) {
$text = $request->input('text'); $text = $request->input('text');
DB::insert(' DB::insert('
@ -180,7 +179,7 @@ function user_news_comments()
$html .= form([ $html .= form([
form_textarea('text', __('Message'), ''), form_textarea('text', __('Message'), ''),
form_submit('submit', __('Save')) form_submit('submit', __('Save'))
], page_link_to('news_comments', ['nid' => $news['ID']])); ], page_link_to('news_comments', ['nid' => $news->id]));
} else { } else {
$html .= __('Invalid request.'); $html .= __('Invalid request.');
} }
@ -199,25 +198,21 @@ function user_news()
$html = '<div class="col-md-12"><h1>' . news_title() . '</h1>' . msg(); $html = '<div class="col-md-12"><h1>' . news_title() . '</h1>' . msg();
$isMeeting = $request->postData('treffen'); $isMeeting = $request->postData('treffen', false);
if ($request->has('text') && $request->has('betreff') && auth()->can('admin_news')) { if ($request->has('text') && $request->has('betreff') && auth()->can('admin_news')) {
if (!$request->has('treffen')) {
$isMeeting = 0;
}
$text = $request->postData('text'); $text = $request->postData('text');
if (!auth()->can('admin_news_html')) { if (!auth()->can('admin_news_html')) {
$text = strip_tags($text); $text = strip_tags($text);
} }
News::create([ $news = News::create([
'title' => strip_tags($request->postData('betreff')), 'title' => strip_tags($request->postData('betreff')),
'text' => $text, 'text' => $text,
'user_id' => $user->id, 'user_id' => $user->id,
'is_meeting' => !!$isMeeting, 'is_meeting' => (bool)$isMeeting,
]); ]);
engelsystem_log('Created news: ' . $request->postData('betreff') . ', treffen: ' . $isMeeting); engelsystem_log('Created news: ' . $news->title . ', is meeting: ' . ($news->is_meeting ? 'yes' : 'no'));
success(__('Entry saved.')); success(__('Entry saved.'));
redirect(page_link_to('news')); redirect(page_link_to('news'));
} }

@ -5,6 +5,7 @@ namespace Engelsystem\Controllers\Metrics;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Database; use Engelsystem\Database\Database;
use Engelsystem\Models\EventConfig; use Engelsystem\Models\EventConfig;
use Engelsystem\Models\News;
use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\Expression as QueryExpression; use Illuminate\Database\Query\Expression as QueryExpression;
@ -209,16 +210,10 @@ class Stats
/** /**
* @param bool $meeting * @param bool $meeting
* @return int * @return int
* @codeCoverageIgnore
*/ */
public function announcements($meeting = null) public function announcements($meeting = null)
{ {
$query = $this $query = is_null($meeting) ? News::query() : News::whereIsMeeting($meeting);
->getQuery('News');
if (!is_null($meeting)) {
$query->where('Treffen', '=', $meeting);
}
return $query->count(); return $query->count();
} }

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Models;
use Carbon\Carbon;
use Engelsystem\Models\User\UsesUserModel;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
* @property int $id
* @property string $title
* @property string $text
* @property bool $is_meeting
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @method static QueryBuilder|LogEntry[] whereId($value)
* @method static QueryBuilder|LogEntry[] whereTitle($value)
* @method static QueryBuilder|LogEntry[] whereText($value)
* @method static QueryBuilder|LogEntry[] whereIsMeeting($value)
* @method static QueryBuilder|LogEntry[] whereCreatedAt($value)
* @method static QueryBuilder|LogEntry[] whereUpdatedAt($value)
*/
class News extends BaseModel
{
use UsesUserModel;
/** @var bool Enable timestamps */
public $timestamps = true;
/** @var array */
protected $casts = [
'is_meeting' => 'boolean',
];
/** @var array */
protected $fillable = [
'title',
'text',
'is_meeting',
'user_id',
];
}

@ -1,30 +0,0 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Models\News;
use Engelsystem\Models\User\UsesUserModel;
use Illuminate\Database\Eloquent\Model;
/**
* This class represents a news item.
*/
class News extends Model
{
use UsesUserModel;
protected $casts = [
'is_meeting' => 'boolean',
];
protected $attributes = [
'is_meeting' => false,
];
protected $fillable = [
'title',
'text',
'is_meeting',
'user_id',
];
}

@ -4,7 +4,7 @@ namespace Engelsystem\Models\User;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Models\BaseModel; use Engelsystem\Models\BaseModel;
use Engelsystem\Models\News\News; use Engelsystem\Models\News;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Builder as QueryBuilder;

@ -5,6 +5,7 @@ namespace Engelsystem\Test\Unit\Controllers\Metrics;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Controllers\Metrics\Stats; use Engelsystem\Controllers\Metrics\Stats;
use Engelsystem\Models\LogEntry; use Engelsystem\Models\LogEntry;
use Engelsystem\Models\News;
use Engelsystem\Models\User\PasswordReset; use Engelsystem\Models\User\PasswordReset;
use Engelsystem\Models\User\PersonalData; use Engelsystem\Models\User\PersonalData;
use Engelsystem\Models\User\State; use Engelsystem\Models\User\State;
@ -26,7 +27,6 @@ class StatsTest extends TestCase
*/ */
public function testNewUsers() public function testNewUsers()
{ {
$this->initDatabase();
$this->addUsers(); $this->addUsers();
$stats = new Stats($this->database); $stats = new Stats($this->database);
@ -38,7 +38,6 @@ class StatsTest extends TestCase
*/ */
public function testVouchers() public function testVouchers()
{ {
$this->initDatabase();
$this->addUsers(); $this->addUsers();
$stats = new Stats($this->database); $stats = new Stats($this->database);
@ -50,7 +49,6 @@ class StatsTest extends TestCase
*/ */
public function testTshirts() public function testTshirts()
{ {
$this->initDatabase();
$this->addUsers(); $this->addUsers();
$stats = new Stats($this->database); $stats = new Stats($this->database);
@ -63,7 +61,6 @@ class StatsTest extends TestCase
*/ */
public function testTshirtSizes() public function testTshirtSizes()
{ {
$this->initDatabase();
$this->addUsers(); $this->addUsers();
$stats = new Stats($this->database); $stats = new Stats($this->database);
@ -75,12 +72,30 @@ class StatsTest extends TestCase
]), $sizes); ]), $sizes);
} }
/**
* @covers \Engelsystem\Controllers\Metrics\Stats::announcements
*/
public function testAnnouncements()
{
$this->addUsers();
$newsData = ['title' => 'Test', 'text' => 'Foo Bar', 'user_id' => 1];
(new News($newsData))->save();
(new News($newsData))->save();
(new News($newsData + ['is_meeting' => true]))->save();
$stats = new Stats($this->database);
$this->assertEquals(3, $stats->announcements());
$this->assertEquals(2, $stats->announcements(false));
$this->assertEquals(1, $stats->announcements(true));
}
/** /**
* @covers \Engelsystem\Controllers\Metrics\Stats::arrivedUsers * @covers \Engelsystem\Controllers\Metrics\Stats::arrivedUsers
*/ */
public function testArrivedUsers() public function testArrivedUsers()
{ {
$this->initDatabase();
$this->addUsers(); $this->addUsers();
$stats = new Stats($this->database); $stats = new Stats($this->database);
@ -92,8 +107,6 @@ class StatsTest extends TestCase
*/ */
public function testSessions() public function testSessions()
{ {
$this->initDatabase();
$this->database $this->database
->getConnection() ->getConnection()
->table('sessions') ->table('sessions')
@ -114,8 +127,6 @@ class StatsTest extends TestCase
*/ */
public function testDatabase() public function testDatabase()
{ {
$this->initDatabase();
$stats = new Stats($this->database); $stats = new Stats($this->database);
$read = $stats->databaseRead(); $read = $stats->databaseRead();
@ -132,8 +143,6 @@ class StatsTest extends TestCase
*/ */
public function testLogEntries() public function testLogEntries()
{ {
$this->initDatabase();
(new LogEntry(['level' => LogLevel::INFO, 'message' => 'Some info']))->save(); (new LogEntry(['level' => LogLevel::INFO, 'message' => 'Some info']))->save();
(new LogEntry(['level' => LogLevel::INFO, 'message' => 'Another info']))->save(); (new LogEntry(['level' => LogLevel::INFO, 'message' => 'Another info']))->save();
(new LogEntry(['level' => LogLevel::CRITICAL, 'message' => 'A critical error!']))->save(); (new LogEntry(['level' => LogLevel::CRITICAL, 'message' => 'A critical error!']))->save();
@ -152,7 +161,6 @@ class StatsTest extends TestCase
*/ */
public function testPasswordResets() public function testPasswordResets()
{ {
$this->initDatabase();
$this->addUsers(); $this->addUsers();
(new PasswordReset(['user_id' => 1, 'token' => 'loremIpsum123']))->save(); (new PasswordReset(['user_id' => 1, 'token' => 'loremIpsum123']))->save();
@ -203,4 +211,14 @@ class StatsTest extends TestCase
->associate($user) ->associate($user)
->save(); ->save();
} }
/**
* Set up the environment
*/
protected function setUp(): void
{
parent::setUp();
$this->initDatabase();
}
} }

@ -1,7 +1,10 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
use Engelsystem\Models\News\News; namespace Engelsystem\Test\Unit\Models;
use Engelsystem\Models\News;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase; use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase; use Engelsystem\Test\Unit\TestCase;
@ -13,14 +16,10 @@ class NewsTest extends TestCase
{ {
use HasDatabase; use HasDatabase;
/** /** @var array */
* @var array
*/
private $newsData; private $newsData;
/** /** @var User */
* @var User
*/
private $user; private $user;
/** /**
@ -31,13 +30,12 @@ class NewsTest extends TestCase
parent::setUp(); parent::setUp();
$this->initDatabase(); $this->initDatabase();
$this->user = User::make([ $this->user = (new User())->create([
'name' => 'lorem', 'name' => 'lorem',
'password' => '', 'password' => '',
'email' => 'foo@bar.batz', 'email' => 'foo@bar.batz',
'api_key' => '', 'api_key' => '',
]); ]);
$this->user->save();
$this->newsData = [ $this->newsData = [
'title' => 'test title', 'title' => 'test title',
@ -53,7 +51,8 @@ class NewsTest extends TestCase
*/ */
public function testCreateDefault(): void public function testCreateDefault(): void
{ {
$news = News::create($this->newsData); $news = (new News())->create($this->newsData);
$news = $news->find($news->id);
$this->assertSame(1, $news->id); $this->assertSame(1, $news->id);
$this->assertSame($this->newsData['title'], $news->title); $this->assertSame($this->newsData['title'], $news->title);
@ -68,9 +67,10 @@ class NewsTest extends TestCase
*/ */
public function testCreate(): void public function testCreate(): void
{ {
$news = News::create( $news = (new News())->create(
$this->newsData + ['is_meeting' => true,] $this->newsData + ['is_meeting' => true]
); );
$news = $news->find($news->id);
$this->assertSame(1, $news->id); $this->assertSame(1, $news->id);
$this->assertSame($this->newsData['title'], $news->title); $this->assertSame($this->newsData['title'], $news->title);

@ -3,7 +3,8 @@
namespace Engelsystem\Test\Unit\Models; namespace Engelsystem\Test\Unit\Models;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Engelsystem\Models\News\News; use Engelsystem\Models\BaseModel;
use Engelsystem\Models\News;
use Engelsystem\Models\User\Contact; use Engelsystem\Models\User\Contact;
use Engelsystem\Models\User\HasUserModel; use Engelsystem\Models\User\HasUserModel;
use Engelsystem\Models\User\PersonalData; use Engelsystem\Models\User\PersonalData;
@ -19,6 +20,7 @@ class UserTest extends TestCase
use ArraySubsetAsserts; use ArraySubsetAsserts;
use HasDatabase; use HasDatabase;
/** @var string[] */
protected $data = [ protected $data = [
'name' => 'lorem', 'name' => 'lorem',
'password' => '', 'password' => '',
@ -26,15 +28,6 @@ class UserTest extends TestCase
'api_key' => '', 'api_key' => '',
]; ];
/**
* Prepare test
*/
protected function setUp(): void
{
parent::setUp();
$this->initDatabase();
}
/** /**
* @return array * @return array
*/ */
@ -103,23 +96,25 @@ class UserTest extends TestCase
} }
/** /**
* @covers User::news() * @covers \Engelsystem\Models\User\User::news()
* *
* @dataProvider hasManyRelationsProvider * @dataProvider hasManyRelationsProvider
* *
* @param string $class Class name of the related models * @param string $class Class name of the related models
* @param string $name Name of the accessor for the related models * @param string $name Name of the accessor for the related models
* @param array $data List of the related models * @param array $modelData List of the related models
*/ */
public function testHasManyRelations(string $class, string $name, array $data): void public function testHasManyRelations(string $class, string $name, array $modelData): void
{ {
$user = new User($this->data); $user = new User($this->data);
$user->save(); $user->save();
$relatedModelIds = []; $relatedModelIds = [];
foreach ($data as $d) { foreach ($modelData as $data) {
$stored = $class::create($d + ['user_id' => $user->id]); /** @var BaseModel $model */
$model = (new $class);
$stored = $model->create($data + ['user_id' => $user->id]);
$relatedModelIds[] = $stored->id; $relatedModelIds[] = $stored->id;
} }
@ -127,7 +122,7 @@ class UserTest extends TestCase
} }
/** /**
* @return array * @return array[]
*/ */
public function hasManyRelationsProvider(): array public function hasManyRelationsProvider(): array
{ {
@ -150,4 +145,13 @@ class UserTest extends TestCase
] ]
]; ];
} }
/**
* Prepare test
*/
protected function setUp(): void
{
parent::setUp();
$this->initDatabase();
}
} }

Loading…
Cancel
Save