2020-02-12 10:32:57 +00:00
< ? php
namespace Tests\Feature ;
2021-06-30 04:00:41 +00:00
use Illuminate\Foundation\Testing\DatabaseTransactions ;
use Illuminate\Support\Arr ;
2020-02-12 10:32:57 +00:00
use Tests\TestCase ;
2021-06-30 04:00:41 +00:00
use App\Models\ { Account , Group , Product , Service , Site };
2020-02-12 10:32:57 +00:00
class InvoiceTest extends TestCase
{
2021-06-30 04:00:41 +00:00
use DatabaseTransactions ;
private array $setup ;
private function account_setup () : void
{
$this -> setup [ 'account' ] = [
2021-07-02 00:03:36 +00:00
'a' => Account :: factory () -> create ([ 'site_id' => Arr :: get ( $this -> setup , 'site.a' ) -> site_id ]),
'b' => Account :: factory () -> create ([ 'site_id' => Arr :: get ( $this -> setup , 'site.b' ) -> site_id ])
2021-06-30 04:00:41 +00:00
];
}
private function site_setup () : void
{
$this -> setup [ 'site' ] = [
'a' => Site :: factory () -> create ([ 'url' => 'Test A' ]),
'b' => Site :: factory () -> create ([ 'url' => 'Test B' ])
];
2021-07-02 00:03:36 +00:00
Group :: factory () -> create ([ 'site_id' => Arr :: get ( $this -> setup , 'site.a' ) -> site_id ]);
Group :: factory () -> create ([ 'site_id' => Arr :: get ( $this -> setup , 'site.b' ) -> site_id ]);
2021-06-30 04:00:41 +00:00
}
2020-02-12 10:32:57 +00:00
/**
* A basic feature test example .
*
* @ return void
*/
public function testInvoiceAmount ()
{
2021-06-30 04:00:41 +00:00
$this -> site_setup ();
$this -> account_setup ();
2020-02-12 10:32:57 +00:00
2022-04-20 07:05:49 +00:00
dump ( $this -> setup [ 'site' ]);
2021-06-30 04:00:41 +00:00
// Create two services for the same account
2020-02-12 10:32:57 +00:00
// First service was billed a month ago, so this invoice will have 1 service charge
2022-04-20 07:05:49 +00:00
$po = Product :: factory () -> notStrict () -> create ([
'site_id' => Arr :: get ( $this -> setup , 'site.a' ) -> site_id ,
]);
dump ( $po );
2021-06-30 04:00:41 +00:00
$o = Service :: factory () -> create ([
2021-07-02 00:03:36 +00:00
'site_id' => Arr :: get ( $this -> setup , 'site.a' ) -> site_id ,
2021-06-30 04:00:41 +00:00
'account_id' => Arr :: get ( $this -> setup , 'account.a' ) -> id ,
2022-04-20 07:05:49 +00:00
'product_id' => $po -> id ,
2021-06-30 04:00:41 +00:00
]);
$o -> setRelation ( 'product' , $po );
2020-02-12 10:32:57 +00:00
$this -> assertEquals ( 110 , $o -> next_invoice_items ( FALSE ) -> sum ( 'total' ), 'Invoice Equals 110' );
2021-06-30 04:00:41 +00:00
/*
2020-02-12 10:32:57 +00:00
// Second service wasnt billed, connected 1.5 months ago, so invoice will have 2 service charges - 1 x 0.5 month and 1 x full month. and a connection charge
$o = factory ( Service :: class ) -> states ( 'new-connect' ) -> make ();
$o -> setRelation ( 'product' , factory ( Product :: class ) -> states ( 'strict' ) -> make ());
$this -> assertEqualsWithDelta ( 110 + 110 + 55 + 55 , $o -> next_invoice_items ( FALSE ) -> sum ( 'total' ), 2.5 , 'Invoice Equals 220' );
2021-06-30 04:00:41 +00:00
*/
2020-02-12 10:32:57 +00:00
}
}