// +---------------------------------------------------------------------- namespace tests\helpers; use think\response\View; use think\Session; use think\Url; trait AssertionsTrait { public function assertResponseOk() { $actual = $this->response->getCode(); $this->assertTrue(200 == $actual, "Expected status code 200, got {$actual}."); } public function assertResponseStatus($code) { $actual = $this->response->getCode(); $this->assertEquals($code, $actual, "Expected status code {$code}, got {$actual}."); } public function assertViewHas($key, $value = null) { if (is_array($key)) { $this->assertViewHasAll($key); } else { if (!$this->response instanceof View) { $this->assertTrue(false, 'The response was not a view.'); } else { if (is_null($value)) { $this->assertArrayHasKey($key, $this->response->getVars()); } else { $this->assertEquals($value, $this->response->getVars($key)); } } } } public function assertViewHasAll(array $bindings) { foreach ($bindings as $key => $value) { if (is_int($key)) { $this->assertViewHas($value); } else { $this->assertViewHas($key, $value); } } } public function assertViewMissing($key) { if (!$this->response instanceof View) { $this->assertTrue(false, 'The response was not a view.'); } else { $this->assertArrayNotHasKey($key, $this->response->getVars()); } } public function assertRedirectedTo($uri, $params = []) { $this->assertInstanceOf('think\response\Redirect', $this->response); $this->assertEquals(Url::build($uri, $params), $this->response->getTargetUrl()); } public function assertSessionHas($key, $value = null) { if (is_array($key)) { $this->assertSessionHasAll($key); } else { if (is_null($value)) { $this->assertTrue(Session::has($key), "Session missing key: $key"); } else { $this->assertEquals($value, Session::get($key)); } } } public function assertSessionHasAll(array $bindings) { foreach ($bindings as $key => $value) { if (is_int($key)) { $this->assertSessionHas($value); } else { $this->assertSessionHas($key, $value); } } } }