The class 'Symfony\\Component\\HttpFoundation\\File\\UploadedFile' was not found in the chain configured namespaces during Rest API Unit Test -
i'm writing rest api client , trying unit test user creation process let user upload image.
i using symfony 2.7, doctrine extension bundle (uploadable extension) , fos rest bundle
the unit tests working excepted when try upload file, triggers me following error when error_log 500 http reponse :
the class 'symfony\\component\\httpfoundation\\file\\uploadedfile' not found in chain configured namespaces please find relevant code :
userscontrollertest.php
<?php namespace acme\bundle\userbundle\tests\controller; use symfony\component\httpfoundation\file\uploadedfile; use symfony\component\httpfoundation\file\mimetype\mimetypeguesser; use symfony\bundle\frameworkbundle\test\webtestcase; class userscontrollertest extends webtestcase { public function testavatar(){ $client = static::createclient(); $shortimage = tempnam(sys_get_temp_dir(), 'upl'); imagepng(imagecreatetruecolor(10, 10), $shortimage); $file = new uploadedfile( $shortimage, basename($shortimage), mimetypeguesser::getinstance()->guess($shortimage), filesize($shortimage) ); $crawler = $client->request( "post", "/api/users", array( "user_registration" => array( "firstname" => "test", "lastname" => "test" ), ), array( 'user_registration'=>array('avatar'=>$file) ), array( 'content-type' => 'multipart/formdata' ) ); error_log($client->getresponse()); //here see uploadable class namespace error $this->assertequals(200, $client->getresponse()->getstatuscode()); } } userregistrationtype.php
<?php namespace acme\bundle\userbundle\form\type; use acme\bundle\userbundle\entity\user; use symfony\component\form\abstracttype; use symfony\component\optionsresolver\optionsresolverinterface; use symfony\component\form\formbuilderinterface; class userregistrationtype extends abstracttype{ public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => 'acme\bundle\userbundle\entity\user', 'cascade_validation' => true, 'csrf_protection' => false )); } public function buildform(formbuilderinterface $builder, array $options) { $builder->add('firstname', 'text'); $builder->add('lastname', 'text'); $builder->add('avatar', 'file', array( 'required' => false )); } public function getparent() { return 'form'; } public function getname() { return 'user_registration'; } } userscontroller.php
<?php namespace acme\bundle\userbundle\controller; use fos\restbundle\controller\fosrestcontroller; use fos\restbundle\controller\annotations\view; use acme\bundle\userbundle\entity\user; use acme\bundle\userbundle\entity\avatar; use acme\bundle\userbundle\form\type\userregistrationtype; use symfony\component\httpfoundation\request; use fos\restbundle\controller\annotations\routeresource; use jms\securityextrabundle\annotation jmssecurity; class userscontroller extends fosrestcontroller { /** * @view(serializergroups={"registration"}) */ public function postusersaction(request $request){ $user = new user(); $form = $this->createform(new userregistrationtype(), $user); $form->submit($request); if ($form->isvalid()) { $em = $this->getdoctrine()->getmanager(); $em->persist($user); if(null !== $user->getavatar()){ $uploadablemanager = $this->get('stof_doctrine_extensions.uploadable.manager'); $uploadablemanager->markentitytoupload($user, $user->getavatar()); } $em->flush(); return $user; } else { $validator = $this->get('validator'); $errors = $validator->validate($user, array('default','registration')); $view = $this->view($errors, 400); return $this->handleview($view); } } } avatar.php
<?php namespace acme\bundle\userbundle\entity; use doctrine\orm\mapping orm; use gedmo\mapping\annotation gedmo; /** * avatar * * @orm\table("avatar") * @orm\entity * @gedmo\uploadable(pathmethod="getpath", callback="postuploadaction", filenamegenerator="sha1", allowoverwrite=true, appendnumber=true) */ class avatar { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\column(name="path", type="string") * @gedmo\uploadablefilepath */ private $path; /** * @orm\column(name="name", type="string") * @gedmo\uploadablefilename */ private $name; /** * @orm\column(name="mime_type", type="string") * @gedmo\uploadablefilemimetype */ private $mimetype; /** * @orm\column(name="size", type="decimal") * @gedmo\uploadablefilesize */ private $size; public function postuploadaction(array $info) { // stuff file.. } /** * id * * @return integer */ public function getid() { return $this->id; } public function getpath(){ return __dir__.'../../web/avatars/'; } /** * set path * * @param string $path * @return image */ public function setpath($path) { $this->path = $path; return $this; } /** * set mimetype * * @param string $mimetype * @return image */ public function setmimetype($mimetype) { $this->mimetype = $mimetype; return $this; } /** * mimetype * * @return string */ public function getmimetype() { return $this->mimetype; } /** * set size * * @param string $size * @return image */ public function setsize($size) { $this->size = $size; return $this; } /** * size * * @return string */ public function getsize() { return $this->size; } } user.php
class user{ ... /** * avatar * * @orm\onetoone(targetentity="acme\bundle\userbundle\entity\avatar", cascade={"all"}) * @orm\joincolumn(nullable=true) * @assert\valid */ private $avatar; public function getavatar(){ return $this->avatar; } public function setavatar($avatar = null){ $this->avatar = $avatar; return $this; } } error stack (json) full stack please see here : http://pastebin.com/sgpe4uh1
{ "error":{ "code":500, "message":"internal server error", "exception":[ { "message":"the class 'symfony\\component\\httpfoundation\\file\\uploadedfile' not found in chain configured namespaces acme\\bundle\\userbundle\\entity", "class":"doctrine\\common\\persistence\\mapping\\mappingexception", "trace":[ { "namespace":"", "short_class":"", "class":"", "type":"", "function":"", "file":"d:\\xampp\\htdocs\\restapi\\vendor\\doctrine\\common\\lib\\doctrine\\common\\persistence\\mapping\\mappingexception.php", "line":37, "args":[ ] }] } } } i don't know origin of error, on config.yml doctrine mapping set auto
thank in advance help
edit 1
i have refactored code scenario :
the upload request independant, has own isolated request content-type set multipart/form-data
this time not have problem, but..the same error still here :
{ code: 500 message: "the class 'symfony\component\httpfoundation\file\uploadedfile' not found in chain configured namespaces sowq\bundle\userbundle\entity" } i wondering if that's not bug or compatibility issue between symfony 2.7 , doctrine uploadable extension, idea?
i think problem or (uploadablemanager extension - don't know this) try set object file (of type "uploadedfile class")
$avatar file object if want save database, need datatype blob. usually, before save file on file system , save $avatar on db, $avatar file path or file name o link file system resource
see https://github.com/atlantic18/doctrineextensions/issues/1353
Comments
Post a Comment