<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace Tests\Libraries\Payments;

use App\Libraries\Payments\NotificationType;
use App\Libraries\Payments\PaymentSignature;
use App\Models\Store\Order;
use App\Models\Store\OrderItem;
use Tests\TestCase;

class PaymentProcessorTest extends TestCase
{
    private Order $order;
    private TestPaymentProcessor $subject;

    public function testCancelWithoutPayment()
    {
        $this->expectsEvents('store.payments.error.test');
        $this->doesntExpectEvents('store.payments.cancelled.test');

        $this->subject->run();

        $this->order->refresh();

        $this->assertTrue($this->order->isCancelled());
    }

    public function testCancelWithPayment()
    {
        $this->doesntExpectEvents('store.payments.error.test');
        $this->expectsEvents('store.payments.cancelled.test');

        $payment = $this->order->payments()->create([
            'country_code' => 'CC',
            'paid_at' => now(),
            'provider' => 'test',
            'transaction_id' => $this->order->getProviderReference(),
        ]);

        $this->order->paid($payment);

        $this->subject->run();

        $this->order->refresh();

        $this->assertTrue($this->order->isCancelled());
        $this->assertTrue($this->order->payments()->where('cancelled', true)->exists());
    }


    public function testCancelWithCancelledPayment()
    {
        $this->expectsEvents('store.payments.error.test');
        $this->doesntExpectEvents('store.payments.cancelled.test');

        $payment = $this->order->payments()->create([
            'cancelled' => true,
            'country_code' => 'CC',
            'paid_at' => now(),
            'provider' => 'test',
            'transaction_id' => $this->order->getProviderReference(),
        ]);

        $this->order->paid($payment);

        $this->subject->run();

        $this->order->refresh();

        $this->assertTrue($this->order->isCancelled());
        $this->assertTrue($this->order->payments()->where('cancelled', true)->exists());
    }

    protected function setUp(): void
    {
        parent::setUp();

        config()->set('store.order.prefix', 'test');

        $this->order = factory(Order::class)->states('checkout')->create([
            'transaction_id' => 'test-123',
        ]);
        factory(OrderItem::class)->states('supporter_tag')->create(['order_id' => $this->order->getKey()]);

        $this->subject = new TestPaymentProcessor([
            'countryCode' => 'CC',
            'notificationType' => NotificationType::REFUND,
            'orderNumber' => "test-{$this->order->user_id}-{$this->order->getKey()}",
            'paymentTransactionId' => '123',
            'paymentDate' => json_time(now()),
        ], $this->validSignature());
    }

    private function validSignature()
    {
        return new class implements PaymentSignature {
            public function isValid()
            {
                return true;
            }
        };
    }
}<commit_msg>alias test payment processor name<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace Tests\Libraries\Payments;

use App\Libraries\Payments\NotificationType;
use App\Libraries\Payments\PaymentSignature;
use App\Models\Store\Order;
use App\Models\Store\OrderItem;
use Tests\Libraries\Payments\TestPaymentProcessor as PaymentProcessor;
use Tests\TestCase;

class PaymentProcessorTest extends TestCase
{
    private Order $order;
    private PaymentProcessor $subject;

    public function testCancelWithoutPayment()
    {
        $this->expectsEvents('store.payments.error.test');
        $this->doesntExpectEvents('store.payments.cancelled.test');

        $this->subject->run();

        $this->order->refresh();

        $this->assertTrue($this->order->isCancelled());
    }

    public function testCancelWithPayment()
    {
        $this->doesntExpectEvents('store.payments.error.test');
        $this->expectsEvents('store.payments.cancelled.test');

        $payment = $this->order->payments()->create([
            'country_code' => 'CC',
            'paid_at' => now(),
            'provider' => 'test',
            'transaction_id' => $this->order->getProviderReference(),
        ]);

        $this->order->paid($payment);

        $this->subject->run();

        $this->order->refresh();

        $this->assertTrue($this->order->isCancelled());
        $this->assertTrue($this->order->payments()->where('cancelled', true)->exists());
    }


    public function testCancelWithCancelledPayment()
    {
        $this->expectsEvents('store.payments.error.test');
        $this->doesntExpectEvents('store.payments.cancelled.test');

        $payment = $this->order->payments()->create([
            'cancelled' => true,
            'country_code' => 'CC',
            'paid_at' => now(),
            'provider' => 'test',
            'transaction_id' => $this->order->getProviderReference(),
        ]);

        $this->order->paid($payment);

        $this->subject->run();

        $this->order->refresh();

        $this->assertTrue($this->order->isCancelled());
        $this->assertTrue($this->order->payments()->where('cancelled', true)->exists());
    }

    protected function setUp(): void
    {
        parent::setUp();

        config()->set('store.order.prefix', 'test');

        $this->order = factory(Order::class)->states('checkout')->create([
            'transaction_id' => 'test-123',
        ]);
        factory(OrderItem::class)->states('supporter_tag')->create(['order_id' => $this->order->getKey()]);

        $this->subject = new PaymentProcessor([
            'countryCode' => 'CC',
            'notificationType' => NotificationType::REFUND,
            'orderNumber' => "test-{$this->order->user_id}-{$this->order->getKey()}",
            'paymentTransactionId' => '123',
            'paymentDate' => json_time(now()),
        ], $this->validSignature());
    }

    private function validSignature()
    {
        return new class implements PaymentSignature {
            public function isValid()
            {
                return true;
            }
        };
    }
}
{'fraction_non_alphanumeric': 0.16954425260538186, 'fraction_numerical': 0.002488722974023954, 'mean_word_length': 2.645124716553288, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 169, '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': '5101331', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1780, 'n_words': 292}