SilverStripe version: 4.13
Issue creating SapphireTest unit test case in SilverStripe:
I created a unit test php file that gets a URL and checks if all the h1 tags having class custom-heading-title has the CSS style color:red to it…
This is the code I have written.(Found by clicking on URL below)
I am executing this code using terminal command
vendor/bin/phpunit app/tests/StyleTest.php and receiving following error
StyleTest::testCssPropertyExists
Error: Call to undefined method StyleTest::get()
I tried a lot but was unable to resolve this issue. Would much appreciate if anyone can help me on this. There are no online tutorials for this.
I have put the entire code below if the above link does not work. I have placed this code in app → tests → StyleTest.php
<?php
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Image;
class MyTest extends SapphireTest
{
public function testCssPropertyExists()
{
$page = $this->get('/my-page/');
$html = $page->getBody();
$dom = new \DOMDocument();
@$dom->loadHTML($html);
$xpath = new \DOMXPath($dom);
$elements = $xpath->query('//div[@id="my-element"]');
$this->assertEquals(1, $elements->length);
$styleAttribute = $elements[0]->getAttribute('style');
$this->assertStringContainsString('color: red;', $styleAttribute);
}
private function get($url)
{
$request = new HTTPRequest('GET', $url);
$controller = Director::test($url);
$controller->handleRequest($request);
$response = $controller->getResponse();
return $response;
}
}
?>