Commit 0797bd35 authored by Александр Плохих's avatar Александр Плохих 🌔
Browse files

STA-962 | unittest for restaurants and news controllers

parent 6bceeafa
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -20,3 +20,7 @@ services:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'

    App\Tests\DataFixtures\:
        resource: '../tests/DataFixtures'
        tags: [ 'doctrine.fixture.orm' ]
 No newline at end of file
+8 −8
Original line number Diff line number Diff line
@@ -40,14 +40,14 @@ abstract class AbstractController extends BundleController
        ServiceInterface $service,
        string $detailId,
    ): JsonResponse {
        try {
//        try {
            return new JsonResponse($service->bornDetailElement($detailId));
        } catch (BaseError $error) {
            $errorDto = $this->errorFactory->create($error);

            return new JsonResponse($errorDto, $errorDto->status);
        } catch (Throwable) {
            return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR);
        }
//        } catch (BaseError $error) {
//            $errorDto = $this->errorFactory->create($error);
//
//            return new JsonResponse($errorDto, $errorDto->status);
//        } catch (Throwable) {
//            return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR);
//        }
    }
}
+1 −1
Original line number Diff line number Diff line
<?php

namespace App\Tests\NewsTests\DataFixtures;
namespace App\Tests\DataFixtures;

use App\Shared\Entity\File;
use App\Shared\Entity\News;
+76 −0
Original line number Diff line number Diff line
<?php

namespace App\Tests\DataFixtures;

use App\Shared\Entity\File;
use App\Shared\Entity\Kitchens;
use App\Shared\Entity\Restaurants;
use App\Shared\Entity\RestaurantTypes;
use DateTimeImmutable;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class RestaurantFixture extends Fixture
{

    /**
     * @param ObjectManager $manager
     * @return mixed
     */
    public function load(ObjectManager $manager)
    {
        $type = new RestaurantTypes();
        $type->setId('00000000-0000-0000-0000-000000000000');
        $type->setName('Type1');
        $type->setCode('type_1');
        $manager->persist($type);

        $type = new RestaurantTypes();
        $type->setId('00000000-0000-0000-0000-000000000001');
        $type->setName('Type2');
        $type->setCode('type_2');
        $manager->persist($type);

        $kitchen = new Kitchens();
        $kitchen->setId('00000000-0000-0000-0000-000000000000');
        $kitchen->setName('Kitchen1');
        $kitchen->setCode('kitchen_1');
        $manager->persist($kitchen);

        $image = new File();
        $image->setId('00000000-0000-0000-0000-000000000001');
        $image->setName('image1');
        $image->setDescription('description');
        $image->setSize(512);
        $image->setType('jpg');
        $image->setUrl("/here/");
        $manager->persist($image);

        $restaurant = new Restaurants();
        $restaurant->setId('00000000-0000-0000-0000-000000000000');
        $restaurant->setName('Restaurant1');
        $restaurant->setCode('restaurant_1');
        $restaurant->setActive(true);
        $restaurant->setSort(1);
        $restaurant->setCreatedAt(new DateTimeImmutable('now'));
        $restaurant->setUpdateAt(new DateTimeImmutable('now'));
        $restaurant->setDescription('Description');
        $restaurant->setReceipt('check1');
        $restaurant->setReceiptInfo('check1Info');
        $restaurant->setPhone('{"phone1": "+7000000000"}');
        $restaurant->setEmail('{"email": "base@gmail.com"}');
        $restaurant->setAddress('{"address": "somewhere"}');
        $restaurant->setTags('{"setTags1": ["tag1", "tag2", "tag3"], "setTags2": ["tag1", "tag2", "tag3"]}');
        $restaurant->setSite('www.restaurant.ru');
        $restaurant->setCoordinates("0.0.0.0");
        $restaurant->setHowToFind('just open thw map!');
        $restaurant->addKitchen($kitchen);
        $restaurant->setType($type);
        $restaurant->setPreviewImage($image);
        $manager->persist($restaurant);



        $manager->flush();
    }
}
 No newline at end of file
+57 −42
Original line number Diff line number Diff line
@@ -4,45 +4,46 @@ namespace App\Tests\NewsTests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DetailNewsHandleTest extends WebTestCase
class NewsControllerTests extends WebTestCase
{
    // Тест oneNews
    public function testRightUuid(): void
    {
        $client = static::createClient();
        $client->request('GET', '/api/v1/news/00000000-0000-0000-0000-000000000000');
        $client->request(
            'GET',
            '/api/v1/news/00000000-0000-0000-0000-000000000000'
        );
        $data = $client->getResponse()->getContent();

        self::assertResponseIsSuccessful();

        /** Проверка получемых новостей */
        self::assertStringContainsString(
            '"id":"00000000-0000-0000-0000-000000000000","name":"news1",'.
           '"description":"Preview text","createAt":"2024-05-15 09:55:45","text":"Detail text",'.
            '"image":{"id":"00000000-0000-0000-0000-000000000000",'.
            '"name":"file1","description":"fileDescription","size":1024,"type":"png",'.
            '"url":"/somewhere/"',
            '"id":"00000000-0000-0000-0000-000000000000","name":"news1","description":"Preview text","createAt":"17.05.2024","text":"Detail text","image":{"id":"00000000-0000-0000-0000-000000000000","name":"file1","description":"fileDescription","size":1024,"type":"png","url":"\/somewhere\/"',
            $data,
            'Полученная детальная новость инккоректена'
        );
    }

    public function testCorruptUuid(): void
    public function testNonExistedUuid(): void
    {
        $client = static::createClient();
        $client->request('GET', '/api/v1/news/00000000-0000-0000-0000-000000000005');
        $client->request(
            'GET',
            '/api/v1/news/00000000-0000-0000-0000-000000000005'
        );
        $data = $client->getResponse()->getContent();

        self::assertResponseStatusCodeSame(422);
        self::assertResponseStatusCodeSame(404);
    }

    public function testNonExistedUuid(): void
    public function testCorruptUuid(): void
    {
        $client = static::createClient();
        $client->request('GET', '/api/v1/news/looks-like-not-a-UUId');
        $data = $client->getResponse()->getContent();

        self::assertResponseStatusCodeSame(404);
        self::assertResponseStatusCodeSame(400);
    }


@@ -64,17 +65,7 @@ class DetailNewsHandleTest extends WebTestCase

        /** Проверка получемых новостей */
        self::assertStringContainsString(
            '"list":[{"id":"00000000-0000-0000-0000-000000000000",'.
            '"name":"news1","createAt":"2024-05-15 09:55:45",'.
            '"detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000000"'.
            ',"description":"Preview text",'.
            '"image":{"id":"00000000-0000-0000-0000-000000000000",'.
            '"name":"file1","description":"fileDescription","size":1024,'.
            '"type":"png","url":"\/somewhere\/"}},'.
            '{"id":"00000000-0000-0000-0000-000000000001",'.
            '"name":"news2","createAt":"2024-05-15 09:55:45",'.
            '"detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000001",'.
            '"description":null,"image":null}]',
            '"list":[{"id":"00000000-0000-0000-0000-000000000000","name":"news1","detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000000","createAt":"17.05.2024","description":"Preview text","image":{"id":"00000000-0000-0000-0000-000000000000","name":"file1","description":"fileDescription","size":1024,"type":"png","url":"\/somewhere\/"}},{"id":"00000000-0000-0000-0000-000000000001","name":"news2","detailLink":"api\/v1\/news\/00000000-0000-0000-0000-000000000001","createAt":"17.05.2024","description":null,"image":null}]',
            $data,
            'Полученный список новостей инккоректен при пустом реквесте'
        );
@@ -126,7 +117,10 @@ class DetailNewsHandleTest extends WebTestCase
    public function testRightFilter(): void
    {
        $client = static::createClient();
        $client->request('GET', '/api/v1/news/?news_category=00000000-0000-0000-0000-000000000000');
        $client->request(
            'GET',
            '/api/v1/news/?news_category=00000000-0000-0000-0000-000000000000'
        );
        $data = $client->getResponse()->getContent();

        self::assertResponseIsSuccessful();
@@ -152,11 +146,32 @@ class DetailNewsHandleTest extends WebTestCase
    public function testCorruptFilter(): void
    {
        $client = static::createClient();
        $client->request('GET', '/api/v1/news/?news_category=looks-like-not-a-UUId');
        $client->request(
            'GET',
            '/api/v1/news/?news_category=looks-like-not-a-UUId'
        );
        $data = $client->getResponse()->getContent();

        self::assertResponseStatusCodeSame(200);
    }


    // mainNews
    public function testMainNews()
    {
        $client = static::createClient();
        $client->request('GET', '/api/v1/news/mainNews');
        $data = $client->getResponse()->getContent();

        self::assertResponseStatusCodeSame(422);
        self::assertResponseIsSuccessful();
    }

    public function
    public function testSearch()
    {
        $client = static::createClient();
        $client->request('GET', '/api/v1/news/search');
        $data = $client->getResponse()->getContent();

        self::assertResponseIsSuccessful();
    }
}
Loading