Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Lanin committed May 3, 2017
1 parent f5bd90d commit 6e53755
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 86 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
}
],
"require": {
"php": ">=5.6.4",
"php": ">=5.6.0",
"illuminate/support": "^5.3"
},
"require-dev": {
"orchestra/testbench": "~3.1",
"phpunit/phpunit": "^4.7.6",
"orchestra/testbench": "~3.4",
"phpunit/phpunit": "~5.7",
"mockery/mockery": "0.9.*"
},
"autoload": {
Expand Down
52 changes: 33 additions & 19 deletions tests/ExceptionsOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class ExceptionsOutputTest extends TestCase
public function test_404_error_for_json()
{
$this->json('POST', '/foo')
->seeStatusCode(404)
->seeJsonContains([
->assertStatus(404)
->assertJsonFragment([
'id' => 'not_found',
]);
}
Expand All @@ -24,8 +24,8 @@ public function test_404_error_for_json()
public function test_404_error_for_html()
{
$this->get('/foo')
->seeStatusCode(404)
->see('Not Found');
->assertStatus(404)
->assertSee('Requested object not found');
}

/**
Expand All @@ -39,11 +39,17 @@ public function test_validation_error()
});

$this->json('GET', '/foo')
->seeStatusCode(422)
->seeJsonContains([
->assertStatus(422)
->assertJsonFragment([
'id' => 'validation_failed',
])
->seeJsonMatchesPath('$.meta.errors.name');
->assertJsonStructure([
'meta' => [
'errors' => [
'name'
]
]
]);
}

/**
Expand All @@ -56,8 +62,8 @@ public function test_internal_error_error()
});

$this->json('GET', '/foo')
->seeStatusCode(500)
->seeJsonContains([
->assertStatus(500)
->assertJsonFragment([
'id' => 'internal_server_error',
]);
}
Expand All @@ -74,12 +80,14 @@ public function test_internal_error_in_debug_mode()
});

$this->json('GET', '/foo')
->seeStatusCode(500)
->seeJsonContains([
->assertStatus(500)
->assertJsonFragment([
'id' => 'fatal_error_exception',
'message' => 'Fatal error.'
])
->seeJsonMatchesPath('$.trace');
->assertJsonStructure([
'trace',
]);
}

/**
Expand All @@ -92,11 +100,17 @@ public function test_form_request_validation_fail()
});

$this->json('POST', '/foo', ['foo' => 'bar'])
->seeStatusCode(422)
->seeJsonContains([
->assertStatus(422)
->assertJsonFragment([
'id' => 'validation_failed',
])
->seeJsonMatchesPath('$.meta.errors.name');
->assertJsonStructure([
'meta' => [
'errors' => [
'name'
]
]
]);
}

/**
Expand All @@ -109,8 +123,8 @@ public function test_form_request_validation_passed()
});

$this->json('POST', '/foo', ['name' => 'bar'])
->seeStatusCode(200)
->seeJson([
->assertStatus(200)
->assertJsonFragment([
'name' => 'bar',
]);
}
Expand All @@ -125,8 +139,8 @@ public function test_model_not_found_fail()
});

$this->json('GET', '/foo')
->seeStatusCode(404)
->seeJsonContains([
->assertStatus(404)
->assertJsonFragment([
'id' => 'not_found',
]);
}
Expand Down
64 changes: 0 additions & 64 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPUnit_Framework_ExpectationFailedException as PHPUnitException;
use ReflectionClass;


abstract class TestCase extends BaseTestCase
{
/**
Expand Down Expand Up @@ -86,67 +85,4 @@ protected function getPublicMethod($name, $class)

return $method;
}


/**
* Asserts that the response JSON contains the given path.
*
* @param string $path
*
* @return $this
*
* @throws PHPUnitException
*/
protected function seeJsonMatchesPath($path)
{
$response = json_decode($this->response->getContent(), true);

// Remove heading $. symbols
$search = ltrim($path, '$.');

// Using random string to protect against null values
$notFoundString = Str::random(6);

try {
$this->assertNotEquals(
array_get($response, $search, $notFoundString),
$notFoundString
);
} catch (PHPUnitException $e) {
throw new PHPUnitException("Unable to find provided path [{$path}] in received JSON [{$this->response->getContent()}].");
}

return $this;
}

/**
* Return value from the resulting JSON by path.
*
* @param $path
*
* @return mixed
*/
protected function getValueFromJsonByPath($path)
{
$response = json_decode($this->response->getContent(), true);

// Remove heading $. symbols
$search = ltrim($path, '$.');

// Using random string to protect against null values
$notFoundString = Str::random(6);

try {
$value = array_get($response, $search, $notFoundString);

$this->assertNotEquals(
$value,
$notFoundString
);
} catch (PHPUnitException $e) {
throw new PHPUnitException("Unable to find provided path [{$path}] in received JSON [{$this->response->getContent()}].");
}

return $value;
}
}

0 comments on commit 6e53755

Please sign in to comment.