Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Latest commit

 

History

History
194 lines (149 loc) · 3.17 KB

assertions.md

File metadata and controls

194 lines (149 loc) · 3.17 KB

assertion 斷言

assertTrue

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_true() 
    {
        $actual = false;

        $this->assertTrue($actual);
    }
}

assertFalse

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_false() 
    {
        $actual = false;

        $this->assertFalse($actual);
    }
}

assertEquals

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_equals() 
    {
        $expected = true;
        $actual = true;
        
        $this->assertEquals($expected, $actual);
    }
}

assertSame

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_same() 
    {
        $expected = true;
        $actual = true;
        
        $this->assertSame($expected, $actual);
    }
}

assertContains

assertContains (array)

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_contains_array() 
    {
        $needle = 3;
        
        $this->assertContains($needle, [1, 3, 5, 7, 9]);
    }
}

assertContains (string)

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_contains_string()
    {
        $needle = 'oba';
        
        $this->assertContains($needle, 'foobar');
    }
}

assertCount

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_count() 
    {
        $this->assertCount(5, [1, 3, 5, 7, 9]);
    }
}

assertArrayHasKey

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_array_has_key() 
    {
        $this->assertArrayHasKey('bar', ['bar' => 'baz']);
    }
}

assertArraySubset

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_array_subset()
    {
        $this->assertArraySubset(['config' => ['key-a']], ['config' => ['key-a', 'key-c']]);
    }
}

assertInternalType

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_internal_type()
    {
        $this->assertInternalType('numeric', 1);
        $this->assertInternalType('numeric', 1.0);
        $this->assertInternalType('int', 1);
        $this->assertInternalType('float', 1.0);
        $this->assertInternalType('string', '1');
    }
}

assertInstanceOf

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_instance_of()
    {
        $this->assertInstanceOf(stdClass::class, new stdClass);
    }
}

assertJsonStringEqualsJsonString

use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase 
{
    public function test_assert_json_string_equals_json_string()
    {
        $this->assertJsonStringEqualsJsonString(
            json_encode(['Mascot' => 'Tux']),
            json_encode(['Mascot' => 'Tux'])
        );
    }
}