test fix
[civicrm-core.git] / tests / phpunit / CRM / Core / ErrorTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Tests for linking to resource files
32 */
33 class CRM_Core_ErrorTest extends CiviUnitTestCase {
34
35 public function setUp() {
36 parent::setUp();
37 $config = CRM_Core_Config::singleton();
38 $this->oldConfigAndLogDir = $config->configAndLogDir;
39 $config->configAndLogDir = $this->createTempDir('test-log-');
40 }
41
42 public function tearDown() {
43 $config = CRM_Core_Config::singleton();
44 $config->configAndLogDir = $this->oldConfigAndLogDir;
45 parent::tearDown();
46 }
47
48 /**
49 * Make sure that formatBacktrace() accepts values from debug_backtrace()
50 */
51 public function testFormatBacktrace_debug() {
52 $bt = debug_backtrace();
53 $msg = CRM_Core_Error::formatBacktrace($bt);
54 $this->assertRegexp('/CRM_Core_ErrorTest->testFormatBacktrace_debug/', $msg);
55 }
56
57 /**
58 * Make sure that formatBacktrace() accepts values from Exception::getTrace()
59 */
60 public function testFormatBacktrace_exception() {
61 $e = new Exception('foo');
62 $msg = CRM_Core_Error::formatBacktrace($e->getTrace());
63 $this->assertRegexp('/CRM_Core_ErrorTest->testFormatBacktrace_exception/', $msg);
64 }
65
66 /**
67 * We have two coding conventions for writing to log. Make sure that they work together.
68 *
69 * This tests a theory about what caused CRM-10766.
70 */
71 public function testMixLog() {
72 CRM_Core_Error::debug_log_message("static-1");
73 $logger = CRM_Core_Error::createDebugLogger();
74 CRM_Core_Error::debug_log_message("static-2");
75 $logger->info('obj-1');
76 CRM_Core_Error::debug_log_message("static-3");
77 $logger->info('obj-2');
78 CRM_Core_Error::debug_log_message("static-4");
79 $logger2 = CRM_Core_Error::createDebugLogger();
80 $logger2->info('obj-3');
81 CRM_Core_Error::debug_log_message("static-5");
82 $this->assertLogRegexp('/static-1.*static-2.*obj-1.*static-3.*obj-2.*static-4.*obj-3.*static-5/s');
83 }
84
85 /**
86 * @param $pattern
87 */
88 public function assertLogRegexp($pattern) {
89 $config = CRM_Core_Config::singleton();
90 $logFiles = glob($config->configAndLogDir . '/CiviCRM*.log');
91 $this->assertEquals(1, count($logFiles), 'Expect to find 1 file matching: ' . $config->configAndLogDir . '/CiviCRM*log*/');
92 foreach ($logFiles as $logFile) {
93 $this->assertRegexp($pattern, file_get_contents($logFile));
94 }
95 }
96
97 }