Jump to content

Recommended Posts

Posted (edited)

Hello there friends,

As a learning challenge, with Claude Sonnet AI's help, I installed the PEST testing framework and created a bunch of tests for Ryan's Invoices site profile. I'm pretty happy with it. 🙂🙂

You can check the project on GitHub: https://github.com/sjardim/processwire-invoices-with-pest

To try it, just install the site profile on a fresh PW project as instructed here: https://processwire.com/blog/posts/invoices-site-profile/ and copy my project's :

  • the entire /test folder,
  • phpunit.xml file
  • and composer.json file

and install composer dependencies. Next, in your terminal, run `./vendor/bin/pest --filter=InvoicePageTest` to see the magic.

Here's a preview of the InvoicePageTest class:
 

Spoiler
<?php

// Helper functions for creating test data
function createTestInvoice() {
    $pages = pages();
    $parent = $pages->get('/invoices/'); // Adjust path as needed
    
    if (!$parent->id) {
        $parent = $pages->get(1);
    }
    
    $invoice = $pages->add('invoice', $parent, [
        'title' => 'Test Invoice ' . time(),
        'date' => time(),
    ]);
    
    $invoice->save();
    return $invoice;
}

// Helper functions for creating test data
function createInvoiceDayPage($qty) {
    $pages = pages();
    $parent = $pages->get('/settings/invoice-days/');
    
    if (!$parent->id) {
        // If path doesn't exist, use settings or root
        $settings = $pages->get('/settings/');
        $parent = $settings->id ? $settings : $pages->get(1);
    }
    
    $days = $pages->add('invoice-day', $parent, [
        'title' => "$qty Days",
        'qty' => $qty,
    ]);
    
    $days->save();
    return $days;
}

function cleanup($page) {
    if ($page && $page->id) {
        pages()->delete($page, true);
    }
}

beforeEach(function() {
    $this->invoice = null;

    // Get the admin user (ID 41 is the default admin user created during install)
    $admin = pages()->get("id=41");
    if (!$admin->id) {
        // Fallback to finding admin by name/role if ID is different
        $admin = pages()->get("template=user, name=admin");
    }
    
    // Store the current user so we can restore it later
    $this->previousUser = wire('user');
    
    // Log in as admin
    if($admin->isSuperuser()) {
        wire('users')->setCurrentUser($admin);
    }
});

afterEach(function() {

    if (isset($this->invoice) && $this->invoice) {
        cleanup($this->invoice);
    }

  
});

test('getSubtotal calculates correct invoice subtotal', function() {
    $invoice = createTestInvoice();
    
    $invoice->addLineItem('hours', 'Development', 10, 100);
    $invoice->save();
    
    $invoice->addLineItem('service', 'Consulting', 5, 150);
    $invoice->save();
    
    $invoice->addLineItem('product', 'License', 1, 500);
    $invoice->save();
    
    expect($invoice->getSubtotal())->toBe(2250.0);
    expect($invoice->getSubtotal())->toBeNumeric(2250.0);
    
    $invoice->addLineItem('hours', 'Support', 2, 75);
    $invoice->save();
    
    expect($invoice->getSubtotal())->toBe(2400.0);
    expect($invoice->getSubtotal())->toBeNumeric(2400.0);
    
    $invoice->addLineItem('hours', 'Planning', 2.5, 90.50);
    $invoice->save();
    
    expect($invoice->getSubtotal())->toBe(2626.25);
    expect($invoice->getSubtotal())->toBeNumeric();
    
    cleanup($invoice);
});

 

I hope this help you in your testing journey!

 

Edited by Sergio
Add tags
  • Like 5
  • Thanks 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...