Custom PHP classes and unit testing

Game development with Board Game Arena Studio
Post Reply
User avatar
arustleund
Posts: 5
Joined: 30 August 2016, 05:38

Custom PHP classes and unit testing

Post by arustleund »

Hi everyone! After successfully developing my first game on BGA, I wanted to try working on something more advanced. I've done some simple PHP programming, including my game here, but as I got started on my next game, I realized I wanted to start using more advanced PHP techniques, like objects and unit testing. I almost immediately ran into issues, as the game logic must be defined inside the extension of the Table class. Has anyone found a good way to define custom PHP classes and use them within their game. I would like to define certain game objects and use them both within the game logic, and within unit tests so I can make sure everything is working before I deploy to the server. I noticed the Code Sharing page, but I didn't see any instructions on how to set up unit tests or custom classes. Any help would be greatly appreciated!! :)
User avatar
Victoria_La
Posts: 608
Joined: 28 December 2015, 20:55

Re: Custom PHP classes and unit testing

Post by Victoria_La »

You can use custom classes, and it works in studio when they are in multiple files but for production it has to be in ONE file (few classes in one file works fine exception file is huge)

I did some unit testing as well, work fine too, you have to mock all the superclases, some mocks are available in sharedcode project (in git)

Code: Select all

<?php
define( "APP_GAMEMODULE_PATH", "../sharedcode/" ); // include path to mocks, this defined "Table" and other classes
require_once ('../madeira/madeira.game.php'); // include real game class

class MadeiraTest extends Madeira {

    function MadeiraTest() {
        parent::__construct();
        include '../../madeira/madeira/material.inc.php';
        $this->resources = array ();
    }
    // override methods here that access db and stuff
        
}

$x = new MadeiraTest();
$requ = array (
        'crownreq_1_5',
        'crownreq_2_1' 
);
$x->sactionCrownReqScore ( $requ, 13 ); // real function from Madeira class
The only caveat is when you call any method your override you have to call it $this->mymethod() not self::mymethod() from the real game code.
For that reason I never use self:: anymore in my php code.

(I just checked in this into sharecode project: https://github.com/elaskavaia/bga-share ... e.test.php)
User avatar
arustleund
Posts: 5
Joined: 30 August 2016, 05:38

Re: Custom PHP classes and unit testing

Post by arustleund »

Thanks for the response! Do you have a suggestion for where the custom classes should be defined, and how they should be included in the other PHP files. The first thing I wanted to try was to define a custom class for some of my game material. Is it possible to define a class that can be used by both the material file and the game file? Thanks very much for your help!
User avatar
Victoria_La
Posts: 608
Joined: 28 December 2015, 20:55

Re: Custom PHP classes and unit testing

Post by Victoria_La »

Material is include in a php file like in example from prev post, so its technically in one file, if you define you class in game.php you can have access to it from material.
What exactly you trying to do in material file? (you can message me privately if you want)

Also example from my first post shows how to include a php file, what else do you need?

This is example how to share class between main and material (suing my sharedcode project)

.game.php

Code: Select all

require_once (APP_GAMEMODULE_PATH . 'module/table/table.game.php');

// insert between require_one and original game class
class MyCoolClass {
    function __construct() {
        
    }
}
class SharedCode extends Table {
...
material.inc.php

Code: Select all

$this->myclass = new MyCoolClass();
User avatar
arustleund
Posts: 5
Joined: 30 August 2016, 05:38

Re: Custom PHP classes and unit testing

Post by arustleund »

Thanks for all of the great information. I'm still a bit of a PHP newbie and wasn't sure what was legal within the BGA framework as well. Your examples really helped, thanks!
User avatar
dhau
Posts: 3
Joined: 22 February 2014, 23:28

Re: Custom PHP classes and unit testing

Post by dhau »

I've been developing a version of battle for hill 218 which uses custom classes and tests. It would be worth a look for you:
https://github.com/danielholmes/battle-for-hill-218
User avatar
arustleund
Posts: 5
Joined: 30 August 2016, 05:38

Re: Custom PHP classes and unit testing

Post by arustleund »

dhau wrote:I've been developing a version of battle for hill 218 which uses custom classes and tests. It would be worth a look for you:
https://github.com/danielholmes/battle-for-hill-218
Thanks, dhau, this is exactly what I'm looking for! Thanks for sharing!!
Post Reply

Return to “Developers”