简体   繁体   中英

php unit test in silverstripe

Want to know how to start write unit test for this function in siverstripe?

function getAvailableScanners($date){
        $scanners = self::get("Scanner");

        $startDate = date("Y-m-d", strtotime("-".self::$daysNeededAfterLastGig." days", strtotime($date)));
        $endDate = date("Y-m-d", strtotime("+".self::$daysNeededBeforeNextGig." days", strtotime($date)));

        foreach($scanners as $scanner){
            $events = EventProduct::get("EventProduct", "\"ExpiryDate\">='$startDate' AND \"ExpiryDate\"<='$endDate'");
            if($events){
                foreach($events as $event){
                    if($scanner->isAssignedToEvent($event)){
                        $scanners->remove($scanner);
                    }
                }
            }
        }

        return $scanners;
    }

The documentation has several good pages on writing tests with SilverStripe

http://doc.silverstripe.org/sapphire/en/topics/testing/

To get started want to make a fixture file with a bunch of those EventProduct products (documentation has an example of this).

Also you can find lots of good testing examples from looking at the built-in tests. I've picked out the BlogHolder tests as this does something very similar to what you're looking for (testing a customer 'getter' with filters)

https://github.com/silverstripe/silverstripe-blog/blob/master/tests/BlogHolderTest.php (and the fixture file is BlogTree.yml in that same folder)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM