Amal_Udjir/tests/Unit/HomepageLayoutTest.php

220 lines
6.3 KiB
PHP

<?php
namespace Tests\Unit;
use Codeception\Test\Unit;
use Codeception\Util\HttpCode;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use PHPUnit\Framework\TestCase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;
use App\Models\PHP\Php_testing_rule;
class HomepageLayoutTest extends TestCase
{
/**
* A basic feature test example.
* ./vendor/bin/phpunit
*/
public function testLatihanNumberOne():void{
// Perform the HTTP GET request
$I = $this->tester; // No need to define $this->tester, just use $I.
$I->sendGET('/phpunit/result-test-student/');
// Query the database for testing rule
$sql = \DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_one'");
$row = $sql[0];
$html = $row->testing_rule;
// Clean up HTML content
$test = str_replace(["\r\n", "\r", "\n", " "], "", $html);
$result_test = htmlspecialchars($test);
// Clean up response content
$result_content = str_replace(["\r\n", "\r", "\n", " "], "", $I->grabResponse());
// Assert that the cleaned-up response contains the expected string
$I->assertContains($result_test, $result_content);
}
public function testLatihanNumberTwo():void{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_two'");
$row = $sql[0];
$html = $row->testing_rule;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
$this->assertStringContainsString($result_test, $result_content);
}
public function testLatihanNumberThree():void{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_three'");
$row = $sql[0];
$html = $row->testing_rule;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
$this->assertStringContainsString($result_test, $result_content);
}
public function testLatihanNumberFour():void{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_four'");
$row = $sql[0];
$html = $row->testing_rule;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
$this->assertStringContainsString($result_test, $result_content);
}
public function testLatihanNumberFive():void{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_five'");
$row = $sql[0];
$html = $row->testing_rule;
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$result_content = str_replace(array("\r\n","\r"," "),"", $response->content());
$this->assertStringContainsString($result_test, $result_content);
}
public function testConditions(): void
{
}
public function test_addition(){
$a = 2;
$b = 3;
$result = $a + $b;
$response = $this->get('/phpunit/result-test-student-add');
$response->assertStatus(200);
$responseContent = $response->getContent();
$this->assertEquals(
$result,
$responseContent,
"The addition of $a and $b should be $result"
);
}
public function test_variable_value_and_type()
{
$variable = "is php example";
$this->assertEquals("this is php example", $variable);
$this->assertIsString($variable);
}
public function test_php_variable_response()
{
// Send GET request to route /execute-php-variable
$response = $this->get("/execute-php-variable");
$response->assertSee("this is php variable example");
// Checks that the response content is a string
$this->assertIsString($response->getContent());
}
public function test_conditional_statement_output()
{
$response = $this->get("/execute-conditional-php/true");
$response->assertSee("conditional statement example if the condition is true");
$this->assertIsString($response->getContent());
}
public function test_conditional_else_statement_output()
{
$response = $this->get("/execute-conditional-php");
$response->assertSee("conditional statement example if the condition is false");
$this->assertIsString($response->getContent());
}
public function test_loop_output()
{
$response = $this->get("/loop-php-example");
$expectedOutput = "1 2 3 4 5 6 7 8 9 10 this is looping php example";
$response->assertSee($expectedOutput);
$this->assertIsString($response->getContent());
}
public function test_array_output()
{
$response = $this->get("/array-php-example");
$response->assertJson([
'first' => 'this is array php example',
'second' => 'another example',
'third' => 'yet another example'
]);
$this->assertContains('this is array php example', $response->json());
}
public function testAddition(): void
{
$result = $this->add(2, 3);
$this->assertEquals(6, $result);
}
/**
* A method to perform addition.
*
* @param int $a
* @param int $b
* @return int
*/
public function add($a, $b): int
{
return $a + $b;
}
}