Skip to content

Commit

Permalink
Added test for news archive
Browse files Browse the repository at this point in the history
Increased test coverage by adding functional test for news archive #9
  • Loading branch information
Markkaz committed Jul 7, 2021
1 parent 7a55fd3 commit 4a721a0
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 92 deletions.
111 changes: 55 additions & 56 deletions archief.php
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
<?php
error_reporting(E_ALL);

session_start();

/* Classes importeren */
include('Classes/User.php');
include('Classes/Template.php');

/* Includes importeren */
include('Includes/connect.php');
include('Includes/slashes.php');

/* Classes initialiseren */
$cUser = new User();
$cTPL = new Template('Templates/main.tpl');

/* Verbinding met de database maken */
connectDB();

/* Controleren of je bent ingelogd */
include('Includes/login.php');

/* Data op pagina zetten */

$cTPL -> setPlace('TITEL', 'Nieuws archief');
$cTPL -> setFile('CONTENT', 'Templates/archief.tpl');
$cTPL -> parse();

$sQuery = "SELECT nieuwsid, titel, datum, tijd FROM nieuws ORDER BY datum DESC, titel DESC;";
$sBG = '';
if($cResult = mysql_query($sQuery))
{
while($aData = mysql_fetch_assoc($cResult))
{
if($sBG == '')
{
$sBG = 'img/patroon.gif';
}
else
{
$sBG = '';
}
$cTPL -> setBlock('NIEUWS', 'nieuws');
$cTPL -> parse();

$cTPL -> setPlace('BG', $sBG);
$cTPL -> setPlace('ID', add($aData['nieuwsid']));
$cTPL -> setPlace('TITEL', add($aData['titel']));
$cTPL -> setPlace('DATUM', add($aData['datum'] . ' ' . $aData['tijd']));
$cTPL -> parse();
}
}

$cTPL -> show();
?>
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

session_start();

/* Classes importeren */
include_once('Classes/User.php');
include_once('Classes/Template.php');

/* Includes importeren */
include_once('Includes/connect.php');
include_once('Includes/slashes.php');

/* Classes initialiseren */
$cUser = new User();
$cTPL = new Template('Templates/main.tpl');

/* Verbinding met de database maken */
connectDB();

/* Controleren of je bent ingelogd */
include('Includes/login.php');

/* Data op pagina zetten */

$cTPL -> setPlace('TITEL', 'Nieuws archief');
$cTPL -> setFile('CONTENT', 'Templates/archief.tpl');
$cTPL -> parse();

$sQuery = "SELECT nieuwsid, titel, datum, tijd FROM nieuws ORDER BY datum DESC, titel DESC;";
$sBG = '';
if($cResult = mysql_query($sQuery))
{
while($aData = mysql_fetch_assoc($cResult))
{
if($sBG == '')
{
$sBG = 'img/patroon.gif';
}
else
{
$sBG = '';
}
$cTPL -> setBlock('NIEUWS', 'nieuws');
$cTPL -> parse();

$cTPL -> setPlace('BG', $sBG);
$cTPL -> setPlace('ID', add($aData['nieuwsid']));
$cTPL -> setPlace('TITEL', add($aData['titel']));
$cTPL -> setPlace('DATUM', add($aData['datum'] . ' ' . $aData['tijd']));
$cTPL -> parse();
}
}

$cTPL -> show();
24 changes: 24 additions & 0 deletions tests/Factories/NewsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php


namespace Tests\Factories;


class NewsFactory
{
public static function create(\PDO $pdo, $userId, $title, $body)
{
$sql = 'INSERT INTO nieuws
(userid, titel, bericht, datum, tijd)
VALUES
(?, ?, ?, NOW(), NOW());';
$query = $pdo->prepare($sql);
$query->execute([
$userId,
$title,
$body
]);

return $pdo->lastInsertId();
}
}
27 changes: 27 additions & 0 deletions tests/Factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php


namespace Tests\Factories;


use Tests\Pages\IndexTest;

class UserFactory
{
public static function create(\PDO $pdo, $name, $password, $email, $ip)
{
$sql = 'INSERT INTO users
(username, password, email, ip, activate, permis, posts, datum)
VALUES
(?, SHA2(?, 0), ?, ?, 1, 0, 0, NOW())';
$query = $pdo->prepare($sql);
$query->execute([
$name,
$password,
$email,
$ip
]);

return $pdo->lastInsertId();
}
}
47 changes: 11 additions & 36 deletions tests/Pages/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tests\Pages;

use Tests\Factories\NewsFactory;
use Tests\Factories\UserFactory;
use Tests\TestCase;

class IndexTest extends TestCase
Expand All @@ -16,16 +18,17 @@ protected function tearDown()
/** @test */
public function it_shows_top_5_last_news_items()
{
$userId = $this->createUser(
$userId = UserFactory::create(
self::$pdo,
'Mark',
'newpassword',
'[email protected]',
'127.0.0.1'
);

$this->createNewsItem($userId, 'Invisible news item', 'This news item is invisible');
NewsFactory::create(self::$pdo, $userId, 'Invisible news item', 'This news item is invisible');
foreach (range(1, 5) as $i) {
$this->createNewsItem($userId, 'News item '.$i, 'Body of news item '.$i);
NewsFactory::create(self::$pdo, $userId, 'News item '.$i, 'Body of news item '.$i);
}

$page = $this->visitPage(
Expand All @@ -39,15 +42,15 @@ public function it_shows_top_5_last_news_items()
/** @test */
public function it_shows_amount_of_comments_with_a_news_item()
{
$userId = $this->createUser(
$userId = UserFactory::create(
self::$pdo,
'Mark',
'newpassword',
'newspassword',
'[email protected]',
'127.0.0.1'
);

$newsId = $this->createNewsItem($userId, 'A news item', 'And some content on the news item');

$newsId = NewsFactory::create(self::$pdo, $userId, 'A news item', 'And some content on the news item');
foreach (range(1, 5) as $item) {
$this->createNewsComment($newsId, $userId, 'A reply');
}
Expand Down Expand Up @@ -92,35 +95,7 @@ public function it_shows_three_highlighted_games()

protected function createUser($name, $password, $email, $ip)
{
$sql = 'INSERT INTO users
(username, password, email, ip, activate, permis, posts, datum)
VALUES
(?, SHA2(?, 0), ?, ?, 1, 0, 0, NOW())';
$query = self::$pdo->prepare($sql);
$query->execute([
$name,
$password,
$email,
$ip
]);

return self::$pdo->lastInsertId();
}

protected function createNewsItem($userId, $title, $body)
{
$sql = 'INSERT INTO nieuws
(userid, titel, bericht, datum, tijd)
VALUES
(?, ?, ?, NOW(), NOW());';
$query = self::$pdo->prepare($sql);
$query->execute([
$userId,
$title,
$body
]);

return self::$pdo->lastInsertId();
return UserFactory::create(self::$pdo, $name, $password, $email, $ip);
}

protected function createConsole($name)
Expand Down
42 changes: 42 additions & 0 deletions tests/Pages/NewsArchiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Tests\Pages;

use Tests\Factories\NewsFactory;
use Tests\Factories\UserFactory;
use Tests\TestCase;

class NewsArchiveTest extends TestCase
{
/** @test */
public function it_shows_news_messages()
{
$userId = UserFactory::create(
self::$pdo,
'Mark',
'random',
'[email protected]',
'127.0.0.1'
);

NewsFactory::create(
self::$pdo,
$userId,
'First news item',
'The body of the first news item'
);
NewsFactory::create(
self::$pdo,
$userId,
'Second news item',
'The body of the second news item'
);

$page = $this->visitPage(
__DIR__ . '/../../archief.php'
);

$this->assertContains('First news item', $page);
$this->assertContains('Second news item', $page);
}
}

0 comments on commit 4a721a0

Please sign in to comment.