class ProductController extends AbstractController
{
/**
* @Route("/", name="app_product_new")
*/
public function new(Request $request)
{
$productes = $this->getDoctrine()->getRepository(Product::class)->findAll();
$product = new Product();
$form = $this->createForm(ProductType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() ) {
/** @var UploadedFile $brochureFile */
$brochureFile = $form->get('brochure')->getData();
// this condition is needed because the 'brochure' field is not required
// so the PDF file must be processed only when a file is uploaded
if ($brochureFile) {
$originalFilename = pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$safeFilename = $originalFilename;
$newFilename = $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
// Move the file to the directory where brochures are stored
try {
$brochureFile->move(
$this->getParameter('brochures_directory'),
$newFilename
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
// updates the 'brochureFilename' property to store the PDF file name
// instead of its contents
$product->setBrochureFilename($newFilename);
$em = $this->getDoctrine()->getManager(); // getManager отвечает за взаимодейтсвие с БД
$em->persist($product); // добавляет объект в БД
$em->flush(); // выполняет все ранее поставленные инстукции
}
// ... persist the $product variable or any other work
return $this->redirect($this->generateUrl('csv', ['id' => $product->getId()]));
}
return $this->render('product/new.html.twig', [
'form' => $form->createView(),
'productes' => $productes
]);
}
}
class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('brochure', FileType::class, [
'label' => 'Invoice CSV: ',
// unmapped means that this field is not associated to any entity property
'mapped' => false,
// make it optional so you don't have to re-upload the CSV file
// every time you edit the Product details
'required' => false,
// unmapped fields can't define their validation using annotations
// in the associated entity, so you can use the PHP constraint classes
'constraints' => [
new File([
'maxSize' => '1024k',
'mimeTypes' => [
'text/plain',
'text/csv',
'text/csv-schema',
'application/csvm+json',
],
'mimeTypesMessage' => 'Please upload a valid CSV document',
])
],
])
// ...
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Product::class,
]);
}
}
class ResponseStatus extends WebTestCase
{
public function testShowPost()
{
$client = static::createClient();
$csv = new UploadedFile(
'/var/www/testwork.loc/public/uploads/brochures/file_valid-5ec3f39f6903e.txt',
'file_valid-5ec3f39f6903e.txt',
'text/plain',
123
);
$client->request(
'POST',
'/',
// array('name' => 'Fabien'),
array('brochure' => $csv)
);
}
}
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Risky: 1.