<commit_before><?php

namespace erdiko\users\tests\phpunit;

use erdiko\authenticate\services\JWTAuthenticator;
use erdiko\users\controllers\admin\UserAjax;
use erdiko\users\models\User;
use GuzzleHttp\Client;

require_once dirname(__DIR__) . '/ErdikoTestCase.php';

class UserAjaxTest extends \tests\ErdikoTestCase
{
    /**
     * @var Client
     */
    protected $client;

    protected $credentialsAsSuper = [
        'email' => '<EMAIL>',
        'password' => '<PASSWORD>'
    ];

    protected $credentialsAsUser = [
        'email' => '<EMAIL>',
        'password' => '<PASSWORD>'
    ];

    protected $requestOptions = [];

    protected $hosts = [
        'http://webserver',
        'http://erdiko.local',
        'http://erdiko_users_web',
        'http://localhost',
        'http://127.0.0.1',
        'http://0.0.0.0',
    ];

    public function testCurl()
    {
        foreach ($this->hosts as $host) {
            $this->callWithCurl($host);
        }
    }

    public function testGuzzle()
    {
        foreach ($this->hosts as $host) {
            $this->callWithGuzzle($host);
        }
    }

    protected function callWithCurl($host)
    {
        $data_string = json_encode($this->credentialsAsSuper);

        $ch = curl_init($host.'/ajax/users/authentication/login');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
        );

        $result = curl_exec($ch);

        var_dump($result);
    }

    protected function callWithGuzzle($host)
    {
        $this->client = new Client(['base_uri' => $host]);

        $response = $this->client->post('/ajax/users/authentication/login', ['json' => $this->credentialsAsSuper]);
        $jsonResponse = $this->getJsonResponse($response);

        var_dump($jsonResponse);
    }

//    public function setup()
//    {
//        $this->client = new Client(['base_uri' => 'http://erdiko.local/']);
//    }
//
//    protected function login($credentials)
//    {
//        $response = $this->client->post('/ajax/users/authentication/login', ['json' => $credentials]);
//        $phpsessid = $this->getPhpsessid($response->getHeader('Set-Cookie'));
//
//        $jsonResponse = $this->getJsonResponse($response);
//        $token = $jsonResponse->body->token;
//
//        $this->requestOptions = ['headers' => [
//            'Content-Type' => 'application/json',
//            'Authorization' => 'Bearer '.$token,
//            'Cookie' => $phpsessid
//        ]];
//    }
//
//    protected function loginAsUser()
//    {
//        $this->login($this->credentialsAsUser);
//    }
//
//    protected function loginAsSuper()
//    {
//        $this->login($this->credentialsAsSuper);
//    }
//
//    protected function getPhpsessid($setCookie)
//    {
//        $setCookieRaw = explode(';', $setCookie[0]);
//        return $setCookieRaw[0];
//    }
//
//    protected function getJsonResponse($response)
//    {
//        return json_decode($response->getBody());
//    }
//
//    protected function setJsonDataRequest($data)
//    {
//        $this->requestOptions['json'] = $data;
//    }
//
//    public function testPostCreateSuccess()
//    {
//        $this->loginAsSuper();
//        $response = $this->postCreate();<commit_msg>Added Test to Check on CircleCI - 2<commit_after><?php

namespace erdiko\users\tests\phpunit;

use erdiko\authenticate\services\JWTAuthenticator;
use erdiko\users\controllers\admin\UserAjax;
use erdiko\users\models\User;
use GuzzleHttp\Client;

require_once dirname(__DIR__) . '/ErdikoTestCase.php';

class UserAjaxTest extends \tests\ErdikoTestCase
{
    /**
     * @var Client
     */
    protected $client;

    protected $credentialsAsSuper = [
        'email' => '<EMAIL>',
        'password' => '<PASSWORD>'
    ];

    protected $credentialsAsUser = [
        'email' => '<EMAIL>',
        'password' => '<PASSWORD>'
    ];

    protected $requestOptions = [];

    protected $hosts = [
        'http://webserver',
        'http://erdiko.local',
        'http://erdiko_users_web',
        'http://localhost',
        'http://127.0.0.1',
        'http://0.0.0.0',
    ];

    public function testCurl()
    {
        foreach ($this->hosts as $host) {
            $this->callWithCurl($host);
        }
    }

    public function testGuzzle()
    {
        foreach ($this->hosts as $host) {
            $this->callWithGuzzle($host);
        }
    }

    protected function callWithCurl($host)
    {
        $data_string = json_encode($this->credentialsAsSuper);

        $ch = curl_init($host.'/ajax/users/authentication/login');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
        );

        $result = curl_exec($ch);

        var_dump($result);
    }

    protected function callWithGuzzle($host)
    {
        $this->client = new Client(['base_uri' => $host]);

        try {
            $response = $this->client->post('/ajax/users/authentication/login', ['json' => $this->credentialsAsSuper]);
            $jsonResponse = $this->getJsonResponse($response);

            var_dump($jsonResponse);
        } catch (\Exception $e) {
            var_dump( $e->getMessage() );
        }

    }

//    public function setup()
//    {
//        $this->client = new Client(['base_uri' => 'http://erdiko.local/']);
//    }
//
//    protected function login($credentials)
//    {
//        $response = $this->client->post('/ajax/users/authentication/login', ['json' => $credentials]);
//        $phpsessid = $this->getPhpsessid($response->getHeader('Set-Cookie'));
//
//        $jsonResponse = $this->getJsonResponse($response);
//        $token = $jsonResponse->body->token;
//
//        $this->requestOptions = ['headers' => [
//            'Content-Type' => 'application/json',
//            'Authorization' => 'Bearer '.$token,
//            'Cookie' => $phpsessid
//        ]];
//    }
//
//    protected function loginAsUser()
//    {
//        $this->login($this->credentialsAsUser);
//    }
//
//    protected function loginAsSuper()
//    {
//        $this->login($this->credentialsAsSuper);
//    }
//
//    protected function getPhpsessid($setCookie)
//    {
//        $setCookieRaw = explode(';', $setCookie[0]);
//        return $setCookieRaw[0];
//    }
//
    protected function getJsonResponse($response)
    {
        return json_decode($response->getBody());
    }
//
//    protected function setJsonDataRequest($data)
//    {
//        $this->requestOptions['json'] = $data;
//    }
//
//    public function testPostCreateSuccess()
//    {
//        $this->loginAsSuper();
//        $response = $this->postCreate();
{'fraction_non_alphanumeric': 0.17825537294563842, 'fraction_numerical': 0.0035117291754459897, 'mean_word_length': 2.2378353797180535, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '4645063', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2192, 'n_words': 333}