summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2019-11-10 21:30:26 +0100
committerIgor Scheller <igor.scheller@igorshp.de>2019-11-10 22:10:11 +0100
commitb878740f80ce7cfe2a0bc53956e3f7e4e0aa2f78 (patch)
tree4003b59c5ea2a731f8881711b95e2b736ae04626 /tests
parent6534191d59521e1e5a88638b1c6a1c77f74dd12a (diff)
News: Bug fixes, cleanup, comments & formatting
Use more magically available methods and properties Fixed atom feed and stats not using the new model
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/Controllers/Metrics/StatsTest.php42
-rw-r--r--tests/Unit/Models/NewsTest.php (renamed from tests/Unit/Models/News/NewsTest.php)24
-rw-r--r--tests/Unit/Models/User/UserTest.php40
3 files changed, 64 insertions, 42 deletions
diff --git a/tests/Unit/Controllers/Metrics/StatsTest.php b/tests/Unit/Controllers/Metrics/StatsTest.php
index 9204f7db..54add688 100644
--- a/tests/Unit/Controllers/Metrics/StatsTest.php
+++ b/tests/Unit/Controllers/Metrics/StatsTest.php
@@ -5,6 +5,7 @@ namespace Engelsystem\Test\Unit\Controllers\Metrics;
use Carbon\Carbon;
use Engelsystem\Controllers\Metrics\Stats;
use Engelsystem\Models\LogEntry;
+use Engelsystem\Models\News;
use Engelsystem\Models\User\PasswordReset;
use Engelsystem\Models\User\PersonalData;
use Engelsystem\Models\User\State;
@@ -26,7 +27,6 @@ class StatsTest extends TestCase
*/
public function testNewUsers()
{
- $this->initDatabase();
$this->addUsers();
$stats = new Stats($this->database);
@@ -38,7 +38,6 @@ class StatsTest extends TestCase
*/
public function testVouchers()
{
- $this->initDatabase();
$this->addUsers();
$stats = new Stats($this->database);
@@ -50,7 +49,6 @@ class StatsTest extends TestCase
*/
public function testTshirts()
{
- $this->initDatabase();
$this->addUsers();
$stats = new Stats($this->database);
@@ -63,7 +61,6 @@ class StatsTest extends TestCase
*/
public function testTshirtSizes()
{
- $this->initDatabase();
$this->addUsers();
$stats = new Stats($this->database);
@@ -75,12 +72,30 @@ class StatsTest extends TestCase
]), $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
*/
public function testArrivedUsers()
{
- $this->initDatabase();
$this->addUsers();
$stats = new Stats($this->database);
@@ -92,8 +107,6 @@ class StatsTest extends TestCase
*/
public function testSessions()
{
- $this->initDatabase();
-
$this->database
->getConnection()
->table('sessions')
@@ -114,8 +127,6 @@ class StatsTest extends TestCase
*/
public function testDatabase()
{
- $this->initDatabase();
-
$stats = new Stats($this->database);
$read = $stats->databaseRead();
@@ -132,8 +143,6 @@ class StatsTest extends TestCase
*/
public function testLogEntries()
{
- $this->initDatabase();
-
(new LogEntry(['level' => LogLevel::INFO, 'message' => 'Some info']))->save();
(new LogEntry(['level' => LogLevel::INFO, 'message' => 'Another info']))->save();
(new LogEntry(['level' => LogLevel::CRITICAL, 'message' => 'A critical error!']))->save();
@@ -152,7 +161,6 @@ class StatsTest extends TestCase
*/
public function testPasswordResets()
{
- $this->initDatabase();
$this->addUsers();
(new PasswordReset(['user_id' => 1, 'token' => 'loremIpsum123']))->save();
@@ -203,4 +211,14 @@ class StatsTest extends TestCase
->associate($user)
->save();
}
+
+ /**
+ * Set up the environment
+ */
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $this->initDatabase();
+ }
}
diff --git a/tests/Unit/Models/News/NewsTest.php b/tests/Unit/Models/NewsTest.php
index 7309c0b0..18ca2d9a 100644
--- a/tests/Unit/Models/News/NewsTest.php
+++ b/tests/Unit/Models/NewsTest.php
@@ -1,7 +1,10 @@
<?php
+
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\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;
@@ -13,14 +16,10 @@ class NewsTest extends TestCase
{
use HasDatabase;
- /**
- * @var array
- */
+ /** @var array */
private $newsData;
- /**
- * @var User
- */
+ /** @var User */
private $user;
/**
@@ -31,13 +30,12 @@ class NewsTest extends TestCase
parent::setUp();
$this->initDatabase();
- $this->user = User::make([
+ $this->user = (new User())->create([
'name' => 'lorem',
'password' => '',
'email' => 'foo@bar.batz',
'api_key' => '',
]);
- $this->user->save();
$this->newsData = [
'title' => 'test title',
@@ -53,7 +51,8 @@ class NewsTest extends TestCase
*/
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($this->newsData['title'], $news->title);
@@ -68,9 +67,10 @@ class NewsTest extends TestCase
*/
public function testCreate(): void
{
- $news = News::create(
- $this->newsData + ['is_meeting' => true,]
+ $news = (new News())->create(
+ $this->newsData + ['is_meeting' => true]
);
+ $news = $news->find($news->id);
$this->assertSame(1, $news->id);
$this->assertSame($this->newsData['title'], $news->title);
diff --git a/tests/Unit/Models/User/UserTest.php b/tests/Unit/Models/User/UserTest.php
index 96c2c1b7..da121a4f 100644
--- a/tests/Unit/Models/User/UserTest.php
+++ b/tests/Unit/Models/User/UserTest.php
@@ -3,7 +3,8 @@
namespace Engelsystem\Test\Unit\Models;
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\HasUserModel;
use Engelsystem\Models\User\PersonalData;
@@ -19,6 +20,7 @@ class UserTest extends TestCase
use ArraySubsetAsserts;
use HasDatabase;
+ /** @var string[] */
protected $data = [
'name' => 'lorem',
'password' => '',
@@ -27,15 +29,6 @@ class UserTest extends TestCase
];
/**
- * Prepare test
- */
- protected function setUp(): void
- {
- parent::setUp();
- $this->initDatabase();
- }
-
- /**
* @return array
*/
public function hasOneRelationsProvider()
@@ -103,23 +96,25 @@ class UserTest extends TestCase
}
/**
- * @covers User::news()
+ * @covers \Engelsystem\Models\User\User::news()
*
* @dataProvider hasManyRelationsProvider
*
- * @param string $class Class name of the related models
- * @param string $name Name of the accessor for the related models
- * @param array $data List 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 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->save();
$relatedModelIds = [];
- foreach ($data as $d) {
- $stored = $class::create($d + ['user_id' => $user->id]);
+ foreach ($modelData as $data) {
+ /** @var BaseModel $model */
+ $model = (new $class);
+ $stored = $model->create($data + ['user_id' => $user->id]);
$relatedModelIds[] = $stored->id;
}
@@ -127,7 +122,7 @@ class UserTest extends TestCase
}
/**
- * @return array
+ * @return array[]
*/
public function hasManyRelationsProvider(): array
{
@@ -150,4 +145,13 @@ class UserTest extends TestCase
]
];
}
+
+ /**
+ * Prepare test
+ */
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->initDatabase();
+ }
}