I'm running Laravel, when trying to develop automated there's an issue. I have the following Test:
class SubmitSurvivorTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
//Test for submit with missing parameters
public function testEmptySubmit()
{
$response = $this->get('/submit/survivor');
$response->assertSee('422');
}
//Test for submit with complete parameters
public function testCorrectSubmit()
{
$response = $this->get('/submit/survivor?name=Jhon+Doe&age=22&gender=m&latitude=0&longitude=0&water=6&food=4&medicine=9&ammo=4');
$response->assertSee('666');
}
}
The first one should return true since if i go to /submit/survivor I'll get
{"status":"422","message":"The request could not be delivered since it's missing one or more paramters"}
The second one should return false since I get
{"status":"204","message":"Created successfully"}
But still the second one returns true aswell. No matter the value i put into the second assertSee running phpunit always gives me 0 failures. That is true for every other test aswell, it seems to me that it's only validating the first method.
I've tried changing it to
public function testEmptySubmit()
{
$response = $this->get('/submit/survivor');
$response->assertSee('422');
$asd = $this->get('/submit/survivor?name=Jhon+Doe&age=22&gender=m&latitude=0&longitude=0&water=6&food=4&medicine=9&ammo=4');
$asd->assertSee('666');
}
And still no failures, but if i change the value in the first one I still get a failure.
Changing the order didn't seem to have any impact, I changed the testCorrectSubmit to above the testEmptySubmit and it still wouldn't work.
If i change it to assertDontSee in the second one it gives a failure, even tho it should return true, the error is too large to even fit into my terminal but is something along the lines of
[A ton of lines with html,js and stuff i can't identify]
</script>\n
</body>\n
</html>\n
' does not contain "666".
C:\xampp\htdocs\mysurvivorapi\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:331
C:\xampp\htdocs\mysurvivorapi\tests\Feature\submitSurvivorTest.php:27
FAILURES!
Tests: 6, Assertions: 6, Failures: 1.
I can't even see the "Failed to assert that" but i just assume it's there
Using assertSeeText seems just as weird since
public function testCorrectSubmit()
{
$response = $this->get('/submit/survivor?name=Jhon+Doe&age=22&gender=m&latitude=0&longitude=0&water=6&food=4&medicine=9&ammo=4');
$response->assertSeeText('Jhon');
}
returns no erros, but if i change 'Jhon' to '666' it returns an error even tho neither of them are in the page
According to the documentation it should look like
public function testCorrectSubmit()
{
$response = $this->get('/submit/survivor',[
'name' => 'Jhon Doe',
'age' => '22',
'gender' => 'm',
'latitude' => '0',
'longitude' => '0',
'water' => '6',
'food'=>'4',
'medicine' =>'9',
'ammo' => '4']);
$response->assertSeeText('Jhon');
}
which works partially, it returns
Failed asserting that '{"status":"422","message":"The request could not be delivered since it's missing one or more paramters"}' contains "Jhon".
which shows me that it's making the request but it's not sending the GET parameters.
I did everything up to step 3 from @Spacemudd, since I have no idea how to do step 4, now it is sending the GET parameters since now Jhon Doe appears in the DB. But now the phpunit is returning
C:\xampp\htdocs\mysurvivorapi>php vendor/phpunit/phpunit/phpunit
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.
.{"status":"204","message":"Created successfully"}
and that's it, no failures no ok no nothing, but if I go to the survivors table I can see that Jhon Doe was inserted and I guess that's an improvement?
Turns out that it was working as intended. The last weird behaviour was due to the fact that I had other tests that were just as wrong as the testCorrectSubmit. Removing them solved the issue, and now I'll rewrite the correctly.
Turns out that I was wrong again, it seems that even with the correct structure and no other tests testing the complete submit fails. I deleted all my tests, created a new one using php artisan make:test submitSurvivorTest which looks like this
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class submitSurvivorTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testSubmitEmpty()
{
$route = route('submit.survivor');
$response = $this->get($route);
$response->assertSee('422');
}
public function testSubmitComplete()
{
$route = route('submit.survivor', [
'name' => 'Jhon Doe',
'age' => '22',
'gender' => 'm',
'latitude' => '0',
'longitude' => '0',
'water' => '6',
'food'=> '4',
'medicine' => '9',
'ammo' => '4',
]);
$response = $this->get($route);
$response->assertStatus(200);
}
}
I've put names into my routes file and it looks like this:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//Go to Index
Route::get('/', 'Controller@goHome');
//Submit a new Survivor
Route::name('submit.survivor')->get('/submit/survivor', 'SurvivorsController@submit');
//See a survivor, if no survivor id is submited sees all registered survivors
Route::name('request.survivors')->get('/request/survivors', 'SurvivorsController@getSurvivors');
//See statistics from the survivors, like average water per survivor for example.
Route::name('request.survivors.reports')->get('/request/survivors/reports', 'SurvivorsController@getReportsSurvivors');
//Update a survivor location
Route::name('update.survivor.location')->get('/update/survivor/location', 'SurvivorsController@updateSurvivorLocation');
//Flag a survivor as contaminated
Route::name('update.survivor.flag')->get('/update/survivor/flag', 'LogsController@submitFlag');
//Trade items between survivors
Route::name('update.survivors.trade')->get('/update/survivor/trade', 'TradesController@submitTrade');
and my submit method is simply
public function submit(Request $request){
//Check if the required variables where sent, and if no it'll return an error
$validator = Validator::make($request->all(), [
'name' => 'required',
'age' => 'required',
'gender' => 'required',
'latitude' => 'required',
'longitude' => 'required',
'water' => 'required',
'food' => 'required',
'medicine' => 'required',
'ammo' => 'required'
]);
if ($validator->fails()) {
$result = array('status' => '422', 'message' => 'The request could not be delivered since it\'s missing one or more paramters');
echo json_encode($result);
}
//Create a new survivor
$survivor = new Survivor;
$survivor->name = $request->input('name');
$survivor->age = $request->input('age');
$survivor->gender = $request->input('gender');
$survivor->latitude = $request->input('latitude');
$survivor->longitude = $request->input('longitude');
$survivor->water = $request->input('water');
$survivor->food = $request->input('food');
$survivor->medicine = $request->input('medicine');
$survivor->ammunition = $request->input('ammo');
//Save survivor
$survivor->save();
//Set the success message
$result = array('status' => '204', 'message' => 'Created successfully');
//Save the change into the logs and display the result
app( 'App\Http\Controllers\LogsController')->submitLog($survivor->id,$survivor->id,"register",$result);
exit();
}
All the functions work directly into the browser, they just don't work with the automated tests.
As of this moment running returns
C:\xampp\htdocs\mysurvivorapi>php vendor/phpunit/phpunit/phpunit
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.
..Array{"status":"204","message":"Created successfully"}
and by using --testdox-html it seems to me that it stops it's execution even before displaying the result of the first test, which works fine
It seems that running with --process-isolation avoids the crashing and the test performs just fine. I've changed all the echo $response to return, as it seems to be better that way. Now it validates the emptySubmit but doesn't validate the completeSubmit
By removing the exit() now the test runs without --process-isolation and returns
Failed asserting that '' contains "204".
C:\xampp\htdocs\mysurvivorapi\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:253
C:\xampp\htdocs\mysurvivorapi\tests\Feature\submitSurvivorTest.php:36
FAILURES!
Tests: 4, Assertions: 4, Failures: 1.
But was that was fixed by making it so the response is returned directly in the submit method and not in the log. Now it seems to be working properly as it now returns responses based on the validations. I'll develop the rest of the automated tests and see if the problem shows up again.