Amal_Udjir/tests/Feature/HomepageLayoutTest_.php
2025-05-07 12:51:49 +07:00

242 lines
7.1 KiB
PHP

<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\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{
$response = $this->get("/phpunit/result-test-student/");
$sql = DB::select("SELECT * FROM php_testing_rule WHERE testing_name = 'testing_number_one'");
$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","\n"," "),"", $response->content());
$this->assertStringContainsString($result_test, $response->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{
$response = $this->get("/phpunit/result-test-student/");
$html = '<?php
$value = 55;
if ($value >= 70) {
echo "Your Score: " . $values;
echo "You passed!";
} else {
echo "Your Score: " . $value;
echo "You did not pass.";
}
?>';
$test = str_replace(array("\r\n","\r","\n"," "),"",$html);
$result_test = htmlspecialchars($test);
$res = '<?php
$value = 55;
if ($value >= 70) {
echo "Your Score: " . $values;
echo "You passed!";
} else {
echo "Your Score: " . $value;
echo "You did not pass.";
}
?>';
$result_content = str_replace(array("\r\n","\r"," "),"", $res);
$this->assertStringContainsString($result_test, $result_content);
}
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;
}
}