Skip to content

Commit

Permalink
code coverage love-hate-sex-pain
Browse files Browse the repository at this point in the history
  • Loading branch information
Redjik committed Aug 14, 2015
1 parent 5c608a8 commit 2d1e443
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 9 deletions.
9 changes: 1 addition & 8 deletions api/app/Extensions/Controller/RequestValidationTrait.php
Expand Up @@ -134,20 +134,13 @@ public function validateRequest($requestEntity, $validationRules, $limitToReques
$validationRules = array_intersect_key($validationRules, $requestEntity);
}

/** @var Validator $validation */
$validation = $this->getValidationFactory()->make($requestEntity, $validationRules);
if (!$validation instanceof Validator) {
throw new \InvalidArgumentException('Validator must be instance of '.Validator::class);
}

if ($validation->fails()) {
throw new ValidationException($validation->messages());
}

return true;
}

public function getIdOverrideRule($id)
{
return 'same:'.$id;
}
}
1 change: 0 additions & 1 deletion api/app/Models/BaseModel.php
Expand Up @@ -3,7 +3,6 @@
use Carbon\Carbon;
use Bosnadev\Database\Traits\UuidTrait;
use DateTime;
use Illuminate\Validation\Factory as ValidationFactory;

abstract class BaseModel extends \Spira\Model\Model\BaseModel
{
Expand Down
2 changes: 2 additions & 0 deletions api/app/SpiraApplication.php
Expand Up @@ -20,7 +20,9 @@ protected function registerErrorHandling()
ini_set('display_errors', 0);
if ('cli' !== php_sapi_name() && (!ini_get('log_errors') || ini_get('error_log'))) {
// CLI - display errors only if they're not already logged to STDERR
// @codeCoverageIgnoreStart
ini_set('display_errors', 1);
// @codeCoverageIgnoreEnd
}
}

Expand Down
10 changes: 10 additions & 0 deletions api/tests/ResponseTest.php
Expand Up @@ -14,6 +14,16 @@ public function testRedirect()
$this->assertEquals($url, $response->headers->get('location'));
}

public function testCreatedWithRedirect()
{
$url = 'http:://foo.bar';
$response = new ApiResponse;
$response->created($url);

$this->assertEquals(201, $response->getStatusCode());
$this->assertEquals($url, $response->headers->get('location'));
}

public function testRedirectNoUrl()
{
$this->setExpectedExceptionRegExp(
Expand Down
7 changes: 7 additions & 0 deletions api/tests/TransformerTest.php
Expand Up @@ -70,6 +70,13 @@ public function testItem()
$this->assertTrue(is_array($item));
}

public function testTransfomerService()
{
$checkArray = ['item'=>'foo'];
$transformed = $this->transformer->getService()->item(new \Illuminate\Database\Eloquent\Collection($checkArray));
$this->assertEquals($checkArray,$transformed);
}

public function testNonArrayableItem()
{
$array = ['foo' => 'bar'];
Expand Down
8 changes: 8 additions & 0 deletions api/tests/ValidatorTest.php
@@ -1,5 +1,6 @@
<?php

use App\Services\SpiraValidator;
use Rhumsaa\Uuid\Uuid;

class ValidatorTest extends TestCase
Expand All @@ -11,6 +12,13 @@ public function setUp()
$this->validator = $this->app->make('validator');
}

public function testSpiraValidator()
{
$data = ['float' => 'foo'];
$validation = $this->validator->make($data, ['float'=>'float']);
$this->assertInstanceOf(SpiraValidator::class, $validation);
}

public function testPassingFloatValidation()
{
$data = ['float' => 12.042];
Expand Down
62 changes: 62 additions & 0 deletions api/tests/integration/EntityTest.php
Expand Up @@ -306,6 +306,29 @@ public function testPutManyNew()
$this->assertStringStartsWith('http', $object[0]->_self);
}

public function testPutManySomeNew()
{
$entities = factory(App\Models\TestEntity::class, 5)->create();


$entities = array_map(function ($entity){
return $this->prepareEntity($entity);
}, $entities->all());
$entities[0]['entityId'] = (string) Uuid::uuid4();
$entities[1]['entityId'] = (string) Uuid::uuid4();
$rowCount = TestEntity::count();

$this->put('/test/entities', ['data' => $entities]);

$object = json_decode($this->response->getContent());

$this->assertResponseStatus(201);
$this->assertEquals($rowCount + 2, TestEntity::count());
$this->assertTrue(is_array($object));
$this->assertCount(5, $object);
$this->assertStringStartsWith('http', $object[0]->_self);
}

public function testPutManyNewInvalidId()
{
$entities = factory(App\Models\TestEntity::class, 5)->make();
Expand All @@ -326,6 +349,27 @@ public function testPutManyNewInvalidId()
$this->assertEquals($rowCount, TestEntity::count());
}

public function testPutManyNewInvalid()
{
$entities = factory(App\Models\TestEntity::class, 5)->make();

$entities = array_map(function ($entity) {
return array_add($this->prepareEntity($entity), 'multi_word_column_title', 'foobar');
}, $entities->all());

$rowCount = TestEntity::count();

$this->put('/test/entities', ['data' => $entities]);

$object = json_decode($this->response->getContent());

$this->assertCount(5, $object->invalid);
$this->assertObjectHasAttribute('multiWordColumnTitle', $object->invalid[0]);

$this->assertEquals('The multi word column title field must be true or false.', $object->invalid[0]->multiWordColumnTitle[0]->message);
$this->assertEquals($rowCount, TestEntity::count());
}

public function testPatchOne()
{
$entity = factory(App\Models\TestEntity::class)->create();
Expand Down Expand Up @@ -385,6 +429,24 @@ public function testPatchManyInvalidId()
$this->assertEquals('The selected entity id is invalid.', $object->invalid[0]->entityId[0]->message);
}

public function testPatchManyInvalid()
{
$entities = factory(App\Models\TestEntity::class, 5)->create();

$data = array_map(function ($entity) {
return [
'entity_id' => $entity->entity_id,
'multi_word_column_title' => 'foobar'
];
}, $entities->all());

$this->patch('/test/entities', ['data' => $data]);
$object = json_decode($this->response->getContent());

$this->assertObjectHasAttribute('multiWordColumnTitle', $object->invalid[0]);
$this->assertEquals('The multi word column title field must be true or false.', $object->invalid[0]->multiWordColumnTitle[0]->message);
}

public function testDeleteOne()
{
$entity = factory(App\Models\TestEntity::class)->create();
Expand Down

0 comments on commit 2d1e443

Please sign in to comment.