/** * Test a factory. * * @param string $className * @return void */ public function testFactory($classname, array $requiredOptions, array $options) { // Test that the factory does not allow a scalar option. try { $classname::factory(0); $this->testCase->fail('An expected exception was not thrown'); } catch (\Zend\Mvc\Router\Exception\InvalidArgumentException $e) { $this->testCase->assertContains('factory expects an array or Traversable set of options', $e->getMessage()); } // Test required options. foreach ($requiredOptions as $option => $exceptionMessage) { $testOptions = $options; unset($testOptions[$option]); try { $classname::factory($testOptions); $this->testCase->fail('An expected exception was not thrown'); } catch (\Zend\Mvc\Router\Exception\InvalidArgumentException $e) { $this->testCase->assertContains($exceptionMessage, $e->getMessage()); } } // Create the route, will throw an exception if something goes wrong. $classname::factory($options); // Try the same with an iterator. $classname::factory(new ArrayIterator($options)); }
/** * @param \PHPUnit_Framework_TestCase $testCase * @param array[] $expectedCalls */ function assertCalls(\PHPUnit_Framework_TestCase $testCase, array $expectedCalls) { if (array_values($expectedCalls) !== $expectedCalls) { throw new \InvalidArgumentException('$expectedCalls must be a numeric array with no keys missing.'); } $extractFunction = array($this, 'callGetFunction'); $testCase->assertEquals("\n" . implode("\n", array_map($extractFunction, $expectedCalls)) . "\n", "\n" . implode("\n", array_map($extractFunction, $this->calls)) . "\n"); $testCase->assertEquals($expectedCalls, $this->calls); for ($i = 0; TRUE; ++$i) { $actualCall = isset($this->calls[$i]) ? $this->calls[$i] : NULL; $expectedCall = isset($expectedCalls[$i]) ? $expectedCalls[$i] : NULL; if (NULL === $actualCall && NULL === $expectedCall) { break; } if (NULL === $actualCall) { $testCase->fail("Call {$i} missing.\nExpected: " . var_export($expectedCall, TRUE)); break; } if (NULL === $expectedCall) { $testCase->fail("Call {$i} was not expected.\nActual: " . var_export($actualCall, TRUE)); break; } if ($actualCall !== $expectedCall) { $testCase->fail("Call {$i} mismatch.\nExpected: " . var_export($expectedCall, TRUE) . "\nActual: " . var_export($this->calls[$i], TRUE)); break; } } $testCase->assertEquals($expectedCalls, $this->calls); }
/** * Checks if deployment configuration has been changed by a test * * Changing deployment configuration violates isolation between tests, so further tests may become broken. * To fix this issue, find out why this test changes deployment configuration. * If this is intentional, then it must be reverted to the previous state within the test. * After that, the application needs to be wiped out and reinstalled. * * @param \PHPUnit_Framework_TestCase $test * @return void */ public function endTest(\PHPUnit_Framework_TestCase $test) { $config = $this->reader->load(); if ($this->config != $config) { $error = "\n\nERROR: deployment configuration is corrupted. The application state is no longer valid.\n" . 'Further tests may fail.' . " This test failure may be misleading, if you are re-running it on a corrupted application.\n" . $test->toString() . "\n"; $test->fail($error); } }
/** * Overwritten lime_test method * * @see lime_test#fail() */ public function fail($message = '') { // $this->testCase->fail could not be called here, otherwise // the ugly exception // "Exception thrown without a stack frame in Unknown on line 0" will be thrown $this->testCase->fail($message); // @TODO find a solution to be able calling the fail method directly #$this->testCase->assertTrue(false, 'FAIL: ' . $message); }
/** * Assert that conformation message is present. * * @param Customer $customer * @param CustomerAccountEdit $customerAccountEdit * @return void */ public function processAssert(Customer $customer, CustomerAccountEdit $customerAccountEdit) { $validationMessages = $customerAccountEdit->getAccountInfoForm()->getValidationMessages($customer); if (isset($validationMessages['password_confirmation'])) { \PHPUnit_Framework_Assert::assertEquals(self::CONFIRMATION_MESSAGE, $validationMessages['password_confirmation'], 'Wrong password confirmation validation text message.'); } else { \PHPUnit_Framework_TestCase::fail('Password confirmation validation message is absent.'); } }
/** * Asserts that the log buffer does NOT contain specified message * * @param string $expected Message subsctring * @param string $errmsg The error message to display. * * @return void */ public function assertNotInLogs($message, $errorMsg = "Unexpected string '%s' found in logs:\n\n%s") { foreach ($this->getLines() as $log) { if (false !== stripos($log, $message)) { $this->testCase->fail(sprintf($errorMsg, $message, var_export($this->getLines(), true))); } } $this->testCase->assertEquals(1, 1); // increase number of positive assertions }
public static function isSpecification(\PHPUnit_Framework_TestCase $test) { if ($test->getName(false) === NULL) { throw new \PHPUnit_Framework_Exception('PHPUnit_Framework_TestCase::$name must not be NULL.'); } try { $class = new \ReflectionClass($test); $method = $class->getMethod($test->getName(false)); $methodName = $method->getName(); return substr($methodName, -4) == 'Spec' || preg_match('/\\s+@spec\\s+/', $method->getDocComment()); } catch (\ReflectionException $e) { $test->fail($e->getMessage()); } return false; }
public static function fail($message = '') { // save in a static var that this particular test has failed // (but only if not called from subclass objects / multitests) if (function_exists('debug_backtrace') && strtolower(get_called_class()) == 'localhosttests') { $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); for ($i = 0; $i < count($trace); $i++) { if (strpos($trace[$i]['function'], 'test') === 0) { self::$failed_tests[$trace[$i]['function']] = true; break; } } } parent::fail($message); }
/** * Analyze results of aggregated tests execution and complete test case appropriately * * @param array $results * @param int $passed * @return void */ protected function processResults(array $results, $passed) { $totalCountsMessage = sprintf('Passed: %d, Failed: %d, Incomplete: %d, Skipped: %d.', $passed, count($results['PHPUnit_Framework_AssertionFailedError']), count($results['PHPUnit_Framework_IncompleteTestError']), count($results['PHPUnit_Framework_SkippedTestError'])); if ($results['PHPUnit_Framework_AssertionFailedError']) { $this->_testCase->fail($totalCountsMessage . PHP_EOL . implode(PHP_EOL, $results['PHPUnit_Framework_AssertionFailedError'])); } if (!$results['PHPUnit_Framework_IncompleteTestError'] && !$results['PHPUnit_Framework_SkippedTestError']) { return; } $message = $totalCountsMessage . PHP_EOL . implode(PHP_EOL, $results['PHPUnit_Framework_IncompleteTestError']) . PHP_EOL . implode(PHP_EOL, $results['PHPUnit_Framework_SkippedTestError']); if ($results['PHPUnit_Framework_IncompleteTestError']) { $this->_testCase->markTestIncomplete($message); } elseif ($results['PHPUnit_Framework_SkippedTestError']) { $this->_testCase->markTestSkipped($message); } }
/** * testFiles * * Run simple syntax checks, if the filename ends with pass.php - expect it to pass */ public static function testProvider() { $tests = array(); $standard = dirname(dirname(__FILE__)); if (basename($standard) !== 'CakePHP') { PHPUnit_Framework_TestCase::fail("The dirname for the standard must be CakePHP"); } $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname(__FILE__) . '/files')); foreach ($iterator as $dir) { if ($dir->isDir()) { continue; } $file = $dir->getPathname(); $expectPass = substr($file, -8) === 'pass.php'; $tests[] = array($file, $standard, $expectPass); } return $tests; }
/** * Overwritten lime_test method * * @see lime_test#fail() */ public function fail($message = '') { $this->testCase->fail($message); return false; }
/** * Fails the test with specified error message * * @param string $message * @param \PHPUnit_Framework_TestCase $test * @throws \Exception */ private static function fail($message, \PHPUnit_Framework_TestCase $test) { $test->fail("{$message} in the test '{$test->toString()}'"); throw new \Exception('The above line was supposed to throw an exception.'); }
public function existeUnObjetoDe($name, $arg1, $arg2) { foreach ($this->getObjects($name) as $objeto) { if ($objeto[$arg1] == $arg2) { return true; } } \PHPUnit_Framework_TestCase::fail("No existe {$arg1} == {$arg2}"); return false; }
protected function prepare() { $this->container = \Mockery::mock(Container::class)->makePartial(); $schemaMapper = \Mockery::mock(SchemaMapper::class)->makePartial(); $schemaMapper->shouldReceive('buildCredentialsFromInlineFormat')->once()->passthru(); $this->container->shouldReceive('resolve')->once()->with('Domain\\Mapper\\Schema')->andReturn($schemaMapper); $credentialsMatcher = \Mockery::on(function ($args) { /** @var CredentialsInterface $credentials */ $credentials = $args[0]; \PHPUnit_Framework_TestCase::assertInstanceOf(CredentialsInterface::class, $credentials); $actual = $credentials->toArray(); $expected = ["username" => "user1", "password" => "pass2", "host" => "sample.host", "database" => "sample_database"]; if ($expected != $actual) { \PHPUnit_Framework_TestCase::fail('Check credentials format or sample.'); } return true; }); $pdo = \Mockery::mock(\PDO::class); $this->container->shouldReceive('resolve')->once()->with('Persistence\\Storage\\PDO', $credentialsMatcher)->andReturn($pdo); $storageMatcher = \Mockery::on(function ($args) use($pdo) { /** @var StorageInterface $storage */ $storage = $args[0]; if ($pdo != $storage) { \PHPUnit_Framework_TestCase::fail('Check pipes.'); } return true; }); $gateway = \Mockery::mock(MySQLGateway::class); $this->container->shouldReceive('resolve')->once()->with('Persistence\\Gateway\\Schema\\MySQL', $storageMatcher)->andReturn($gateway); $gatewayAndMapperMatcher = \Mockery::on(function ($args) use($gateway, $schemaMapper) { if ($args[0] != $gateway) { \PHPUnit_Framework_TestCase::fail('Gateway mismatch.'); } if ($args[1] != $schemaMapper) { \PHPUnit_Framework_TestCase::fail('Mapper mismatch.'); } return true; }); $this->schemaService = \Mockery::mock(SchemaService::class); $this->container->shouldReceive('resolve')->once()->with('Infrastructure\\Services\\Schema', $gatewayAndMapperMatcher)->andReturn($this->schemaService); $classCommand = '\\DatabaseInspect\\Console\\Command\\DetailsCommand[execute]'; $this->command = \Mockery::mock($classCommand, [$this->container])->makePartial(); $this->input = \Mockery::mock(ArgvInput::class); $this->input->shouldReceive('getArgument')->once()->andReturn('user1:pass2@sample.host/sample_database'); $this->output = \Mockery::mock(ConsoleOutput::class); }