Skip to content

Instantly share code, notes, and snippets.

@akatche
Created April 18, 2019 11:15
Show Gist options
  • Save akatche/144cec08ca3093efdd0915d74f43f2f0 to your computer and use it in GitHub Desktop.
Save akatche/144cec08ca3093efdd0915d74f43f2f0 to your computer and use it in GitHub Desktop.
Laravel code I love. This was for a project on my current work. The requirement was to have an API call to fetch documents, but we have 3 different types of documents and 2 different types of ways that those documents can come. So, I´ve created this small widget to make it happen, that It worked great!
<?php
namespace App\Http\Controllers\API\Apps\Protocolos;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
class ObtenerProtocolos extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
$className = 'App\Http\Controllers\API\Apps\Protocolos\\' . ucfirst($request->get('organismo')) . ucfirst($request->get('tipo_descarga'));
$class = new $className();
return response()->json($class->obtenerProtocolo($request));
}
}
<?php
namespace Tests\Feature\Apps\Escritos;
use App\Modelos\Apps\Liquidar\Liquidacion;
use App\Organismo;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ObtenerProtocolosTest extends TestCase
{
use RefreshDatabase;
use WithFaker;
/**
* @test
*/
public function obtengo_protocolos_completos_de_una_sala_correspondientes_a_un_año_en_particular()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'completo',
'organismo' => 'sala',
'organismo_numero' => 1,
'organismo_todos' => false,
'anio' => 2018,
'mes' => '',
]);
$response->assertSuccessful();
$response->assertJson([
'url' => '/completos/salas/2018/2018-SalaI.exe'
]);
}
/**
* @test
*/
public function obtengo_protocolos_completos_de_un_sala_8_a_un_año_en_particular()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'completo',
'organismo' => 'sala',
'organismo_numero' => 8,
'organismo_todos' => false,
'anio' => 2018,
'mes' => '',
]);
$response->assertSuccessful();
$response->assertJson([
'url' => '/completos/salas/2018/2018-SalaVIII.exe'
]);
}
/**
* @test
*/
public function obtengo_protocolos_completos_de_todas_las_salas_en_un_año_en_particular()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'completo',
'organismo' => 'sala',
'organismo_numero' => 10,
'organismo_todos' => true,
'anio' => 2018,
'mes' => '',
]);
$response->assertSuccessful();
$response->assertJson([
'url' => '/completos/salas/2018/Salas-2018.exe'
]);
}
/**
* @test
*/
public function si_no_existe_protocolo_completo_de_sala_debe_existir_algun_error()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'completo',
'organismo' => 'sala',
'organismo_numero' => 8,
'organismo_todos' => false,
'anio' => 2016,
'mes' => '',
]);
$response->assertStatus(500);
$response->assertJsonFragment([
'message' => 'El archivo solicitado no se encuentra disponible'
]);
}
/**
* @test
*/
public function obtengo_protocolos_completos_de_un_juzgado_menor_a_10()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'completo',
'organismo' => 'juzgado',
'organismo_numero' => 9,
'organismo_todos' => false,
'anio' => 2018,
'mes' => '',
]);
$response->assertSuccessful();
$response->assertJson([
'url' => '/completos/juzgados/2018/2018-J09.exe'
]);
}
/**
* @test
*/
public function obtengo_protocolos_completos_de_un_juzgado_mayor_igual_a_10()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'completo',
'organismo' => 'juzgado',
'organismo_numero' => 10,
'organismo_todos' => false,
'anio' => 2018,
'mes' => '',
]);
$response->assertSuccessful();
$response->assertJson([
'url' => '/completos/juzgados/2018/2018-J10.exe'
]);
}
/**
* @test
*/
public function si_no_existe_protocolo_completo_de_juzgado_debe_existir_algun_error()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'completo',
'organismo' => 'juzgado',
'organismo_numero' => 10,
'organismo_todos' => false,
'anio' => 2016,
'mes' => '',
]);
$response->assertStatus(500);
$response->assertJsonFragment([
'message' => 'El archivo solicitado no se encuentra disponible'
]);
}
/**
* @test
*/
public function obtengo_protocolos_completos_de_todos_los_juzgados_de_un_año()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'completo',
'organismo' => 'juzgado',
'organismo_numero' => 10,
'organismo_todos' => true,
'anio' => 2018,
'mes' => '',
]);
$response->assertSuccessful();
$response->assertJson([
'url' => '/completos/juzgados/2018/Juzgados-2018.exe'
]);
}
/**
* @test
*/
public function obtengo_protocolos_completos_de_un_año_de_las_fiscalias()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'completo',
'organismo' => 'fiscalia',
'organismo_numero' => '',
'organismo_todos' => true,
'anio' => 2018,
'mes' => '',
]);
$response->assertSuccessful();
$response->assertJson([
'url' => '/completos/fiscalia/FiscaliaGeneral2018.exe'
]);
}
/**
* @test
*/
public function si_no_existe_protocolo_completo_de_fiscalia_debe_existir_algun_error()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'completo',
'organismo' => 'fiscalia',
'organismo_numero' => '',
'organismo_todos' => true,
'anio' => 2008,
'mes' => '',
]);
$response->assertStatus(500);
$response->assertJsonFragment([
'message' => 'El archivo solicitado no se encuentra disponible'
]);
}
/**
* @test
*/
public function obtengo_protocolos_por_sentencia_de_una_sala_en_un_mes_anio_particular()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'sentencia',
'organismo' => 'sala',
'organismo_numero' => 10,
'organismo_todos' => false,
'anio' => 2017,
'mes' => '10',
]);
$response->assertSuccessful();
$response->assertJsonCount(2, 'protocolos');
$response->assertJsonStructure([
'protocolos' => [
'*' => [
'url',
'file',
]
]
]);
}
/**
* @test
*/
public function obtengo_protocolos_por_sentencia_de_un_juzgado_menor_a_10_en_un_mes_anio_particular()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'sentencia',
'organismo' => 'juzgado',
'organismo_numero' => 1,
'organismo_todos' => false,
'anio' => 2018,
'mes' => 2,
]);
$response->assertSuccessful();
$response->assertJsonCount(9, 'protocolos');
$response->assertJsonStructure([
'protocolos' => [
'*' => [
'url',
'file',
]
]
]);
}
/**
* @test
*/
public function obtengo_protocolos_por_sentencia_de_un_juzgado_mayor_igual_a_10_en_un_mes_anio_particular()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'sentencia',
'organismo' => 'juzgado',
'organismo_numero' => 12,
'organismo_todos' => false,
'anio' => 2018,
'mes' => 4,
]);
$response->assertSuccessful();
$response->assertJsonCount(6, 'protocolos');
$response->assertJsonStructure([
'protocolos' => [
'*' => [
'url',
'file',
]
]
]);
}
/**
* @test
*/
public function obtengo_protocolos_por_sentencia_de_fiscalia_en_un_mes_anio_particular()
{
$response = $this->json('POST','api/v1/protocolos', [
'tipo_descarga' => 'sentencia',
'organismo' => 'fiscalia',
'organismo_numero' => 12,
'organismo_todos' => false,
'anio' => 2018,
'mes' => 2,
]);
$response->assertSuccessful();
$response->assertJsonCount(5, 'protocolos');
$response->assertJsonStructure([
'protocolos' => [
'*' => [
'url',
'file',
]
]
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment