mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 02:47:34 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			168 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			168 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/**
 | 
						|
 * Invoice Ninja (https://invoiceninja.com).
 | 
						|
 *
 | 
						|
 * @link https://github.com/invoiceninja/invoiceninja source repository
 | 
						|
 *
 | 
						|
 * @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
 | 
						|
 *
 | 
						|
 * @license https://opensource.org/licenses/AAL
 | 
						|
 */
 | 
						|
namespace Tests\Feature\Payments;
 | 
						|
 | 
						|
use App\DataMapper\ClientSettings;
 | 
						|
use App\Factory\ClientFactory;
 | 
						|
use App\Factory\CreditFactory;
 | 
						|
use App\Factory\InvoiceFactory;
 | 
						|
use App\Factory\InvoiceItemFactory;
 | 
						|
use App\Factory\PaymentFactory;
 | 
						|
use App\Helpers\Invoice\InvoiceSum;
 | 
						|
use App\Models\Client;
 | 
						|
use App\Models\ClientContact;
 | 
						|
use App\Models\Credit;
 | 
						|
use App\Models\Invoice;
 | 
						|
use App\Models\Payment;
 | 
						|
use App\Utils\Traits\MakesHash;
 | 
						|
use Illuminate\Database\Eloquent\Model;
 | 
						|
use Illuminate\Foundation\Testing\DatabaseTransactions;
 | 
						|
use Illuminate\Foundation\Testing\WithoutEvents;
 | 
						|
use Illuminate\Routing\Middleware\ThrottleRequests;
 | 
						|
use Illuminate\Support\Facades\Session;
 | 
						|
use Illuminate\Validation\ValidationException;
 | 
						|
use Tests\MockAccountData;
 | 
						|
use Tests\MockUnitData;
 | 
						|
use Tests\TestCase;
 | 
						|
 | 
						|
/**
 | 
						|
 * @test
 | 
						|
 */
 | 
						|
class StorePaymentValidationTest extends TestCase
 | 
						|
{
 | 
						|
    use MakesHash;
 | 
						|
    use DatabaseTransactions;
 | 
						|
    use MockAccountData;
 | 
						|
    use WithoutEvents;
 | 
						|
 | 
						|
    public function setUp() :void
 | 
						|
    {
 | 
						|
        parent::setUp();
 | 
						|
 | 
						|
        Session::start();
 | 
						|
 | 
						|
        $this->faker = \Faker\Factory::create();
 | 
						|
 | 
						|
        Model::reguard();
 | 
						|
 | 
						|
        $this->makeTestData();
 | 
						|
 | 
						|
 | 
						|
        $this->withoutMiddleware(
 | 
						|
            ThrottleRequests::class
 | 
						|
        );
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public function testValidPayment()
 | 
						|
    {
 | 
						|
 | 
						|
       $data = [
 | 
						|
            'amount' => 0,
 | 
						|
            'client_id' => $this->client->hashed_id,
 | 
						|
            'invoices' => [
 | 
						|
            ],
 | 
						|
            'date' => '2019/12/12',
 | 
						|
        ];
 | 
						|
 | 
						|
        $response = false;
 | 
						|
 | 
						|
        try {
 | 
						|
            $response = $this->withHeaders([
 | 
						|
            'X-API-SECRET' => config('ninja.api_secret'),
 | 
						|
            'X-API-TOKEN' => $this->token,
 | 
						|
        ])->post('/api/v1/payments/', $data);
 | 
						|
        } catch (ValidationException $e) {
 | 
						|
            $message = json_decode($e->validator->getMessageBag(), 1);
 | 
						|
            nlog($e->validator->getMessageBag());
 | 
						|
        }
 | 
						|
 | 
						|
        $response->assertStatus(200);
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public function testValidPaymentWithAmount()
 | 
						|
    {
 | 
						|
 | 
						|
       $data = [
 | 
						|
            'amount' => 0,
 | 
						|
            'client_id' => $this->client->hashed_id,
 | 
						|
            'invoices' => [
 | 
						|
                [
 | 
						|
                    'invoice_id' => $this->invoice->hashed_id,
 | 
						|
                    'amount' => 10,
 | 
						|
                ],
 | 
						|
            ],
 | 
						|
            'credits' => [
 | 
						|
                [
 | 
						|
                    'credit_id' => $this->credit->hashed_id,
 | 
						|
                    'amount' => 5
 | 
						|
                ]
 | 
						|
            ],
 | 
						|
            'date' => '2019/12/12',
 | 
						|
        ];
 | 
						|
 | 
						|
        $response = false;
 | 
						|
 | 
						|
        try {
 | 
						|
            $response = $this->withHeaders([
 | 
						|
            'X-API-SECRET' => config('ninja.api_secret'),
 | 
						|
            'X-API-TOKEN' => $this->token,
 | 
						|
        ])->post('/api/v1/payments/', $data);
 | 
						|
        } catch (ValidationException $e) {
 | 
						|
            $message = json_decode($e->validator->getMessageBag(), 1);
 | 
						|
            nlog($e->validator->getMessageBag());
 | 
						|
        }
 | 
						|
 | 
						|
        $response->assertStatus(200);
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    public function testValidPaymentWithInvalidData()
 | 
						|
    {
 | 
						|
 | 
						|
        
 | 
						|
       $data = [
 | 
						|
            'amount' => 0,
 | 
						|
            'client_id' => $this->client->hashed_id,
 | 
						|
            'invoices' => [
 | 
						|
                [
 | 
						|
                    'invoice_id' => $this->invoice->hashed_id,
 | 
						|
                ],
 | 
						|
            ],
 | 
						|
            'credits' => [
 | 
						|
                [
 | 
						|
                    'credit_id' => $this->credit->hashed_id,
 | 
						|
                    'amount' => 5
 | 
						|
                ]
 | 
						|
            ],
 | 
						|
            'date' => '2019/12/12',
 | 
						|
        ];
 | 
						|
 | 
						|
        $response = false;
 | 
						|
 | 
						|
        try{
 | 
						|
            $response = $this->withHeaders([
 | 
						|
                'X-API-SECRET' => config('ninja.api_secret'),
 | 
						|
                'X-API-TOKEN' => $this->token,
 | 
						|
            ])->post('/api/v1/payments/', $data);
 | 
						|
        }catch(ValidationException $e){
 | 
						|
            $response->assertStatus(302);
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
}
 | 
						|
 |