PHP code example of lifterlms / lifterlms-tests
1. Go to this page and download the library: Download lifterlms/lifterlms-tests library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lifterlms / lifterlms-tests example snippets
$method = LLMS_Unit_Test_Util::get_private_method( new MyClass(), 'my_private_method' );
$method = LLMS_Unit_Test_Util::get_protected_method( new MyClass(), 'my_private_method' );
$result = LLMS_Unit_Test_Util::call_method( new MyClass(), 'my_private_method', array( 'argument_1', 'arg_2', ... ) );
$this->assertTrue( $result );
public function test_mock_https_request() {
// Mocks a WP REST post creation request.
$this->mock_http_request( '/wp-json/wp/v2/posts',
[
'body' => '{"id":123,"title":"Mock Title",...}',
'response' => [
'code' => 201,
],
],
true
);
$res = wp_remote_post(
rest_url( '/wp-json/wp/v2/posts' ),
[
'body' => [
'title' => 'Mock Title',
],
],
);
$this->assertEquals( 201, wp_remote_retrieve_response_code( $res ) );
$this->assertEquals( 123, json_decode( wp_remote_retrieve_response_body( $res ) )['id'] );
}
##### Utility Methods
+ Get the output of a function: `$output = $this->get_output( $callable, $args_array );`
## Exceptions
Included exceptions allow easy testing of methods which call `exit()` and `llms_redirect_and_exit()`.
##### LLMS_Unit_Test_Exception_Exit
Test methods which call `exit()`: Call `$this->expectException( LLMS_Unit_Test_Exception_Exit::class );` before calling the function that calls exit.
public function test_my_redirect_and_exit() {
$this->expectException( LLMS_Unit_Test_Exception_Redirect::class );
$this->expectExceptionMessage( 'https://lifterlms.com [302] YES' );
llms_redirect_and_exit( 'https://lifterlms.com' );
}
public function test_my_redirect_and_exit() {
$this->expectException( LLMS_Unit_Test_Exception_Redirect::class );
$this->expectExceptionMessage( 'https://lifterlms.com [302] YES' );
try {
llms_redirect_and_exit( 'https://lifterlms.com' );
} catch( LLMS_Unit_Test_Exception_Redirect $exception ) {
// Any additional assertions can be added here.
$this->assertTrue( ... );
throw $exception;
}
}
$args = array(
'sections' => 2, // 2 sections in the course
'lessons' => 5, // 5 lessons per section
'quizzes' => 1, // 1 quiz per section (will always be the last lesson in the section)
'questions' => 5, // 5 questions per quiz
);
$course_id = $this->factory->course->create( $args );
$course = $this->factory->course->create_and_get( $args );
$course_id = $this->factory->course->create();
// single student
$student_id = $this->factory->student->create_and_enroll( $coursed_id );
// multiple students
$student_ids = $this->factory->student->create_and_enroll_many( 5, $coursed_id );
$this->cookies->set( $name, $value, ... )
$this->cookies->set( 'name', 'value', 0, ... );
// Retrieve all cookies
$cookies = $this->cookies->get_all();
var_dump( $cookies );
// array(
// 'name' => array(
// 'value' => 'value',
// 'expires' => 0,
// ...
// ),
// )
//
// Retrieve a single cookie.
$cookie = $this->cookies->get( 'name' );
var_dump( $cookie );
// array(
// 'value' => 'value',
// 'expires' => 0,
// ...
// ),
$this->cookies->expect_success();
$this->assertTrue( llms_setcookie( 'name', 'val' ) );
$this->cookies->expect_error();
$this->assertFalse( llms_setcookie( 'name', 'val' ) );
$this->cookies->unset_all()
var_dump( $this->cookies->get_all() );
// array()
llms_setcookie( 'name', 'val' );
$this->cookies->unset( 'name' );
var_dump( $this->cookies->get( 'name' ) );
// null