add tests for files and refactor
This commit is contained in:
parent
b0cb0db558
commit
010282a7bb
6 changed files with 112 additions and 49 deletions
26
app/File.php
26
app/File.php
|
@ -3,6 +3,7 @@
|
||||||
namespace App;
|
namespace App;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use TheSeer\Tokenizer\Exception;
|
||||||
|
|
||||||
class File extends Model
|
class File extends Model
|
||||||
{
|
{
|
||||||
|
@ -17,6 +18,8 @@ class File extends Model
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $primaryKey = 'hash';
|
protected $primaryKey = 'hash';
|
||||||
|
public $incrementing = false;
|
||||||
|
protected $keyType = 'string';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes excluded from the model's JSON form.
|
* The attributes excluded from the model's JSON form.
|
||||||
|
@ -24,4 +27,27 @@ class File extends Model
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $hidden = ['created_at','updated_at'];
|
protected $hidden = ['created_at','updated_at'];
|
||||||
|
|
||||||
|
public static function create(array $attributes = [])
|
||||||
|
{
|
||||||
|
if (!isset($attributes['data'])) {
|
||||||
|
throw new Exception("foo" );
|
||||||
|
}
|
||||||
|
$pos = strpos($attributes['data'], ",");
|
||||||
|
$image = base64_decode(substr($attributes['data'], $pos + 1), true);
|
||||||
|
if (!$image) {
|
||||||
|
throw new Exception("foo" );
|
||||||
|
}
|
||||||
|
$hash = md5(time());
|
||||||
|
if (!file_exists('staticimages'))
|
||||||
|
mkdir('staticimages', 0755, true);
|
||||||
|
file_put_contents('staticimages/' . $hash, $image);
|
||||||
|
|
||||||
|
$attributes['hash'] = $hash;
|
||||||
|
return static::query()->create($attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,17 +20,8 @@ class FileController extends Controller
|
||||||
|
|
||||||
public function create(Request $request)
|
public function create(Request $request)
|
||||||
{
|
{
|
||||||
$File = File::create($request->all());
|
$file = File::create($request->only(['data','iid']));
|
||||||
|
return response()->json($file, 201);
|
||||||
return response()->json($File, 201);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function update($id, Request $request)
|
|
||||||
{
|
|
||||||
$File = File::findOrFail($id);
|
|
||||||
$File->update($request->all());
|
|
||||||
|
|
||||||
return response()->json($File, 200);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete($id)
|
public function delete($id)
|
||||||
|
|
|
@ -49,18 +49,10 @@ class ItemController extends Controller
|
||||||
$item = Item::create($newitem);
|
$item = Item::create($newitem);
|
||||||
|
|
||||||
if($request->get('dataImage')) {
|
if($request->get('dataImage')) {
|
||||||
$pos = strpos($request->get('dataImage'), ",");
|
$file = File::create(array('data' => $request->get('dataImage'), 'iid' => $item['iid']));
|
||||||
//$head = substr($request->get('dataImage'), 0, $pos);
|
$item['file'] = $file['hash'];
|
||||||
$image = base64_decode(substr($request->get('dataImage'), $pos + 1), true);
|
|
||||||
if ($image) {
|
|
||||||
$hash = md5(time());
|
|
||||||
if (!file_exists('staticimages'))
|
|
||||||
mkdir('staticimages', 0755, true);
|
|
||||||
file_put_contents('staticimages/' . $hash, $image);
|
|
||||||
$file = File::create(array('hash' => $hash, 'iid' => $item['iid']));
|
|
||||||
$item['file'] = $hash;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json($item, 201);
|
return response()->json($item, 201);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,18 +67,10 @@ class ItemController extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
if($request->get('dataImage')) {
|
if($request->get('dataImage')) {
|
||||||
$pos = strpos($request->get('dataImage'), ",");
|
$file = File::create(array('data' => $request->get('dataImage'), 'iid' => $item['iid']));
|
||||||
//$head = substr($request->get('dataImage'), 0, $pos);
|
$item['file'] = $file['hash'];
|
||||||
$image = base64_decode(substr($request->get('dataImage'), $pos + 1), true);
|
|
||||||
if ($image) {
|
|
||||||
$hash = md5(time());
|
|
||||||
if (!file_exists('staticimages'))
|
|
||||||
mkdir('staticimages', 0755, true);
|
|
||||||
file_put_contents('staticimages/' . $hash, $image);
|
|
||||||
$file = File::create(array('hash' => $hash, 'iid' => $item['iid']));
|
|
||||||
$item['file'] = $hash;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json(Item::find($item['uid']), 200);
|
return response()->json(Item::find($item['uid']), 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,6 @@ $router->group(['prefix' => '1'], function () use ($router) {
|
||||||
$router->get('file/{id}', ['uses' => 'FileController@showOneFile']);
|
$router->get('file/{id}', ['uses' => 'FileController@showOneFile']);
|
||||||
$router->post('file', ['uses' => 'FileController@create']);
|
$router->post('file', ['uses' => 'FileController@create']);
|
||||||
$router->delete('file/{id}', ['uses' => 'FileController@delete']);
|
$router->delete('file/{id}', ['uses' => 'FileController@delete']);
|
||||||
$router->put('file/{id}', ['uses' => 'FileController@update']);
|
|
||||||
|
|
||||||
// items
|
// items
|
||||||
$router->get('{event}/items', ['uses' => 'ItemController@showByEvent']);
|
$router->get('{event}/items', ['uses' => 'ItemController@showByEvent']);
|
||||||
|
|
63
tests/FileTest.php
Normal file
63
tests/FileTest.php
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Container;
|
||||||
|
use App\Event;
|
||||||
|
use App\Item;
|
||||||
|
use App\File;
|
||||||
|
|
||||||
|
class FileTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A basic test example.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function testListFiles(){
|
||||||
|
$item = File::create(['iid'=>1,'data'=>",".base64_encode("foo")]);
|
||||||
|
|
||||||
|
$this->get( '/1/files');
|
||||||
|
$this->assertResponseOk();
|
||||||
|
$this->assertEquals(1, json_decode($this->response->getContent(),true)[0]['iid']);
|
||||||
|
$this->assertEquals($item->hash, json_decode($this->response->getContent(),true)[0]['hash']);
|
||||||
|
$this->assertEquals(32, strlen(json_decode($this->response->getContent(),true)[0]['hash']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOneFile(){
|
||||||
|
$item = File::create(['iid'=>1,'data'=>",".base64_encode("foo")]);
|
||||||
|
|
||||||
|
$this->get( '/1/file/'.$item->hash);
|
||||||
|
$this->assertResponseOk();
|
||||||
|
$this->assertEquals(1, json_decode($this->response->getContent(),true)['iid']);
|
||||||
|
$this->assertEquals($item->hash, json_decode($this->response->getContent(),true)['hash']);
|
||||||
|
$this->assertEquals(32, strlen(json_decode($this->response->getContent(),true)['hash']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateFile(){
|
||||||
|
$event = Event::create(['slug'=>'EVENT', 'name'=>'Event']);
|
||||||
|
$box = Container::create(['name'=>'BOX']);
|
||||||
|
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'1']);
|
||||||
|
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'2']);
|
||||||
|
|
||||||
|
$this->post( '/1/file',['data'=>",".base64_encode("foo"), 'iid'=>$item->iid]);
|
||||||
|
$this->assertResponseStatus(201);
|
||||||
|
$this->assertEquals($item->iid, json_decode($this->response->getContent(),true)['iid']);
|
||||||
|
$this->assertEquals(32, strlen(json_decode($this->response->getContent(),true)['hash']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDeleteFile(){
|
||||||
|
$event = Event::create(['slug'=>'EVENT', 'name'=>'Event']);
|
||||||
|
$box = Container::create(['name'=>'BOX']);
|
||||||
|
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'1']);
|
||||||
|
File::create(['iid'=>$item->iid,'data'=>",".base64_encode("foo")]);
|
||||||
|
$file = File::create(['iid'=>$item->iid,'data'=>",".base64_encode("bar")]);
|
||||||
|
|
||||||
|
$this->assertEquals(2, count(File::all()));
|
||||||
|
|
||||||
|
$this->delete( '/1/file/'.$file->hash);
|
||||||
|
$this->assertResponseOk();
|
||||||
|
|
||||||
|
$this->assertEquals(1, count(Item::all()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -25,7 +25,7 @@ class ItemTest extends TestCase
|
||||||
{
|
{
|
||||||
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
||||||
$box = Container::create(['name'=>'BOX']);
|
$box = Container::create(['name'=>'BOX']);
|
||||||
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
|
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'1']);
|
||||||
|
|
||||||
$this->get('/1/EVENT/items');
|
$this->get('/1/EVENT/items');
|
||||||
$response = $this->response->getOriginalContent();
|
$response = $this->response->getOriginalContent();
|
||||||
|
@ -43,8 +43,8 @@ class ItemTest extends TestCase
|
||||||
{
|
{
|
||||||
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
||||||
$box = Container::create(['name'=>'BOX']);
|
$box = Container::create(['name'=>'BOX']);
|
||||||
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
|
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'1']);
|
||||||
File::create(['iid'=>$item->iid, 'hash'=>'filename']);
|
$file = File::create(['iid'=>$item->iid, 'data'=>",".base64_encode("foo")]);
|
||||||
|
|
||||||
$this->get('/1/EVENT/items');
|
$this->get('/1/EVENT/items');
|
||||||
$response = $this->response->getOriginalContent();
|
$response = $this->response->getOriginalContent();
|
||||||
|
@ -55,16 +55,16 @@ class ItemTest extends TestCase
|
||||||
$this->assertEquals('1', $response[0]['description']);
|
$this->assertEquals('1', $response[0]['description']);
|
||||||
$this->assertEquals($box->name, $response[0]['box']);
|
$this->assertEquals($box->name, $response[0]['box']);
|
||||||
$this->assertEquals($box->cid, $response[0]['cid']);
|
$this->assertEquals($box->cid, $response[0]['cid']);
|
||||||
$this->assertEquals('filename', $response[0]['file']);
|
$this->assertEquals($file->hash, $response[0]['file']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testmultiMembers()
|
public function testmultiMembers()
|
||||||
{
|
{
|
||||||
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
||||||
$box = Container::create(['name'=>'BOX']);
|
$box = Container::create(['name'=>'BOX']);
|
||||||
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
|
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'1']);
|
||||||
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'2']);
|
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'2']);
|
||||||
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'3']);
|
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'3']);
|
||||||
|
|
||||||
$this->get('/1/EVENT/items');
|
$this->get('/1/EVENT/items');
|
||||||
$response = $this->response->getOriginalContent();
|
$response = $this->response->getOriginalContent();
|
||||||
|
@ -110,7 +110,7 @@ class ItemTest extends TestCase
|
||||||
|
|
||||||
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
||||||
$box = Container::create(['name'=>'BOX']);
|
$box = Container::create(['name'=>'BOX']);
|
||||||
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
|
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'1']);
|
||||||
|
|
||||||
$this->assertEquals(1, $item['uid']);
|
$this->assertEquals(1, $item['uid']);
|
||||||
$this->assertEquals('1', $item['description']);
|
$this->assertEquals('1', $item['description']);
|
||||||
|
@ -138,8 +138,8 @@ class ItemTest extends TestCase
|
||||||
public function testDeleteItem(){
|
public function testDeleteItem(){
|
||||||
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
||||||
$box = Container::create(['name'=>'BOX']);
|
$box = Container::create(['name'=>'BOX']);
|
||||||
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
|
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'1']);
|
||||||
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'2']);
|
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'2']);
|
||||||
|
|
||||||
$this->assertEquals(2, count(Item::all()));
|
$this->assertEquals(2, count(Item::all()));
|
||||||
|
|
||||||
|
@ -153,8 +153,8 @@ class ItemTest extends TestCase
|
||||||
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
|
||||||
$box = Container::create(['name'=>'BOX']);
|
$box = Container::create(['name'=>'BOX']);
|
||||||
|
|
||||||
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
|
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'1']);
|
||||||
$item2 = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'2']);
|
$item2 = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'2']);
|
||||||
|
|
||||||
$this->assertEquals(2, count(Item::all()));
|
$this->assertEquals(2, count(Item::all()));
|
||||||
|
|
||||||
|
@ -163,7 +163,7 @@ class ItemTest extends TestCase
|
||||||
$this->assertResponseOk();
|
$this->assertResponseOk();
|
||||||
$this->assertEquals(1, count(Item::all()));
|
$this->assertEquals(1, count(Item::all()));
|
||||||
|
|
||||||
$item3 = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'3']);
|
$item3 = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'description'=>'3']);
|
||||||
|
|
||||||
$this->assertEquals(3, $item3['uid']);
|
$this->assertEquals(3, $item3['uid']);
|
||||||
$this->assertEquals(2, count(Item::all()));
|
$this->assertEquals(2, count(Item::all()));
|
||||||
|
|
Loading…
Reference in a new issue