Przeglądaj źródła

adds plaintext, update, and fortunes tests to lithium

Keith Newman 9 lat temu
rodzic
commit
31db4160e0

+ 4 - 73
frameworks/PHP/lithium/app/config/routes.php

@@ -19,79 +19,10 @@
 use lithium\net\http\Router;
 use lithium\core\Environment;
 
-/**
- * With globalization enabled a localized route is configured by connecting a
- * continuation route. Once the route has been connected, all the other
- * application routes become localized and may now carry a locale.
- *
- * Requests to routes like `/en/posts/edit/1138` or `/fr/posts/edit/1138` will
- * carry a locale, while `/posts/edit/1138` keeps on working as it did before.
- */
-if ($locales = Environment::get('locales')) {
-	$template = '/{:locale:' . join('|', array_keys($locales)) . '}/{:args}';
-	Router::connect($template, array(), array('continue' => true));
-}
-
-/**
- * Here, we are connecting `'/'` (the base path) to controller called `'Pages'`,
- * its action called `view()`, and we pass a param to select the view file
- * to use (in this case, `/views/pages/home.html.php`; see `app\controllers\PagesController`
- * for details).
- *
- * @see app\controllers\PagesController
- */
 Router::connect('/json', 'Bench::json');
-
-/**
- * Connect the rest of `PagesController`'s URLs. This will route URLs like `/pages/about` to
- * `PagesController`, rendering `/views/pages/about.html.php` as a static page.
- */
 Router::connect('/db/{:queries}', array('Bench::db', 'queries' => 1));
+Router::connect('/plaintext', 'Bench::plaintext');
+Router::connect('/update/{:queries}', array('Bench::update', 'queries' => 1));
+Router::connect('/fortunes', 'Bench::fortunes');
 
-/**
- * Add the testing routes. These routes are only connected in non-production environments, and allow
- * browser-based access to the test suite for running unit and integration tests for the Lithium
- * core, as well as your own application and any other loaded plugins or frameworks. Browse to
- * [http://path/to/app/test](/test) to run tests.
- */
-if (!Environment::is('production')) {
-	Router::connect('/test/{:args}', array('controller' => 'lithium\test\Controller'));
-	Router::connect('/test', array('controller' => 'lithium\test\Controller'));
-}
-
-/**
- * ### Database object routes
- *
- * The routes below are used primarily for accessing database objects, where `{:id}` corresponds to
- * the primary key of the database object, and can be accessed in the controller as
- * `$this->request->id`.
- *
- * If you're using a relational database, such as MySQL, SQLite or Postgres, where the primary key
- * is an integer, uncomment the routes below to enable URLs like `/posts/edit/1138`,
- * `/posts/view/1138.json`, etc.
- */
-// Router::connect('/{:controller}/{:action}/{:id:\d+}.{:type}', array('id' => null));
-// Router::connect('/{:controller}/{:action}/{:id:\d+}');
-
-/**
- * If you're using a document-oriented database, such as CouchDB or MongoDB, or another type of
- * database which uses 24-character hexidecimal values as primary keys, uncomment the routes below.
- */
-// Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}.{:type}', array('id' => null));
-// Router::connect('/{:controller}/{:action}/{:id:[0-9a-f]{24}}');
-
-/**
- * Finally, connect the default route. This route acts as a catch-all, intercepting requests in the
- * following forms:
- *
- * - `/foo/bar`: Routes to `FooController::bar()` with no parameters passed.
- * - `/foo/bar/param1/param2`: Routes to `FooController::bar('param1, 'param2')`.
- * - `/foo`: Routes to `FooController::index()`, since `'index'` is assumed to be the action if none
- *   is otherwise specified.
- *
- * In almost all cases, custom routes should be added above this one, since route-matching works in
- * a top-down fashion.
- */
-Router::connect('/{:controller}/{:action}/{:args}');
-
-?>
+?>

+ 43 - 1
frameworks/PHP/lithium/app/controllers/BenchController.php

@@ -4,9 +4,14 @@ namespace app\controllers;
 
 use  lithium\action\Controller;
 use  app\models\World;
+use  app\models\Fortune;
 
 class BenchController extends Controller {
 
+    public function plaintext() {
+      return $this->render(array('text' => 'Hello, World!'));
+    }
+
     public function json() {
         return $this->render(array(
             'json' => array('message' => 'Hello, World!')
@@ -17,6 +22,7 @@ class BenchController extends Controller {
         $queries = isset($this->request->query['queries'])
             ? $this->request->query['queries']
             : 1;
+        $queries = min(max(1, $queries), 500);
         $worlds = array();
 
         for ($i = 0; $i < $queries; ++$i) {
@@ -31,4 +37,40 @@ class BenchController extends Controller {
             'json' => $worlds
         ));
     }
-}
+
+    public function update() {
+      $queries = isset($this->request->query['queries'])
+          ? $this->request->query['queries']
+          : 1;
+      $queries = min(max(1, $queries), 500);
+      $worlds = array();
+
+      for ($i = 0; $i < $queries; ++$i) {
+          $id = mt_rand(1, 10000);
+          $random_number = mt_rand(1, 10000);
+          $world = World::first(array(
+              'conditions' => array(
+                  'id' => $id
+              )
+          ));
+          $world->randomNumber = $random_number;
+          $world->save();
+          $worlds[] = $world;
+      }
+
+      return $this->render(array(
+          'json' => $worlds
+      ));
+    }
+
+    public function fortunes() {
+        $fortunes = Fortune::find('all')->to('array');
+        $fortunes[] = array('id' => 0, 'message' => 'Additional fortune added at request time.');
+        usort($fortunes, function($left, $right) {
+           return strcmp($left['message'], $right['message']);
+        });
+
+        $this->set(['fortunes' => $fortunes]);
+        return $this->render(array('layout' => false));
+    }
+}

+ 12 - 0
frameworks/PHP/lithium/app/models/Fortune.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace app\models;
+
+use \lithium\data\Model;
+
+class Fortune extends Model {
+    // stop lithium from pluralizing the table name
+    protected $_meta = array(
+        'source' => 'Fortune'
+    );
+}

+ 12 - 0
frameworks/PHP/lithium/app/views/bench/fortunes.html.php

@@ -0,0 +1,12 @@
+<!doctype html>
+<html>
+<head><title>Fortunes</title></head>
+<body>
+<table>
+<tr><th>id</th><th>message</th></tr>
+<?php foreach($fortunes as $fortune) { ?>
+  <tr><td><?php echo $fortune['id']; ?></td><td><?php echo $h($fortune['message']); ?></td></tr>
+<?php } ?>
+</table>
+</body>
+</html>

+ 3 - 0
frameworks/PHP/lithium/benchmark_config.json

@@ -3,9 +3,12 @@
   "tests": [{
     "default": {
       "setup_file": "setup",
+      "plaintext_url": "/plaintext",
       "json_url": "/json",
       "db_url": "/db",
+      "update_url": "/update?queries=",
       "query_url": "/db?queries=",
+      "fortune_url": "/fortunes",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Fullstack",