<commit_before><?php

namespace Openl10n\Bundle\ApiBundle\Tests\Controller;

use Openl10n\Bundle\ApiBundle\Test\WebTestCase;
use Openl10n\Value\String\Slug;
use Symfony\Component\HttpFoundation\Response;

class ProjectControllerTest extends WebTestCase
{
    public function setUp()
    {
        parent::setUp();

        $client = $this->getClient();
        $client->setCredentials('user', 'user');
    }

    /**
     * Test projects list response.
     *
     * Example of excepted response:
     *
     *     [
     *       {
     *         "slug": "demo",
     *         "name": "Demo",
     *         "default_locale": "en"
     *       },
     *       {
     *         "slug": "foobar",
     *         "name": "Foobar",
     *         "default_locale": "fr_FR"
     *       }
     *     ]
     */
    public function testGetProjectsList()
    {
        $client = $this->getClient();
        $client->jsonRequest('GET', '/api/projects');

        $data = $this->assertJsonResponse(
            $client->getResponse(),
            Response::HTTP_OK
        );

        $this->assertCount(3, $data, 'There should be 3 projects');
    }

    /**
     * Test demo project response.
     *
     * Example of excepted response:
     *
     *     {
     *       "slug": "demo",
     *       "name": "Demo",
     *       "default_locale": "en"
     *     }
     */
    public function testGetProject()
    {
        $client = $this->getClient();
        $client->jsonRequest('GET', '/api/projects/demo');

        $data = $this->assertJsonResponse(
            $client->getResponse(),
            Response::HTTP_OK,
            'project'
        );

        $this->assertEquals('demo', $data->slug, 'Project\'s slug should be "demo"');
        $this->assertEquals('Demo', $data->name, 'Project\'s name should be "Demo"');
        $this->assertEquals('en', $data->default_locale, 'Project\'s default locale should be "en"');
    }

    /**
     * Test to get a non existing project.
     */
    public function testGetProjectNotFound()
    {
        $client = $this->getClient();
        $client->jsonRequest('GET', '/api/projects/not-found');

        $data = $this->assertJsonResponse(
            $client->getResponse(),
            Response::HTTP_NOT_FOUND
        );
    }

    /**
     * Test to create a new project.
     */
    public function testCreateNewProject()
    {
        $this->markTestIncomplete();
    }

    /**
     * Test to create an existing project (identified by slug).
     */
    public function testCreateExistingProject()
    {
        $this->markTestIncomplete();
    }

    /**
     * Test to update an existing project.
     */
    public function testUpdateProject()
    {
        $this->markTestIncomplete();
    }

    /**
     * Test to delete an existing project.
     */
    public function testDeleteProject()<commit_msg>Complete project tests<commit_after><?php

namespace Openl10n\Bundle\ApiBundle\Tests\Controller;

use Openl10n\Bundle\ApiBundle\Test\WebTestCase;
use Openl10n\Value\String\Slug;
use Symfony\Component\HttpFoundation\Response;

class ProjectControllerTest extends WebTestCase
{
    public function setUp()
    {
        parent::setUp();

        $client = $this->getClient();
        $client->setCredentials('user', 'user');
    }

    /**
     * Test projects list response.
     *
     * Example of excepted response:
     *
     *     [
     *       {
     *         "slug": "demo",
     *         "name": "Demo",
     *         "default_locale": "en"
     *       },
     *       {
     *         "slug": "foobar",
     *         "name": "Foobar",
     *         "default_locale": "fr_FR"
     *       }
     *     ]
     */
    public function testGetProjectsList()
    {
        $client = $this->getClient();
        $client->jsonRequest('GET', '/api/projects');

        $data = $this->assertJsonResponse(
            $client->getResponse(),
            Response::HTTP_OK
        );

        $this->assertCount(3, $data, 'There should be 3 projects');
    }

    /**
     * Test demo project response.
     *
     * Example of excepted response:
     *
     *     {
     *       "slug": "demo",
     *       "name": "Demo",
     *       "default_locale": "en"
     *     }
     */
    public function testGetProject()
    {
        $client = $this->getClient();
        $client->jsonRequest('GET', '/api/projects/demo');

        $data = $this->assertJsonResponse(
            $client->getResponse(),
            Response::HTTP_OK,
            'project'
        );

        $this->assertEquals('demo', $data->slug, 'Project\'s slug should be "demo"');
        $this->assertEquals('Demo', $data->name, 'Project\'s name should be "Demo"');
        $this->assertEquals('en', $data->default_locale, 'Project\'s default locale should be "en"');
    }

    /**
     * Test to get a non existing project.
     */
    public function testGetProjectNotFound()
    {
        $client = $this->getClient();
        $client->jsonRequest('GET', '/api/projects/not-found');

        $data = $this->assertJsonResponse(
            $client->getResponse(),
            Response::HTTP_NOT_FOUND
        );
    }

    /**
     * Test to create a new project.
     */
    public function testCreateNewProject()
    {
        $client = $this->getClient();
        $client->jsonRequest('POST', '/api/projects', [
            'name' => 'New Project',
            'slug' => 'new-project',
            'default_locale' => 'en',
        ]);

        $this->assertJsonResponse(
            $client->getResponse(),
            Response::HTTP_CREATED
        );

        // Ensure project has been created
        $project = $this->get('openl10n.repository.project')->findOneBySlug(new Slug('new-project'));
        $this->assertNotNull($project, 'Project "new-project" should exist');
        $this->assertEquals((string) $project->getName(), 'New Project', 'Project\'s name should be "New Project"');
        $this->assertEquals((string) $project->getDefaultLocale(), 'en', 'Project\'s locale should be "en"');
    }

    /**
     * Test to create an existing project (identified by slug).
     */
    public function testCreateExistingProject()
    {
        $client = $this->getClient();
        $client->jsonRequest('POST', '/api/projects', [
            'name' => '<NAME>',
            'slug' => 'demo',
            'default_locale' => 'en',
        ]);

        $this->assertJsonResponse(
            $client->getResponse(),
            Response::HTTP_BAD_REQUEST
        );
    }

    /**
     * Test to update an existing project.
     */
    public function testUpdateProject()
    {
        $client = $this->getClient();
        $client->jsonRequest('PUT', '/api/projects/demo', [
            'name' => 'Foobar',
            'default_locale' => 'fr',
        ]);

        $this->assertJsonResponse(
            $client->getResponse(),
            Response::HTTP_NO_CONTENT
        );

        // Ensure project has been created
        $project = $this->get('openl10n.repository.project')->findOneBySlug(new Slug('demo'));
        $this->assertNotNull($project, 'Project "demo" should exist');
        $this->assertEquals((string) $project->getName(), 'Foobar', 'Project\'s name should be "Foobar"');
        $this->assertEquals((string) $project->getDefaultLocale(), 'fr', 'Project\'s locale should be "fr"');
    }

    /**
     * Test to delete an existing project.
     */
    public function testDeleteProject()
{'fraction_non_alphanumeric': 0.1543615168162937, 'fraction_numerical': 0.0026798874447273215, 'mean_word_length': 1.9737051792828686, 'pattern_counts': {'":': 18, '<': 6, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7251519', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2125, 'n_words': 430}