<commit_before><?php

namespace Adldap\Tests\Log;

use Adldap\Models\User;
use Adldap\Tests\TestCase;
use Adldap\Log\EventLogger;
use Adldap\Auth\Events\Failed;
use Adldap\Auth\Events\Event as AuthEvent;
use Adldap\Models\Events\Event as ModelEvent;
use Adldap\Connections\ConnectionInterface;
use Psr\Log\LoggerInterface;

class EventLoggerTest extends TestCase
{
    public function test_auth_events_are_logged()
    {
        $e = $this->mock(AuthEvent::class);
        $l = $this->mock(LoggerInterface::class);
        $c = $this->mock(ConnectionInterface::class);

        $log = 'LDAP (ldap://192.168.1.1) - Connection: domain-a - Operation: Mockery_4_Adldap_Auth_Events_Event - Username: <EMAIL>';

        $l->shouldReceive('info')->once()->with($log);

        $c
            ->shouldReceive('getHost')->once()->andReturn('ldap://192.168.1.1')
            ->shouldReceive('getName')->once()->andReturn('domain-a');

        $e
            ->shouldReceive('getConnection')->once()->andReturn($c)
            ->shouldReceive('getUsername')->once()->andReturn('<EMAIL>');

        $eLogger = new EventLogger($l);

        $this->assertNull($eLogger->auth($e));
    }

    public function test_failed_auth_event_reports_result()
    {
        $l = $this->mock(LoggerInterface::class);
        $c = $this->mock(ConnectionInterface::class);

        $e = new Failed($c, '<EMAIL>', 'super-secret');

        $log = 'LDAP (ldap://192.168.1.1) - Connection: domain-a - Operation: Adldap\Auth\Events\Failed - Username: <EMAIL> - Reason: Invalid Credentials';

        $l->shouldReceive('warning')->once()->with($log);

        $c
            ->shouldReceive('getHost')->once()->andReturn('ldap://192.168.1.1')
            ->shouldReceive('getName')->once()->andReturn('domain-a')
            ->shouldReceive('getLastError')->once()->andReturn('Invalid Credentials');

        $eLogger = new EventLogger($l);

        $this->assertNull($eLogger->auth($e));
    }

    public function test_model_events_are_logged()
    {
        $c = $this->mock(ConnectionInterface::class);

        $b = $this->newBuilder($c);

        $dn = 'cn=<NAME>,dc=corp,dc=acme,dc=org';

        $u = new User(['dn' => $dn], $b);

        $l = $this->mock(LoggerInterface::class);

        $eLogger = new EventLogger($l);

        $me = $this->mock(ModelEvent::class);

        $me->shouldReceive('getModel')->once()->andReturn($u);

        $log = "LDAP (ldap://192.168.1.1) - Connection: domain-a - Operation: Mockery_6_Adldap_Models_Events_Event - On: Adldap\Models\User - Distinguished Name: $dn";

        $l->shouldReceive('info')->once()->with($log);

        $c
            ->shouldReceive('getHost')->once()->andReturn('ldap://192.168.1.1')
            ->shouldReceive('getName')->once()->andReturn('domain-a');

        $this->assertNull($eLogger->model($me));
    }
}<commit_msg>Fixed tests<commit_after><?php

namespace Adldap\Tests\Log;

use Psr\Log\LoggerInterface;
use Adldap\Tests\TestCase;
use Adldap\Log\EventLogger;
use Adldap\Models\User;
use Adldap\Models\Events\Creating;
use Adldap\Query\Events\Search;
use Adldap\Auth\Events\Failed;
use Adldap\Auth\Events\Binding;
use Adldap\Connections\ConnectionInterface;

class EventLoggerTest extends TestCase
{
    public function test_auth_events_are_logged()
    {
        $connection = $this->mock(ConnectionInterface::class);

        $connection
            ->shouldReceive('getHost')->once()->andReturn('ldap://192.168.1.1')
            ->shouldReceive('getName')->once()->andReturn('domain-a');

        $logger = $this->mock(LoggerInterface::class);

        $log = "LDAP (ldap://192.168.1.1)"
            ." - Connection: domain-a"
            ." - Operation: Binding"
            ." - Username: <EMAIL>";

        $logger->shouldReceive('info')->once()->with($log);

        $eventLogger = new EventLogger($logger);

        $this->assertNull($eventLogger->log(new Binding($connection, '<EMAIL>', 'super-secret')));
    }

    public function test_failed_auth_event_reports_result()
    {
        $connection = $this->mock(ConnectionInterface::class);

        $connection
            ->shouldReceive('getHost')->once()->andReturn('ldap://192.168.1.1')
            ->shouldReceive('getName')->once()->andReturn('domain-a')
            ->shouldReceive('getLastError')->once()->andReturn('Invalid Credentials');

        $logger = $this->mock(LoggerInterface::class);

        $log = "LDAP (ldap://192.168.1.1)"
            ." - Connection: domain-a"
            ." - Operation: Failed"
            ." - Username: <EMAIL>"
            ." - Reason: Invalid Credentials";

        $logger->shouldReceive('warning')->once()->with($log);

        $eventLogger = new EventLogger($logger);

        $this->assertNull($eventLogger->log(new Failed($connection, '<EMAIL>', 'super-secret')));
    }

    public function test_model_events_are_logged()
    {
        $connection = $this->mock(ConnectionInterface::class);

        $connection
            ->shouldReceive('getHost')->once()->andReturn('ldap://192.168.1.1')
            ->shouldReceive('getName')->once()->andReturn('domain-a');

        $logger = $this->mock(LoggerInterface::class);

        $dn = 'cn=<NAME>,dc=corp,dc=acme,dc=org';

        $log = "LDAP (ldap://192.168.1.1)"
            ." - Connection: domain-a"
            ." - Operation: Creating"
            ." - On: Adldap\Models\User"
            ." - Distinguished Name: $dn";

        $logger->shouldReceive('info')->once()->with($log);

        $query = $this->newBuilder($connection);

        $user = new User(['dn' => $dn], $query);

        $eventLogger = new EventLogger($logger);

        $this->assertNull($eventLogger->log(new Creating($user)));
    }

    public function test_query_events_are_logged()
    {
        $connection = $this->mock(ConnectionInterface::class);

        $connection
            ->shouldReceive('getHost')->once()->andReturn('ldap://192.168.1.1')
            ->shouldReceive('getName')->once()->andReturn('domain-a');

        $logger = $this->mock(LoggerInterface::class);

        $log = "LDAP (ldap://192.168.1.1)"
            ." - Connection: domain-a"
            ." - Operation: Search"
            ." - Base DN: "
            ." - Filter: (objectclass=*)"
            ." - Selected: (*)"
            ." - Time Elapsed: 10";

        $logger->shouldReceive('info')->once()->with($log);

        $eventLogger = new EventLogger($logger);

        $query = $this->newBuilder($connection);

        $this->assertNull($eventLogger->log(new Search($query, 10)));
    }
}
{'fraction_non_alphanumeric': 0.18812785388127853, 'fraction_numerical': 0.01796042617960426, 'mean_word_length': 2.78078250863061, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 126, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 14, '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': '5805935', 'n_tokens_mistral': 2122, 'n_tokens_neox': 1961, 'n_words': 285}