执行方式:./vendor/bin/phpunit
新建测试类:
php artisan make:test --unit nameTest
会在App同级目录tests/Unit 下生成一个nameTest.php 的文件
./vendor/bin/phpunit --filter testAssertClassHasStaticAttribute
表示只执行 testAssertClassHasStaticAttribute() 方法
注意:如果有两个方法名字都一样,则都会执行
--filter=LogTest::testAssertClassHasStaticAttribute
这样就会执行单个php类的方法.
执行目录层级: Unit ==>
json.txt
LogTest.php
```
<?php
namespace Tests\Unit;
use Molbase\Box;
use Tests\TestCase;
class LogTest extends TestCase
{
/**
* bool
*
* @return void
*/
/**
* Description: 真假
* User: jack
* Date: 2016/3/12 14:59
*/
public function testTrueOrFalse()
{
$this->assertTrue(true, 'OJBK');
$this->assertFalse(false, 'not ok');
}
/**
* Description: null与否
* User: jack
* Date: 2016/3/12 14:59
*/
public function testNull()
{
$this->assertNotNull('');
$this->assertNull(null);
}
/**
* Description: 相等
* User: jack
* Date: 2016/3/12 14:59
*/
public function testEquals()
{
$this->assertEquals('jack', 'jack');
$this->assertSame('2204', '2204');//类型也必须一样
}
/**
* Description:大小
* User: jack
* Date: 2016/3/12 14:59
*/
public function testAssertLessThan()
{
$this->assertLessThan(2, 1);
$this->assertLessThanOrEqual(3, 2);
$this->assertNan('1');
$this->assertRegExp('/13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]/', '/138');
$this->assertStringMatchesFormat('%i', '1');
$this->assertStringMatchesFormat('%e', '/');
}
/**
* Description: 是否全是相应的type类型
* User: jack
* Date: 2016/3/12 14:36
*/
public function testAssertContainsOnly()
{
$this->assertContainsOnly('string', ['1', '2', '3']);
}
/**
* Description: 空
* User: jack
* Date: 2016/3/12 14:59
*/
public function testAssertEmpty()
{
$this->assertEmpty(['foo']);
}
public function testAssertCount()
{
$this->assertCount(1, ['111']);
}
/**
* Description: 数组是否存在这个key
* User: jack
* Date: 2016/3/12 15:00
*/
public function testAssertArrayHasKey()
{
$this->assertArrayHasKey('jack', ['jack' => 'jack', 'jackey', 'chan'], 'sorry not has key');
}
public function testAssertArrayNotHasKey()
{
$this->assertArrayNotHasKey('jack1', ['jack', 'jackey', 'chan'], 'sorry not has key');
}
/**
* Description:属性
* User: jack
* Date: 2016/3/12 15:01
*/
public function testAssertClassHasAttribute()
{
$this->assertClassHasAttribute('name', Box::class, 'sorry no attribute name');
}
/**
* Description: subset 子集 是否是子集
* User: jack
* Date: 2016/3/12 14:39
*/
public function testAssertArraySubset()
{
$this->assertArraySubset(['config' => ['key-a']], ['config' => ['key-a', 'key-b']]);
}
/**
* Description:
* User: jack
* Date: 2016/3/12 14:40
*/
public function testAssertContains()
{
$this->assertContains('jack', 'jacksadfasdf');
}
public function testAssertContainsOnlyInstancesOf()
{
$this->assertInstanceOf(\Exception::class, new \Exception());
$this->assertContainsOnlyInstancesOf(LogTest::class, [new LogTest]);
}
public function testAssertClassHasStaticAttribute()
{
$this->assertClassHasStaticAttribute('foo', LogTest::class);
}
public function testAssertDirectoryExists()
{
$this->assertDirectoryExists('tests/Unit');
}
public function testAssertDirectoryIsReadable()
{
$this->assertDirectoryIsReadable('tests/Unit');
}
public function testAssertDirectoryIsWritable()
{
$this->assertDirectoryIsWritable('tests/Unit');
}
public function testAssertFileEquals()
{
$this->assertFileEquals('tests/Unit/json.txt', 'tests/Unit/json1.txt');
}
public function testAssertGreaterThan()
{
$this->assertEquals(2, 1 + 1);
$this->assertGreaterThan(5, 6, 'ojbk');
$this->assertGreaterThanOrEqual(5, 8, 'ojbk');
$this->assertInfinite(INF, 'is infinite');
$this->assertInternalType('string', '42');
$this->assertContainsOnly('string', ['1', '2', '3']);
}
public function testAssertJsonStringEqualsJsonFile()
{
$this->assertJsonFileEqualsJsonFile('tests/Unit/json.txt', 'tests/Unit/json1.txt');
$this->assertJsonStringEqualsJsonFile(
'tests/Unit/json.txt', json_encode(['Mascot' => 'ux'])
);
$this->assertJsonStringEqualsJsonString(
json_encode(['Mascot' => 'cx5']),
json_encode(['Mascot' => 'tharu'])
);
}
public function testAssertStringEndsWith()
{
$this->assertStringEndsWith('er', 'cooler');
$this->assertStringStartsWith('re', 'repeat');
}
}
```
json.txt 里面的内容
{"Mascot": "ux"}