---
title: "Wie kann ich einen Test für Hook-Funktionen schreiben?"
url: "https://www.typo3.net/d/117429-wie-kann-ich-einen-test-fuer-hook-funktionen-schreiben"
source: "typo3.net"
tags: ["Extensions","Entwicklung","Frage","PHP","RealURL"]
created: "2014-02-18"
last_reply: "2014-02-18"
replies: 0
solved: false
page: 1
pages: 1
lang: "de"
---

# Wie kann ich einen Test für Hook-Funktionen schreiben?

Extensions › Entwicklung · 0 Antworten · gestartet 2014-02-18

## Eröffnungsbeitrag

**olek0707** · 2014-02-18 · [Beitrag #1](https://www.typo3.net/d/117429-wie-kann-ich-einen-test-fuer-hook-funktionen-schreiben/1)

Hallo zusammen,

Ich habe 2 Hook-Funktionen für RealURL geschrieben.
Die Klasse mit den beiden Funktionen sieht so aus:

```php
<?php
class tx_xmrealurl extends tx_realurl {
    var $articleRegex = '/([^\/]*?-\d{4}-\d{2}-\d{2}-a(\d+)\.html)/';

    /*
    * Hook for decodeSpURL_preProc
    * adds the "article" to the path to an article. The realurl-extension needs it to resolve the path
    */
    public function addArticleToPath($params, $obj)
    {
        if (preg_match($this->articleRegex, $params['URL'], $m)) {
            $articleUID = intval($m[2]);
            // retrieve the actual URL-part of the article 
            $res = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('value_alias', 'tx_realurl_uniqalias', 
                                                            'value_id=' . $articleUID . ' AND tablename="tx_news_domain_model_news"', '', 'tstamp DESC', 1);

            // if the requested URL is not match with the URL in the table tx_realurl_uniqalias, do the 301-redirect to URL from the table
            if (is_array($res) && isset($res[0]["value_alias"])) {
                $value_alias = $res[0]["value_alias"] . '.html';
                if ($value_alias != $m[1]) {
                    header('Location: ' . $value_alias, TRUE, 301);
                    exit;
                }
            }
            else {
                // if there is no article with the UID, show the 404-page
                $GLOBALS['TSFE']->pageNotFoundAndExit('Der Artikel mit der UID: ' . $articleUID . ' existiert nicht.');
                return;
            }
        }
        $params['pObj']->extConf['init']['enableCHashCache'] = FALSE;
        $params['URL'] = preg_replace($this->articleRegex, 'artikel/${1}', $params['URL']);
    }


    /*
    * Hook for encodeSpURL_postProc
    * removes "article" from the path to an article
    * the links look like this: http://domain/mannheim/johanniter-grosseinsatz-in-seniorenheim-2014-02-04-a9.html
    */
    public function removeArticleFromPath($params)
    {
        if (preg_match($this->articleRegex, $params['URL'])) {
		    $params['URL'] = str_replace('artikel/', '', $params['URL']);
        }
    }
}
?>
```

Wie kann ich einen Test schreiben, um die Funktionen zu testen? Das sind keine static-Funktionen, das heißt, man muss eine Instanz der Klasse tx_xmrealurl erstellen. Wie mache ich das? Ich muss dann den Konstruktor von tx_realurl aufrufen und alle benötigten Parameter übergeben. Oder geht es anders?

Vielen Dank.

---

Quelle: „Wie kann ich einen Test für Hook-Funktionen schreiben?“, typo3.net, https://www.typo3.net/d/117429-wie-kann-ich-einen-test-fuer-hook-funktionen-schreiben
Beiträge von Mitgliedern des Forums. Bitte mit Link auf die Diskussion zitieren.
