-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increased test coverage by adding functional test for news archive #9
- Loading branch information
Showing
5 changed files
with
159 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
namespace Tests\Pages; | ||
|
||
use Tests\Factories\NewsFactory; | ||
use Tests\Factories\UserFactory; | ||
use Tests\TestCase; | ||
|
||
class IndexTest extends TestCase | ||
|
@@ -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( | ||
|
@@ -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'); | ||
} | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |