Uploaded image for project: 'PHP Driver: Library'
  1. PHP Driver: Library
  2. PHPLIB-1139

Refactor logic for various document casts in tests

    • Type: Icon: Task Task
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Tests
    • None

      In mongodb/mongo-php-library#1077 for PHPLIB-1122, andreas.braun@mongodb.com suggested creating a utility function to handle repetitive document casts in data providers:

      Since we're often doing these same casts, WDYT about creating a utility function/class that handles this? Consider something like this: https://3v4l.org/AXNBq

      The closure usage is necessary if we don't want to be prescriptive about what exactly is being cast, unfortunately that also makes the logic a little strange to understand. Feel free to create a ticket for this if you think the change is worthwhile but don't want to apply it to this PR.

      Embedding the 3v4l snippet below:

      <?php
      
      function applyDocumentCasts($keyPattern, Closure $closure)
      {
              $casts = [
                  'unchanged' => fn ($item) => $item,
                  'object' => fn ($item) => (object) $item,
                  // 'Serializable' => fn ($item) => new BSONDocument($item),
                  // 'Document' => fn ($item) => Document::fromPHP($item),
              ];
              
              return array_combine(
                  array_map(fn ($castName) => sprintf($keyPattern, $castName), array_keys($casts)),
                  array_map(fn ($cast) => $closure($cast), $casts),
              );
      }
      
      function provideFilterDocuments(): array
      {
          $expectedQuery = (object) ['x' => 1];
          
          return applyDocumentCasts(
              '%s',
              fn ($cast) => [$cast(['x' => 1]), $expectedQuery]
          );
      }
      
      var_dump(provideFilterDocuments());
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: